Search code examples
iosxcodeswift3disconnectcbperipheral

didDisconnectPeripheral not called iOS Swift 3


I'm trying to disconnect from a BLE device. I'm doing cancelPeripheralConnection() and its never calling didDisconnectPeripheral(). I'm setting notify value to one characteristic, so while disconnecting i'm setting it false.

{

BLEOperations.sharedInstance.connectedDevice?.setNotifyValue(false, for: BLEOperations.sharedInstance.peripheralCharacteristics[0])

BLEOperations.sharedInstance.manager.cancelPeripheralConnection(BLEOperations.sharedInstance.connectedDevice!)

}

I'm trying to disconnect connectedPeripheral when user tries to connect to new device. The current device is not disconnected but new device is connected along with old one.

{

BLEOperations.sharedInstance.manager.connect(self.peripheralArray[indexPath.row] as! CBPeripheral , options: nil)

}

What am i doing wrong here? When device connection is lost the method is being called.


Solution

  • It will work fine when the CentralManager has only one instance in the entire flow.

    Instead of using centralmanagerdelegates in viewcontroller, you can create a CentralManager operations class (which is a singleton) and perform operations.

    I did that and its working fine.