Is there any why to detect CBPeripheral object state change from "Connected" to "Disconnected" in iOS.
For detecting whether a Core Bluetooth Peripheral
object is disconnected use a centralManager(_:didDisconnectPeripheral:error:)
instance method which tells the delegate that the central manager disconnected from a peripheral:
func centralManager(_ central: CBCentralManager,
didDisconnectPeripheral peripheral: CBPeripheral,
error: Error?) {
print(peripheral.state) // CBPeripheralState
}
Do not forget to set a delegate
instance property that is the delegate object specified to receive peripheral events from a CBPeripheralDelegate
protocol that provides updates on the use of a peripheral’s services:
weak var delegate: CBPeripheralDelegate? { get set }
The CBPeripheralState
has three cases:
enum CBPeripheralState : Int {
case disconnected = 0
case connecting = 1
case connected = 2
}