How to get AirPort Connection Status
-
I want to know when my computer connects/disconnects to WiFi/AirPort
network.
Is there a way to get the status of connection to a WiFi network. I tried
using System Configuration API,but I could not get the status. Here is the
snippet
--
static void MyNetworkConnectionCallBack(SCNetworkConnectionRef
connection,SCNetworkConnectionStatus stat,void *info)
{
printf("Call back called \n");
SCNetworkConnectionStatus status = SCNetworkConnectionGetStatus(connection);
switch(status) {
case kSCNetworkConnectionInvalid:
printf("Connection invalid\n");
break;
case kSCNetworkConnectionDisconnected:
printf("Connection disconnected\n");
break;
case kSCNetworkConnectionConnecting:
printf("Connection connecting\n");
break;
case kSCNetworkConnectionConnected:
printf("Connection connected\n");
break;
case kSCNetworkConnectionDisconnecting:
printf("Connection disconnecting\n");
break;
}
}
int main (int argc, const char * argv[]) {
CFDictionaryRef userOptions;
bool scResult;
SCNetworkConnectionContext scncctx = {0, NULL, NULL, NULL, NULL};
CFStringRef currentServiceId;
scResult = SCNetworkConnectionCopyUserPreferences(NULL, ¤tServiceId,
&userOptions);
SCNetworkConnectionRef connection = nil;
connection = SCNetworkConnectionCreateWithServiceID (NULL,
currentServiceId,
MyNetworkConnectionCallBack, &scncctx );
SCNetworkConnectionStatus status =
SCNetworkConnectionGetStatus(connection);
if(status == kSCNetworkConnectionConnected)
printf("connected \n");
else
printf("current status :%d\n", status);
SCNetworkConnectionScheduleWithRunLoop(connection,CFRunLoopGetCurrent(),kCFRunLoopDefaultMode);
CFRunLoopRun();
return 0;
}
--
Am able to get connection status if I connect to a modem, but it fails when
connected
to a WiFi/AirPort network.
Thanks,
Rashmi -
On Sep 18, 2008, at 00:50 , Rashmi Vyshnavi wrote:
> I want to know when my computer connects/disconnects to WiFi/AirPort[...]
> network.
> Is there a way to get the status of connection to a WiFi network. I
> tried
> using System Configuration API,but I could not get the status. Here
> is the
> snippet
> --
>
>
> --
> Am able to get connection status if I connect to a modem, but it
> fails when
> connected
> to a WiFi/AirPort network.
According to the documentation, the SCNetworkConnection API currently
only works with PPP. -
On Thu, Sep 18, 2008 at 12:50 AM, Rashmi Vyshnavi
<rashmivyshnavi...> wrote:
> I want to know when my computer connects/disconnects to WiFi/AirPort
> network.
> Is there a way to get the status of connection to a WiFi network. I tried
> using System Configuration API,but I could not get the status. Here is the
> snippet
I am not greatly familiar with them, but it looks like you could use
the SC Reachability API to get notified of when a host becomes
reachable or unreachable. If wifi is your only internet connection
then any remote host will become unreachable when you disconnect, and
reachable when you connect.
Mike -
I found this page to be helpful:
http://developer.apple.com/qa/qa2001/qa1133.html
It's about the currently-logged in user, but the MyNotificationProc
callback is called for a lot of changes, including IIRC the network
connection transitions.
In the callback function you can do something like this:
NSArray* arr = (NSArray*)changedKeys;
NSLog(@"changed keys %@", [arr description]);
to sort of figure out what kinds of information you're getting.
--Bob
--
Bob Clark
Lead Software Development Engineer
RealPlayer Mac/Unix
RealNetworks, Inc.
On Sep 17, 2008, at 9:50 PM, Rashmi Vyshnavi wrote:
> I want to know when my computer connects/disconnects to WiFi/AirPort
> network.
> Is there a way to get the status of connection to a WiFi network. I
> tried
> using System Configuration API,but I could not get the status. Here
> is the
> snippet
> --
> static void MyNetworkConnectionCallBack(SCNetworkConnectionRef
> connection,SCNetworkConnectionStatus stat,void *info)
> {
> printf("Call back called \n");
> SCNetworkConnectionStatus status =
> SCNetworkConnectionGetStatus(connection);
> switch(status) {
> case kSCNetworkConnectionInvalid:
> printf("Connection invalid\n");
> break;
> case kSCNetworkConnectionDisconnected:
> printf("Connection disconnected\n");
> break;
> case kSCNetworkConnectionConnecting:
> printf("Connection connecting\n");
> break;
> case kSCNetworkConnectionConnected:
> printf("Connection connected\n");
> break;
> case kSCNetworkConnectionDisconnecting:
> printf("Connection disconnecting\n");
> break;
> }
>
> }
>
> int main (int argc, const char * argv[]) {
> CFDictionaryRef userOptions;
> bool scResult;
> SCNetworkConnectionContext scncctx = {0, NULL, NULL, NULL, NULL};
>
> CFStringRef currentServiceId;
>
> scResult = SCNetworkConnectionCopyUserPreferences(NULL,
> ¤tServiceId,
> &userOptions);
>
>
> SCNetworkConnectionRef connection = nil;
> connection = SCNetworkConnectionCreateWithServiceID (NULL,
> currentServiceId,
> MyNetworkConnectionCallBack, &scncctx );
> SCNetworkConnectionStatus status =
> SCNetworkConnectionGetStatus(connection);
> if(status == kSCNetworkConnectionConnected)
> printf("connected \n");
> else
> printf("current status :%d\n", status);
>
> SCNetworkConnectionScheduleWithRunLoop
> (connection,CFRunLoopGetCurrent(),kCFRunLoopDefaultMode);
>
>
>
> CFRunLoopRun();
>
> return 0;
> }
> --
> Am able to get connection status if I connect to a modem, but it
> fails when
> connected
> to a WiFi/AirPort network.
>
> Thanks,
> Rashmi
-
Thanks for the help..
On Fri, Sep 19, 2008 at 2:15 AM, Bob Clark <bobclark...> wrote:
> I found this page to be helpful:
>
> http://developer.apple.com/qa/qa2001/qa1133.html
>
> It's about the currently-logged in user, but the MyNotificationProc
> callback is called for a lot of changes, including IIRC the network
> connection transitions.
>
> In the callback function you can do something like this:
>
> NSArray* arr = (NSArray*)changedKeys;
> NSLog(@"changed keys %@", [arr description]);
>
> to sort of figure out what kinds of information you're getting.
>
> --Bob
>
>
> --
> Bob Clark
> Lead Software Development Engineer
> RealPlayer Mac/Unix
> RealNetworks, Inc.
>
>
>
> On Sep 17, 2008, at 9:50 PM, Rashmi Vyshnavi wrote:
>
> I want to know when my computer connects/disconnects to WiFi/AirPort
>> network.
>> Is there a way to get the status of connection to a WiFi network. I tried
>> using System Configuration API,but I could not get the status. Here is the
>> snippet
>> --
>> static void MyNetworkConnectionCallBack(SCNetworkConnectionRef
>> connection,SCNetworkConnectionStatus stat,void *info)
>> {
>> printf("Call back called \n");
>> SCNetworkConnectionStatus status =
>> SCNetworkConnectionGetStatus(connection);
>> switch(status) {
>> case kSCNetworkConnectionInvalid:
>> printf("Connection invalid\n");
>> break;
>> case kSCNetworkConnectionDisconnected:
>> printf("Connection disconnected\n");
>> break;
>> case kSCNetworkConnectionConnecting:
>> printf("Connection connecting\n");
>> break;
>> case kSCNetworkConnectionConnected:
>> printf("Connection connected\n");
>> break;
>> case kSCNetworkConnectionDisconnecting:
>> printf("Connection disconnecting\n");
>> break;
>> }
>>
>> }
>>
>> int main (int argc, const char * argv[]) {
>> CFDictionaryRef userOptions;
>> bool scResult;
>> SCNetworkConnectionContext scncctx = {0, NULL, NULL, NULL, NULL};
>>
>> CFStringRef currentServiceId;
>>
>> scResult = SCNetworkConnectionCopyUserPreferences(NULL, ¤tServiceId,
>> &userOptions);
>>
>>
>> SCNetworkConnectionRef connection = nil;
>> connection = SCNetworkConnectionCreateWithServiceID (NULL,
>> currentServiceId,
>> MyNetworkConnectionCallBack, &scncctx );
>> SCNetworkConnectionStatus status =
>> SCNetworkConnectionGetStatus(connection);
>> if(status == kSCNetworkConnectionConnected)
>> printf("connected \n");
>> else
>> printf("current status :%d\n", status);
>>
>>
>> SCNetworkConnectionScheduleWithRunLoop(connection,CFRunLoopGetCurrent(),kCFRunLoopDefaultMode);
>>
>>
>>
>> CFRunLoopRun();
>>
>> return 0;
>> }
>> --
>> Am able to get connection status if I connect to a modem, but it fails
>> when
>> connected
>> to a WiFi/AirPort network.
>>
>> Thanks,
>> Rashmi
>>
>
>
>
--
Rashmi Vyshnavi



