a.palo
a.palo

Reputation: 288

Detecting CBPeripheral object state change from "Connected" to "Disconnected" in iOS

Is there any why to detect CBPeripheral object state change from "Connected" to "Disconnected" in iOS.

Upvotes: 2

Views: 927

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58053

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
}

Upvotes: 1

Related Questions