swift - 附近设备的 iOS 蓝牙 mac 地址

标签 swift xcode core-bluetooth mac-address

我想通过蓝牙获取附近设备的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/

相关文章:

ios - 未在不同线程上调用委托(delegate)

swift - iOS 中的排序集

ios - 应用程序在安装时不询问位置服务权限 WhenInUse?

ios - 命令因信号 : Segmentation fault: 11 while archiving 而失败

ios - Interface Builder View 未显示

ios - 是否可以在iOS中检查用户是否给予(评分和评论)

ios - 使用 Core Bluetooth 从 NSArray 获取写入请求值

ios - 无法从 pod 库加载资源

swift - 使结构可哈希?

iOS CoreBluetooth 不扫描 iPad Air 中的服务