蓝牙开发遇到的问题

问题1:API MISUSE: 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])
        }
    }

未完待续...

你可能感兴趣的:(蓝牙开发遇到的问题)