Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, October 15, 2016

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

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?

Answer by Gato for iOS - check if bluetooth is on without system alert popup to user


There is currently no way to disable this alert when your application is run on a iOS device which supports Bluetooth LE and where Bluetooth is disabled. It would be an enhancement request to provide a means to disable the alert. So the more requests Apple gets about this enhancement, the better.

Answer by Ali ABBAS for iOS - check if bluetooth is on without system alert popup to user


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 !

Answer by Rushabh for iOS - check if bluetooth is on without system alert popup to user


I have used below code to disable alert for iOS 8 and above version

self.bluetoothManager = [[CBCentralManager alloc]                                        initWithDelegate:self                                         queue:dispatch_get_main_queue()                                         options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}];    [self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];  

Answer by Ant Avison for iOS - check if bluetooth is on without system alert popup to user


I've only tested this on iOS 9 so maybe someone could test this one older OS devices.

We do everything normally except one thing, instead of settings the CBCentralManager Delegate in viewDidLoad we leave this until the moment we need it, in the example case below I call this once my WKWebView has finished loading, and because each page of my web view potentially requires the use of Bluetooth I put this in WKWebView didFinishNavigation.

Swift

var managerBLE: CBCentralManager?    func bluetoothStatus() {      managerBLE = CBCentralManager(delegate: self, queue: nil, options: nil)  }    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {      bluetoothStatus()  }    func centralManagerDidUpdateState(central: CBCentralManager) {      switch managerBLE!.state      {      case CBCentralManagerState.PoweredOff:          print("Powered Off")      case CBCentralManagerState.PoweredOn:          print("Powered On")      case CBCentralManagerState.Unsupported:          print("Unsupported")      case CBCentralManagerState.Resetting:          print("Resetting")          fallthrough      case CBCentralManagerState.Unauthorized:          print("Unauthorized")      case CBCentralManagerState.Unknown:          print("Unknown")      default:          break;      }  }  

The moment that delegate is set within bluetoothStatus() you will see the state change fire.

The notification to turn on Bluetooth only seems to want to be called right at the initial load of your app, doing it this way mean you just get what you want from the centralManagerDidUpdateState

Answer by Alex Zansir for iOS - check if bluetooth is on without system alert popup to user


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!


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.