CBPeripheralManager behaving badly

Given the following code:


func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
    ...
}

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager){
    ...
}


Once we initialize an instance of CBPeripheralManager, we sometimes observe the peripheralManager(_:didReceiveWrite:) delegate function getting called before the state of peripheral manager is updated to .poweredOn. Since the state is not .poweredOn, we cannot respond to the write message as that results in an API MISUSE warning.


We have occasionally seen this happen on iOS 12, but are seeing it happen more on iOS 13.

Replies

Where did you add the service (i.e.: CBMutableService) to peripheral manager?


Perhaps you should add it after you confirmed the peripheral manager's state is powered on