API misuse

Our app does what we believe to be the right thing using Bluetooth. We init CBCentralManager and then call scanForPeripherals. The problem is that we're seeing the following message in the console when we make that call: "...[CoreBluetooth] API MISUSE: <CBCentralManager: 0x6060007ed820> can only accept this command while in the powered on state". We know that Apple states, "Before you call

CBCentralManager
methods, the state of the central manager object must be powered on, as indicated by the
poweredOn
constant. This state indicates that the central device (your iPhone or iPad, for instance) supports Bluetooth low energy and that Bluetooth is on and available to use." Testing for status always shows it as "unknown" prior to the call to scan.


So it seems to be a catch-22, where we can't call scanForPeripherals with status as unknown, and we can't determine that status is poweredOn until we scan. Is there some other call that is not an API misuse that will allow status to update? Thanks.

It is possible to check the state of the CBCentralManager object via the .state property.


Before making any calls, you should make sure that the .state property is poweredOn

https://developer.apple.com/reference/corebluetooth/cbcentralmanager


You should also be implementing the centralManagerDidUpdateState(_:) delegate method, which will be called every time there is a state change for the manager object.

https://developer.apple.com/reference/corebluetooth/cbcentralmanagerdelegate/1518888-centralmanagerdidupdatestate

Hi Gualtier - indeed, we do check the state property before calling scan, getting an .unknown state, just as we implement the centralManagerDidUpdateState method. In any case, the state doesn't change until the scan is called. So, how does one affect a change in state without making a central manager call?

Does the centralManagerDidUpdateState(_:) not get called after you initialize the CBManager object?

No, centralManagerDidUpdateState does not get called after init. Only when we call scanForPeripherals. Is this a bug?

Any update on this issue? I also encounted it on iPhone 5s(iOS 11).


The weird thing is this problem resolved itself after i turn on and turn off the Bluetooth on iOS Settings, not the Control Center.

API misuse
 
 
Q