关于Objective C:无法在MacOS上获得实际的bssid值

 2021-04-09 

can't get actual bssid value on MacOS

我正在尝试获取Mac OS X 10.14上当前WiFi连接的BSSID,但得到nil

返回nil的代码如下:

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

尽管SSID返回有效值:

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

其他解决方案适用于iOS,例如使用此处提出的CaptiveNetworks框架如何获取当前访问点的MAC地址/ BSSID?但是有些方法(如CNCopyCurrentNetworkInfo)-MacOS不支持。


这不是最佳解决方案,但至少在Qt中有效:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
    }
}


我为OSX支付的2美分,以防万一:

  • 使用CoreWLAN
  • 添加网络扩展
  • 致电:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
       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
         ...

} ??

请谨慎激活:

enter