問(wèn)題1:API MISUSE: <private> can only accept this command while in the powered on state
解決辦法:
//CBCentralManagerDelegate
centralManagerDidUpdateState(_ central: CBCentralManager)
該方法執(zhí)行后central.state == .poweredOn
再去scan
問(wèn)題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])
}
}