4ntoine
4ntoine

Reputation: 20410

CBCentralManager is not powered on

_manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[NSThread sleepForTimeInterval:10]; // waiting for centralManagerDidUpdateState invocation
[_manager scanForPeripheralsWithServices:@[ _serviceUUID ] options:nil]; // warning here

...

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    NSLog(@"CBCentralManager state is %i", (int)central.state);
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    NSLog(@"Found peripheral: %@", peripheral);
    ...
}

For some reason delegate centralManagerDidUpdateState is never invoked and i'm getting warning:

2015-04-04 12:59:20.850 xctest[30276:303] CoreBluetooth[WARNING] is not powered on

while starting discovering for peripherals. AFAIK it should work on OSX (I'm on MBA 2013 and OSX Maverics and running XCTest). Bluetooth is turned ON and i can run LightBlue app and discover some BLE devices (i'm sure no BLE apps are running while testing my code). So no didDiscoverPeripheral is invoked as expected.

Upvotes: 0

Views: 2387

Answers (2)

jlb
jlb

Reputation: 689

With OS X 10.13.2 the queue argument to CBCentralManager.init() must be non-nil to trigger a callback, e.g. in Swift

manager = CBCentralManager(
  delegate: self,
  queue: DispatchQueue(label: "BT_queue"))

Upvotes: 2

4ntoine
4ntoine

Reputation: 20410

I've tried to use background thread as sleepForTimeInterval blocks main thread and CBCentralManager uses main thread by default:

dispatch_queue_t bt_queue = dispatch_queue_create("BT_queue", 0);
_manager = [[CBCentralManager alloc] initWithDelegate:self queue:bt_queue];

So centralManagerDidUpdateState: is invoked almost immediately and peripheral is discovered with didDiscoverPeripheral:.

Upvotes: 0

Related Questions