简体   繁体   中英

How can I detect the state of the bluetooth icon in the status bar of an iPhone?

As the question states, is there a way to query the status bar for its current icons, and which png they're using? If I could do so, I'd be able to test if BlueTooth was on, and if it had a device connected. I want to note, this is different than using the EAAccessory, which is a different connection. In this case, I'm just trying to establish if there's a headset connected, regardless of EAAccessory state.

You can go the Private API route but this will most likely get your app rejected. I have had some Private API sneak past Apple before, but rejected in an update, so be mindful of that.

CoreBluetooth

Contrary to popular belief, this is possible. You can use the CoreBluetooth library. I'm sure that if your app does not demonstrate a use for Bluetooth peripherals, it could be rejected.

However, here is how I detect the Bluetooth state in one of my apps that does use Bluetooth peripherals. Note that this is not the best way to determine the Bluetooth state, but may work for someone.

- (BOOL)isBluetoothEnabled
{
    if ( ! _centralManager ) {
           _centralManager = [[CBCentralManager alloc]
                              initWithDelegate:self
                              queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                              options:@{
                                        CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:NO]
                                        }];
    }
    switch ( _centralManager.state )
    {
        case CBCentralManagerStateUnknown: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateResetting: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateUnsupported: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateUnauthorized: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStatePoweredOff: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStatePoweredOn: {
            _bluetoothEnabled = YES;
            break;
        }
        default: { // Unknown
            _bluetoothEnabled = NO;
            break;
        }
    }
    return _bluetoothEnabled;
}

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