2

I need to scan for and gather information about local Wifi access points in Mac OS X Linux in C++. This perhaps uses Wlan or something akin to WlanScan. I have similar code working in Windows that cannot possibly be translated.

This code is being built in a FireBreath development project.

Do you have an example or suggestion for scanning Wifi access points in C++ on Mac?

David Manpearl
  • 12,362
  • 8
  • 55
  • 72

1 Answers1

3

You can't do this in plain C++, but you can use Objective-C++ (you source file just need to have .mm extension and you can use Objective-C right in your C++ code). Take a look at CoreWLAN framework. You should write something like this:

#import <CoreWLAN/CoreWLAN.h>

struct AccessPoint
{
    string ssid;
    string bssid;
    int rssi;
};

vector<AccessPoint> ScanAir(const string& interfaceName)
{
    NSString* ifName = [NSString stringWithUTF8String:interfaceName.c_str()];
    CWInterface* interface = [CWInterface interfaceWithName:ifName];

    NSError* error = nil;
    NSArray* scanResult = [[interface scanForNetworksWithSSID:nil error:&error] allObjects];
    if (error)
    {
        NSLog(@"%@ (%ld)", [error localizedDescription], [error code]);
    }

    vector<AccessPoint> result;
    for (CWNetwork* network in scanResult)
    {
        AccessPoint ap;
        ap.ssid  = string([[network ssid] UTF8String]);
        ap.bssid = string([[network bssid] UTF8String]);
        ap.rssi = [network rssiValue];
        result.push_back(ap);
    }

    return result;
}

I didn't test this code, but I use similar code in my project, so it should work. Note also that I'm using ARC here. If you'll get some errors - feel free to ask in comments.
There is also example in apple documentation, but it is somewhat outdated (it is for OS X 10.6). There were some changes in OS X 10.7.

Note that CoreWLAN framework requires OS X 10.6 or greater.

cody
  • 3,233
  • 1
  • 22
  • 25
  • Thanks @cody. This looks extremely promising. I see that `scanForNetworksWithSSID` is blocking. Perfect, I am already in an async thread. Are you saying that I can use Objective-C knowing that I am working in FireBreath? (i.e. To your knowledge, will Objective-C work in FireBreath?) – David Manpearl Apr 03 '13 at 06:50
  • Regarding Objective-C in FireBreath, this seems to answer my question: http://stackoverflow.com/questions/12325559/how-do-i-add-objective-c-code-to-a-firebreath-project – David Manpearl Apr 03 '13 at 06:52
  • sorry, I don't know what FireBreath is, but Objective-C is mostly runtime, and it is native for OS X so I think it will – cody Apr 03 '13 at 06:53
  • 1
    [FireBreath](http://www.firebreath.org) is awesome if you need to make cross-platform/cross-browser Internet plugins for web-browsers. One stop shopping. – David Manpearl Apr 03 '13 at 06:56
  • Hi @cody. What do you use for `interfaceName`, as the input to `ScanAir()`? – David Manpearl Apr 03 '13 at 15:41
  • interface name :) en0, en1 – cody Apr 03 '13 at 16:02
  • Ok, thanks. I also believe "network" above should be "item" in three places (i.e. "[[item ssid] UTF8String]"). If I am correct, one of us should edit the answer, above. – David Manpearl Apr 03 '13 at 16:05
  • As are you @cody. Your example provided complete success for what I needed. Thanks again. – David Manpearl Apr 03 '13 at 19:19
  • @code, Do you know how to tell which if any of these networks are currently connected? Do you know the status? This information does not appear in the `CWNetwork` class. – David Manpearl Apr 04 '13 at 02:36
  • 1
    @DavidManpearl You can ask your interface about it (CWInterface). Properties names are the same. And there will be nil in case if the interface is not connected – cody Apr 04 '13 at 05:29
  • Thanks. Worked like a charm. – David Manpearl Apr 04 '13 at 18:01
  • @cody, I had a same requirement I need to list all the available networks and have find other details like IP address , MAC address of each network. Is there any way to get such details. – Vijay Yadav Apr 28 '15 at 12:52
  • @VijayYadav Can't say for sure right now, but I think you can't get this via COREWlan framework. Take a look at SystemConfiguration framework: https://developer.apple.com/library/mac/documentation/Networking/Reference/SysConfig/ May be you can get some info from COREWlan, and use it in SysConfig to get the other – cody Apr 29 '15 at 12:30