我正在尝试从具有CoreBluetooth的iPad连接到MacBook Pro。

这是我的CBCentralManagerDelegate代表团:

extension MasterViewController: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            print("Scanning for peripherals")
            central.scanForPeripherals(withServices: nil, options: nil)
            Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.stopScan), userInfo: nil, repeats: true)
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        print("Did discover peripheral", peripheral)

        central.connect(peripheral, options: nil)

    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Did connect to ", peripheral)

        peripheral.delegate = self
        self.remotePeripheral.append(peripheral)
    }

    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {}
}

但是当我扫描时,我在日志中收到此错误:
<Error>: [CoreBluetooth] API MISUSE: Cancelling connection for unused peripheral

为什么会这样?

最佳答案

不知道为什么这样做有效,但是我发现如果我将外围设备委托分配给self,并在连接到外围设备之前将外围设备添加到阵列中,那么它会工作。

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    print("Did discover peripheral", peripheral)

    peripheral.delegate = self
    self.remotePeripheral.append(peripheral)

    central.connect(peripheral, options: nil)

}

关于ios - 由于API滥用,CoreBluetooth正在与未使用的外围设备断开连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40113545/

10-09 09:15