ios - CoreBluetooth 配对反馈/回调

标签 ios core-bluetooth cbperipheral

感觉好像我在这里遗漏了一些东西,但我如何才能获得关于配对受密码保护的外围设备是失败还是成功的反馈?

当我连接受密码保护的外围设备时,会弹出密码 UIAlertView,并且外围设备会立即连接(调用 didConnectPeripheral)并断开连接(didDisconnectPeripheral)。

[bluetoothManager connectPeripheral:peripheral options:nil];

现在,无论我输入正确的密码、错误的密码还是简单地按取消:在所有情况下,我都不会收到来自 CoreBluetooth 委托(delegate)方法的任何反馈。

问题是我怎样才能得到关于这个过程的反馈?

最佳答案

在此处发布问题多年后面临同样的问题。令人惊讶的是,Apple 没有提供任何关于配对是否成功的回调。但是,可以使用以下步骤得出相同的结论:

  1. 声明和初始化:
var centralManager: CBCentralManager?
var myPeripheral: CBPeripheral?
var peripheralManager: CBPeripheralManager?

centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
peripheralManager = CBPeripheralManager.init(delegate: self, queue: DispatchQueue.main )

  1. CBCentralManager 处于 .poweredOn 状态时扫描设备:
func centralManagerDidUpdateState(_ central: CBCentralManager) {
   if central.state == .poweredOn {
       centralManager?.scanForPeripherals(withServices: [CBUUID.init(string: "SERVICE-ID")])
   }
}
  1. 识别并连接到感兴趣的设备:
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    //Identify the device to be connected
    if peripheral.name?.hasSuffix("DEVICE-SERIAL-NUMBER") ?? false {
        myPeripheral = peripheral
        peripheral.delegate = self
        centralManager?.connect(myPeripheral!, options: nil)
    }
}
  1. 发现连接设备的服务,然后发现这些服务的特征
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        peripheral.discoverServices([CBUUID.init(string: "SERVICE-ID-STRING")])
    }

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {       
    let services = peripheral.services!
    let charId = CBUUID.init(string: “CHARACTERISTIC-ID”)
    for service in services {
        peripheral.discoverCharacteristics([charId], for: service)
    }
}
  1. 对于具有.notify属性的这些特征之一,写入一些数据,写入类型为.withResponse
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    let value = 1234
    let data = withUnsafeBytes(of: value) { Data($0) }
    for characteristic in service.characteristics!
    {
        if characteristic.properties.contains(.notify) {
            peripheral.setNotifyValue(true, for: characteristic)
            peripheral.writeValue(data, for: characteristic, type: .withResponse)   
        }
    }
}
  1. 检查此写入的响应以确定配对是否成功:
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { }

如果由于密码输入无效或用户取消而导致配对不成功,您将收到一条错误消息“身份验证不足”

否则写入特性将成功,错误对象将为零。

关于ios - CoreBluetooth 配对反馈/回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27836416/

相关文章:

ios - 广告另一个iOS设备后未调用didDiscoverPeripheral

ios - ISO 8601 日期的正确日期格式

ios - 如何禁用 UITextField 以删除自定义编号

ios - Apple 框架 ISA 分类

iphone - CoreBluetooth [警告] 未知错误 : 241

ios - corebluetooth 和 ios 状态

ios - 一台设备的 CBCentral 和 CBPeripheral 标识符是否相同

ios - 通过 iPhone 设备扫描三个具有相同 CBUUID 但 MAC 地址不同的相同 BLE 传感器

ios - 从远程位置加载 CGpath/UIBezierPath

ios - 核心蓝牙 : Can you connect to a peripheral device that is not advertising