使用CoreBluetooth
时,我发现没有调用CBCentralManagerDelegate
函数 centralManager(_:didConnect:)的情况,当发生这种情况时,我还发现 centralManager(_:didDiscover:advertisementData:rssi:)函数,其中 peripheral.state 会先被 .disconnected 调用,然后再 .connecting 之后。
是事情应该发生的方式还是表明我的代码中可能存在错误? peripheral.state 是 .connecting 时该怎么办?不做任何事情就退出该功能吗?
以下是一些相关代码:
func centralManager(_ central: CBCentralManager,
didDiscover peripheral: CBPeripheral,
advertisementData: [String : Any],
rssi RSSI: NSNumber) {
print(#function)
if peripheral.state != .disconnected {
// This is not the case the first time this function is called.
// It is not clear what to do here.
print("peripheral not disconnected")
return
}
peripheral.delegate = self
if cbPerifList == nil {cbPerifList = [CBPeripheral]()}
cbPerifList!.index(where:{$0.identifier==peripheral.identifier}) == nil {
cbPerifList.append(peripheral)
}
central.connect(peripheral, options: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print(#function)
peripheral.discoverServices(listeningServices)
}