0

I am trying to get BSSID of current WiFi connection on Mac OS X 10.14 but get nil.

The code returning nil is the following:

NSString *bssid = [[[CWWiFiClient sharedWiFiClient] interface] bssid];

though SSID returns valid value:

NSString *ssid = [[[CWWiFiClient sharedWiFiClient] interface] ssid];

Other solutions works for iOS, e.g. using CaptiveNetworks framework proposed here How do I get the current access point's MAC address/BSSID?, but some methods, like CNCopyCurrentNetworkInfo - not supported for MacOS.

atlascoder
  • 2,746
  • 3
  • 26
  • 34
  • Possible duplicate of [How do I get the current access point's MAC address/BSSID?](https://stackoverflow.com/questions/1460540/how-do-i-get-the-current-access-points-mac-address-bssid) – Cinder Biscuits Oct 08 '18 at 01:47
  • No, @CinderBiscuits, it's not a duplication because my question is about MacOS not iOS – atlascoder Oct 10 '18 at 12:26
  • Did you try the solution from the other question? You use the same API for both MacOS and iOS. – Cinder Biscuits Oct 10 '18 at 13:29
  • 1
    My problem of the proposed solution was in that `CNCopyCurrentNetworkInfo` is supported only on iOS. I get BSSID executing `/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I` in separate process and parsing results, but it does not looks like a good solution.. – atlascoder Oct 10 '18 at 23:35
  • @atlascoder have you found the solution ? – zack Jul 02 '19 at 08:02
  • @zack - yes, but not a good one a for me - I call `/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport` and parse its text output – atlascoder Jul 02 '19 at 08:33
  • @atlascoder so how can i pass interface `CWInterface` in the process and get `bssid` for that paticular interface , how to do that ? – zack Jul 02 '19 at 11:00
  • No way to use CWInterface - just call `/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I` as a separate process on the host and wait for its text outputs. then extract row with BSSID – atlascoder Jul 02 '19 at 11:14
  • @zack, sorry, I don't know how to run a process from ObjC/Swift - I use Qt, would you like that I've added this code as an answer here? – atlascoder Jul 02 '19 at 11:17
  • ya i would like please post as an answer how to add this mechanism. – zack Jul 02 '19 at 11:28
  • @zack see below – atlascoder Jul 02 '19 at 11:41

2 Answers2

1

my two cents for OSX, in case:

  • Use CoreWLAN
  • Add NetworkExtensions
  • Call:

       let client = CWWiFiClient.shared()
        guard let interfaces : [CWInterface] = client.interfaces() else{
            return nil
        }
    
        // on iOS we got the first, here we should return multiple interfaces.
        var ssid: String?
        var bssid: String?
        var interfaceNameString: String?
    
        for interface in interfaces{
    
            ssid = interface.ssid()
            bssid = interface.bssid()
            interfaceNameString = interface.interfaceName
         ...
    

}

be careful to activate:

enter image description here

if on App Store for OSX, when activating Sandboxing.

ingconti
  • 10,876
  • 3
  • 61
  • 48
1

This is not the best solution, but at least it works in Qt:

QString getBssidOnMac() {
    QStringList arguments;
    arguments << "-I";
    QProcess process;
    process.start("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport", arguments);
    process.waitForFinished();
    QString outp = process.readAllStandardOutput();
    QRegExp re_bssid(".*(BSSID\\:\\s([0-9a-fA-F]{1,2}(\\:[0-9a-fA-F]{1,2}){5})).*");
    if (re_bssid.indexIn(outp) !=-1) {
        return re_bssid.cap(2);
    }
    else {
        return QString();
    }
}
atlascoder
  • 2,746
  • 3
  • 26
  • 34
  • what are files and framework do we need import too run this code in objective c project – zack Jul 02 '19 at 11:50
  • Well, this code is for Qt that is not for inclusion to a Xcode project but rather to replace that. I suggest you to find a way to run terminal commands from ObjC - it will work in the same way – atlascoder Jul 02 '19 at 11:54