Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes CBCentralManagerStateUnknown in iOS?

Why do I get CBCentralManagerStateUnknown on an iPad 2 when using this simple code?

- (BOOL)viewDidLoad {

    bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

    if ([manager state] == CBCentralManagerStatePoweredOff) NSLog(@"CBCentralManagerStatePoweredOff");
    if ([manager state] == CBCentralManagerStatePoweredOn) NSLog(@"CBCentralManagerStatePoweredOn");
    if ([manager state] == CBCentralManagerStateResetting) NSLog(@"CBCentralManagerStateResetting");
    if ([manager state] == CBCentralManagerStateUnauthorized) NSLog(@"CBCentralManagerStateUnauthorized");
    if ([manager state] == CBCentralManagerStateUnknown) NSLog(@"CBCentralManagerStateUnknown");
    if ([manager state] == CBCentralManagerStateUnsupported) NSLog(@"CBCentralManagerStateUnsupported");

}

I cannot figure out what CBCentralManagerStateUnknown means. What do I do? The Apple docs just say:

State unknown, update imminent.

I get this response with a Bluetooth device connected, and also when Bluetooth is off. If I try to run something like [manager retrieveConnectedPeripherals], I also get this message in the console:

CoreBluetooth[WARNING] <CBConcreteCentralManager: ...> is not powered on
like image 442
woz Avatar asked Sep 20 '12 18:09

woz


1 Answers

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.

like image 63
Bartosz Hernas Avatar answered Nov 03 '22 19:11

Bartosz Hernas