简体   繁体   中英

Detect to see if Bluetooth on iPad is enabled iOS 7

I'm just trying to detect, when my app is launched on the iPad, if the bluetooth on the device is enabled.

Specifically, I would like to launch the app on my iPad, have the app check to see if bluetooth is enabled on the device in the background and if it is, the app does nothing, but if bluetooth is disabled it triggers an alert that prompts the user to turn bluetooth on. I've done research looking into this but have not been able to find a clear and concise answer to my question. Any help would be much appreciated.

If you instantiate CBCentralManager in your app, ios will automatically prompt user to enable Bluetooth from Settings page.

Add following in your viewDidLoad or some top level function:

_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

You can override 'centralManagerDidUpdateState' to catch the callback:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state == CBCentralManagerStatePoweredOn) {
        //Do what you intend to do
    } else if(central.state == CBCentralManagerStatePoweredOff) {
        //Bluetooth is disabled. ios pops-up an alert automatically
    }
}

The accepted answer needs to be updated slightly for iOS 10.

CBCentralManagerStatePoweredOn and CBCentralManagerStatePoweredOff have been deprecated, and should be replaced by CBManagerStatePoweredOn and CBManagerStatePoweredOff .

Updated code:

- (void)centralManagerDidUpdateState:(CBCentralManager*)aManager
{
    if( aManager.state == CBManagerStatePoweredOn )
    {
        //Do what you intend to do
    }
    else if( aManager.state == CBManagerStatePoweredOff )
    {
        //Bluetooth is disabled. ios pops-up an alert automatically
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM