How to Disconnect CoreBluetooth Peripheral cleanly in Swift
byTo disconnect a connected peripheral in CoreBluetooth we call method cancelPeripheralConnection
centralManager.cancelPeripheralConnection(peripheral)
This did disconnect peripheral in CoreBluetooth. In my program there is code that caches old discovered peripherals for certain period. It does go through cached peripherals to search for their services. What I found was that even if peripheral was disconnected code was somehow discovering the services and characteristics. There is a segment of code which is written to trigger certain behaviour when characteristics of peripheral is discovered. I did not want this for disconnected peripherals. Problem is not an issue for there are very simple solution for this scenario like checking the state of peripherals before trigger and etc.
I am emphasizing that Disconnected peripheral’s services still can be discovered in CoreBluetooth. Which is not an issue. I had assumed that when peripheral gets disconnected it removes its services and characteristics.
These methods do get called up even if peripheral is no more connected.
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)