iOS 蓝牙 mac 附近设备的地址

iOS Bluetooth mac address of nearby devices

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

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

我在委托

中有完全不同的mac地址格式

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

这些是我正在使用的代表

CBCentralManagerDelegate

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : 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")
            }
        }

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

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