iOS - 获取WIFI的BSSID

  1. 对于BSSID 和 Mac地址的理解
    MAC地址这里是指WIFI MAC地址。一般情况下mac地址和BSSID是一致的,但是如果涉及到一个网卡上多个虚拟AP(multi-AP)情况下除了第一个BSSID外其他BSSID往往是通过MAC来算出来,这个时候 Mac adress != BSSID。 但是可以由BSSID 推出Mac adress。
  2. iOS 中的BSSID。
    iOS中没法直接获取到WIFI的Mac adress。通常获取到的都是BSSID。
  3. 获取BSSID的基本实现。
  class func getUsedBSSID() -> String {
        if #available(iOS 13.0, *) {
            let authStatus = CLLocationManager.authorizationStatus()
            if authStatus == CLAuthorizationStatus.denied || authStatus == CLAuthorizationStatus.notDetermined {
                if authStatus == CLAuthorizationStatus.denied {
                    if let keyRoot = SSAppDelegate.shared?.window?.rootViewController {
                        UIAlertController.showIn(keyRoot,
                            alertTitle: "Error",
                            alertMessage: "Please open permission of location first.",
                            cancelText: "Cancel",
                            defaultText: "OK",
                            cancelAction: nil,
                            defaultAction: {
                                gotoSettings(url: UIApplication.openSettingsURLString)
                        },
                            completion: nil)
                    }
                } else {
                    SKSRunTime.shared.locationManager.requestWhenInUseAuthorization()
                }
                
                return kErrorLocation
            }
        }
        
        let interfaces = CNCopySupportedInterfaces()
        var bssid = ""
        if interfaces != nil {
            let interfacesArray = CFBridgingRetain(interfaces) as! Array
            if interfacesArray.count > 0 {
                let interfaceName = interfacesArray[0] as! CFString
                let ussafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName)
                if (ussafeInterfaceData != nil) {
                    let interfaceData = ussafeInterfaceData as! Dictionary
                    bssid = interfaceData["BSSID"]! as! String
                    let strs = bssid.components(separatedBy: ":")
                    var newStr = ""
                    for str in strs {
                        if str.count == 1 {
                            newStr = newStr + "0\(str):"
                        } else {
                            newStr += "\(str):"
                        }
                    }
                    if newStr != "" {
                        let resultStr = newStr as NSString
                        newStr = resultStr.substring(to: resultStr.length - 1)
                    }
                    bssid = newStr
                }
            }
        }
        return bssid
    }
  1. 没找到BSSID 推导 Mac的方式。 暂未找到相关方法。

你可能感兴趣的:(iOS - 获取WIFI的BSSID)