问题1:API MISUSE: <private> can only accept this command while in the powered on state
解决办法:
//CBCentralManagerDelegate
centralManagerDidUpdateState(_ central: CBCentralManager)
该方法执行后central.state == .poweredOn
再去scan
问题2:[CoreBluetooth] API MISUSE: Cancelling connection for unused peripheral
解决办法:用成员变量保存引用
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if peripheral.name == BLE_SERVICE_NAME {
print(peripheral.name)
self.peripheral = peripheral
centralManager.connect(self.peripheral!, options: [CBConnectPeripheralOptionNotifyOnDisconnectionKey : true])
}
}
网友评论