【发布时间】:2020-01-29 20:19:39
【问题描述】:
我想用蓝牙获取附近设备的mac地址 这就是我想要的格式。
FE:4F:AD:37:67:5D
我在代理内部有完全不同格式的 mac 地址
5962C58F-BAD1-65D4-DCAC-06BBB06307C6
这些是我正在使用的代表
CBCentralManagerDelegate
func centralManager(_ central: CBCentralManager,didDiscover 外围设备:CBPeripheral, 广告数据:[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")
}
}
【问题讨论】:
-
在 iOS 上,我们没有真正的“MAC 地址”,它们由操作系统“转换”以避免过多的跟踪。在您配对之前(不仅是“连接”,而且是真正的配对),它们可能每 15 分钟更改一次。你看过
identifier的文档吗?这是一个UUID,不是Mac地址,长度/格式不一样... -
同意。这是故意不可能的。你想解决什么问题?
-
我的后端有设备的 mac 地址。我想确认该设备(具有该 mac 地址)是否存在于蓝牙范围内。我正在扫描所有设备并通过 mac 地址进行确认。
标签: swift xcode core-bluetooth mac-address