iOS 附近设备的蓝牙 mac 地址

Tai*_*mal 6 xcode mac-address core-bluetooth swift

我想使用蓝牙获取附近设备的 MAC 地址 这就是我想要获取的格式。

FE:4F:AD:37:67:5D

我在委托中有完全不同格式的 mac 地址

5962C58F-BAD1-65D4-DCAC-06BBB06307C6

这些是我正在使用的代表

CBCentralManagerDelegate

funccentralManager(_central:CBCentralManager,didDiscover外围设备:CBPeripheral,advertisementData:[字符串:Any],rssi RSSI:NSNumber)

这是我的代码

  func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
            if(!peripherals.contains(peripheral)) {
                peripherals.append(peripheral)
            }
            
            let identifier = "\(peripheral.identifier)"
            if let data = identifier.data(using: .utf8) {
                let mac_address = data.hexEncodedString().uppercased()
                let macAddress = mac_address.separate(every: 2, with: ":")
                if let name = peripheral.name {
                    print("\(name) \n\(peripheral.identifier)\nMAC_ADDRESS: \(macAddress)")
                }
                
            }
        }
        
        
        func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
            mainPeripheral = nil
            print("Disconnected" + peripheral.name!)
        }
        
        
        func centralManagerDidUpdateState(_ central: CBCentralManager) {
            print(central.state)
            switch central.state {
            case .unknown:
                print("unknown")
            case .resetting:
                print("resetting")
            case .unsupported:
                print("unsupported")
            case .unauthorized:
                print("unauthorized")
            case .poweredOff:
                print("poweredOff")
                UIView.animate(withDuration: 0.7) {
                    self.lblBluetoothAlert.alpha = 1.0
                }
                stopScanForBLEDevices()
            case .poweredOn:
                UIView.animate(withDuration: 0.7) {
                    self.lblBluetoothAlert.alpha = 0.0
                }
                print("poweredOn")
                scanBLEDevices()
            default:
                print("default")
            }
        }
Run Code Online (Sandbox Code Playgroud)

Pau*_*w11 3

核心蓝牙不提供对外设 MAC 地址的任何访问。

为了实现您在评论中概述的要求,即根据后端数据库中的数据识别外设,您需要外设也通过特征公开其标识符(可能是其 MAC 地址,可能是其他一些唯一标识符) 。然后,您的应用程序可以连接到目标外围设备并询问特征。