What causes CBCentralManagerStateUnknown in iOS? What causes CBCentralManagerStateUnknown in iOS? objective-c objective-c

What causes CBCentralManagerStateUnknown in iOS?


I know why delegate is never called. Because the object is deleted from memory. Just make a strong property

@property (strong, nonatomic) DiscoverBluetoothDevices *btDevices;

And in init

@implementation DiscoverBluetoothDevices- (id) init{    self = [super init];    if(self) {        centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];        [centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}];    }    return self;}

And now delegate is called properly.


CBCentralManagerStateUnknown simply means iOS has started the BLE process, but has not completed initialization. Give it a moment, and the state will change.

In general, you will "give it a moment" by detecting a state chang in a CBCentralManagerDelegate delegate handler rather than looking at it right after the initialization call. You will implement

- (void) centralManagerDidUpdateState: (CBCentralManager *) central;

There are some good examples that show this, such as Apple's heart rate monitor.


If a central's state goes to CBCentralManagerStateUnsupported (while Bluetooth Low Energy is supported by the device) it most likely means the app has done something bad with CoreBluetooth.

Check the iOS Bluetooth Diagnostic Logging logs.

For example, if you do this...

_cm1 = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{ CBCentralManagerOptionRestoreIdentifierKey: @"not_unique" }]; _cm2 = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{ CBCentralManagerOptionRestoreIdentifierKey: @"not_unique" }];

... the second central's state will go to CBCentralManagerStateUnsupported.