Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - check if bluetooth is on without system alert popup to user

This code allows to determine current bluetooth status:

CBCentralManager* testBluetooth = [[CBCentralManager alloc] initWithDelegate:nil queue: nil];


switch ([testBluetooth state]) {....}

But, when [[CBCentralManager alloc] init...] happens, system popups an alert to user, if bluetooth is off.

Is there any way to check bluetooth status without disturbing my users?

like image 408
Oleg Shanyuk Avatar asked Sep 21 '12 15:09

Oleg Shanyuk


2 Answers

I got the following response from an apple developer : In iOS7, the CBCentralManagerOptionShowPowerAlertKey option lets you disable this alert.

If you havea a CBCentralManager when you initialise it, you can use the method initWithDelegate:queue:options

Example:

In my .h file i have a CBCentralManager * manager

In .m file :

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerOptionShowPowerAlertKey, nil];

_manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];

[_manager scanForPeripheralsWithServices:nil options:nil];

With this code the warning no longer appears, I hope that helps !

like image 151
Ali Abbas Avatar answered Sep 28 '22 03:09

Ali Abbas


In swift you can do write these two lines in your app delegate inside the func: didFinishLaunchingWithOptions launchOptions

    self.bCentralManger = CBCentralManager(delegate: self, queue: dispatch_get_main_queue(), options: [CBCentralManagerOptionShowPowerAlertKey: false])
    self.bCentralManger.scanForPeripheralsWithServices(nil, options: nil)

where your bCentralManger should be declared as :

private var bCentralManger: CBCentralManager!

like image 32
Alex Zanfir Avatar answered Sep 28 '22 05:09

Alex Zanfir