How can I reconnect to device after disconnecting in Core Bluetooth

送分小仙女□ 提交于 2019-12-03 07:54:58

问题


In Core Bluetooth, after connecting to a device, I turn off the device and the device is disconnected. But when I turn on the device again, there is no didDiscoverPeripheral called again. How can I reconnect to the device again?


回答1:


When you disconnect a device with cancelPeripheralConnection the didDisconnectPeripheral delegate method will be invoked. However from iOS 6.0 the device remains connected for about 40-50 seconds (or more), so no didDiscoverPeripheral will be invoked in that timeframe. If you want to "discover" it again just call the retrieveConnectedPeripherals method and you will get the reference in didRetrieveConnectedPeripherals.

However, the best solution is to save the device's UUID and use that to reconnect with the retrievePeripherals method. This will invoke didRetrievePeripherals and you can reconnect with connectPeripheral. This is the fastest way to reconnect to a device, no scanning is required in this case.




回答2:


When you do a scan with scanForPeripheralsWithServices, it will normally only notify you once for a particular device address. You can change this to report duplicates by specifying the option CBCentralManagerScanOptionAllowDuplicatesKey. Or you can have your app detect that the other device disconnected using a timeout, and restart your scan.




回答3:


In CoreBluetooth all management is done by application layer. In your case, what I would do it is to listen for disconnect event than in same event, reconnect the peripheral. The connection method is an inexpensive one and assure you to reconnect to your device when it is back in range.

Note that if you explicitly disconnect the device, you received the same disconnect event, but you haven't to call the reconnect method.




回答4:


@Andras gave me the right path, but his answer is not complete anymore since iOS7.

The best way to reconnect to a previous device is to use the retrievePeripherals(withIdentifiers:) method.

This method does not call a delegate, but directly returns you a list of Peripherals, corresponding to the list of UUID passed in parameters.

if let peripheral = self.centralManager.retrievePeripherals(withIdentifiers: [uuid]).first {
    self.peripheral = peripheral // <-- super important
    self.centralManager.connect(peripheral, options: nil)
}

Please check the "super important" line of the above code: The method connect(_:option:) does not retain the peripheral, and if you don't do it yourself, the connection will always fail without any callback since the peripheral objet will be destroyed.



来源:https://stackoverflow.com/questions/13286487/how-can-i-reconnect-to-device-after-disconnecting-in-core-bluetooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!