CoreBluetooth配对反馈/回调 [英] CoreBluetooth pairing feedback / callback

查看:112
本文介绍了CoreBluetooth配对反馈/回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感觉好像我在这里丢失了什么,但是如何获得密码保护受保护的外围设备配对失败还是成功的反馈?

It feels like I'm missing something here, but how can I get feedback on whether pairing a passcode protected peripheral failed or succeeded?

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

When I to connect a peripheral which is password protected the password UIAlertView pops up and the peripheral connects (didConnectPeripheral is called) and disconnects (didDisconnectPeripheral) immediately.

[bluetoothManager connectPeripheral:peripheral options:nil];

现在无论我输入正确的密码,输入错误的密码还是直接按Cancel键,在所有情况下我都不会没有收到来自CoreBluetooth委托方法的任何反馈。

Now whether I enter the correct passcode, the wrong passcode or simply press cancel: on all occasions I don't receive any feedback from the CoreBluetooth delegate methods.

问题是如何获得有关此过程的反馈?

The question is how can I get feedback on this process?

推荐答案

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

Faced the same issue after years of the question being posted here. Surprisingly Apple does not provide any callbacks on whether the pairing was successful. However the following steps can be used to conclude the same:


  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 状态:

  1. Scan for devices when the CBCentralManager is in .poweredOn state:



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

  1. For one of these characteristics which has the .notify property, write some data with write type as .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?) { }

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

If the pairing was not successful because of invalid passcode entry or cancellation by the user, you will get an error saying "Authentication is insufficient"

否则到该特征将成功,并且错误对象将为nil。

Else the write to the characteristic will be successful and error object will be nil.

这篇关于CoreBluetooth配对反馈/回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆