Simón Urzúa
Simón Urzúa

Reputation: 1546

Any way to turn on bluetooth programmatically on iOS7+

I hear that iOS7 introduced this functionality with CBCentralManager but can't find how. Is possible? There is another way widthout use GKPeerPickerController?

Upvotes: 6

Views: 19567

Answers (3)

Sahil Mahajan
Sahil Mahajan

Reputation: 3990

You can also set the CBCentralManagerOptionShowPowerAlertKey key while instantiating the CBCentralManager to true. Then iOS will show the alert that "Turn On Bluetooth to Allow "Your App" to connect to Accessories". This alert will take you directly to the Bluetooth Setting page.

SampleCode In swift:

centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: true])

SampleCode In Objective-C:

centralManager = [[CBCentralManager alloc]
                                      initWithDelegate:self 
                                      queue:dispatch_get_main_queue() 
                                      options:@{CBCentralManagerOptionShowPowerAlertKey: @(YES)}];

Happy Coding.. :)

Upvotes: 27

Aman Gangurde
Aman Gangurde

Reputation: 70

use bluetooth Manager framework,

import the bluetoothManager Framework, create a object of bluetooth manager framework, as btManager,

write the following code in Bluetooth On button target

[btManager setPowered:YES];
[btManager setEnabled:YES];

all the best...

Upvotes: -3

Paulw11
Paulw11

Reputation: 114856

No, if the user has turned off Bluetooth all you can do is display an alert or message asking them to turn it on.

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {

    if (central.state == CBCentralManagerStatePoweredOff) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Error" message: @"Please turn on Bluetooth in Settings" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [alert show]; 
    }
 }

Upvotes: 14

Related Questions