iOS CoreBluetooth passively check if Bluetooth is enabled without prompting user to turn Bluetooth on
Asked Answered
C

2

14

The scenario is as follows. I have an already deployed app being used by people. I am considering implementing some experimental Bluetooth Low Energy features, but before I do, I want to conduct an "in the wild" survey of the number of users who already have Bluetooth turned on/leave Bluetooth on all the time. So, I would just like to check this in the background and send back to a server statistics on number of users with Bluetooth already on, without ever actually using Bluetooth communications.

I have successfully instantiated a CBCentralManager in my app and can retrieve the Bluetooth status both immediately after instantiation and when the Bluetooth state updates via centralManagerDidUpdateState:. So that is all good. The problem I am having is that if the state is CBCentralManagerStatePoweredOff, then the device brings up an alert prompting the user to "Turn On Bluetooth to Allow [app name] to Connect to Accessories". It is this prompt I am trying to avoid. At this stage I just wish to survey the number of users who leave Bluetooth On - I don't wish to use the Bluetooth connection.

So, is there a way to check the Bluetooth status without prompting the user if you don't intend on using the Bluetooth connection?

Cainozoic answered 7/5, 2013 at 6:48 Comment(0)
M
12

iOS 7 has a new options parameter in the create of CBCentralManager to turn this off, named CBCentralManagerOptionShowPowerAlertKey

I'd love a solution for earlier iOS.

Migdaliamigeon answered 14/10, 2013 at 19:10 Comment(3)
Nice find. Are you referring to CBCentralManagerOptionShowPowerAlertKey? If you decide to expand your answer to specify which call this is passed to/link to further info I'll mark this the accepted answer. Otherwise I'll do an edit on this answer when I get a chance to look at it again in more detail.Cainozoic
Yes, use CBCentralManagerOptionShowPowerAlertKey, try myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:NO]}];Destructible
Possible SWIFT 5 answer https://mcmap.net/q/261425/-what-causes-this-ios-permission-prompt-for-quot-use-bluetooth-for-new-connections-quotBuchan
L
12

You can use the following when you initialise the CBCentralManager.

NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @NO};
self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];

I may have not explained it correctly, please right into the comment if there's any concern.

Lyman answered 17/5, 2014 at 8:4 Comment(4)
@Maximilian are you sure? It works on my iOS 8.4.1 iPad Mini 2.Tableware
@po5i var options = [CBCentralManagerOptionShowPowerAlertKey: false] self.manager = CBCentralManager(delegate: self, queue: nil, options: options)Cosetta
in iOS13 this now prompts users permission to access bluetooth.Harbour
Possible SWIFT 5 answer https://mcmap.net/q/261425/-what-causes-this-ios-permission-prompt-for-quot-use-bluetooth-for-new-connections-quotBuchan

© 2022 - 2024 — McMap. All rights reserved.