我想通过蓝牙获取附近设备的mac地址 这就是我想要的格式。
FE:4F:AD:37:67:5D
我在委托(delegate)中有完全不同格式的 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 地址,可能是其他一些唯一标识符)通过一个特性。然后,您的应用可以连接到目标外围设备并查询该特性。
关于swift - 附近设备的 iOS 蓝牙 mac 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58169563/