【问题标题】:CoreBluetooth is disconnecting from unused peripherals due to an API Misuse由于 API 滥用,CoreBluetooth 与未使用的外围设备断开连接
【发布时间】:2017-02-28 00:58:07
【问题描述】:

我正在尝试使用 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

为什么会这样?

【问题讨论】:

    标签: ios swift core-bluetooth cbperipheralmanager


    【解决方案1】:

    不知道为什么会这样,但我发现如果我将外围设备委托分配给自己,并将外围设备添加到数组中之前我连接到它。

    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)
    
    }
    

    【讨论】:

    • 您需要保留对 CBPeripheral 的引用,否则它们将被释放。这不是关于“销售代表”,而是更多关于self.remotePeripheral.append(peripheral)
    • 我会补充一点:stackoverflow.com/questions/26377470/…(它在 Objective-C 中,但情况是一样的:CoreBluetooth 不保留对参数的强引用,所以他们可能会发布得太早)
    • 经过 8 小时的调试、搜索 SO + 反复试验后救了我!谢谢
    【解决方案2】:

    我看到了这个错误,对于那些也面临同样问题的人,我的建议是我没有将CBPeripheral 设备存储在我的助手类中。这似乎没有必要做,但出于某种原因,我相信它需要在内部受到限制。 好吧,这就是我所做的:-

        class BLEHelper: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate{
            
        @Published var pairedDevice:CBPeripheral?=nil
        ...
    

    然后在您的didDiscover 函数中:

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    pairedDevice=peripheral
    peripheral.delegate=self
    myCentral.connect(peripheral, options:nil)
    myCentral.stopScan()
    

    }

    这一行可以解决问题:- pairedDevice=peripheral

    【讨论】:

      猜你喜欢
      • 2016-11-08
      • 2016-04-22
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多