Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Bluetooth is enabled via iOS External Accessory Framework?

I am writing an iOS-based program that interacts with a Bluetooth device via the External Accessory Framework. I would like to determine ahead of time if Bluetooth is even enabled before attempting to connect. Unfortunately, I don't see anything in the External Accessory Framework documentation that allows me to do this.

After checking the docs for the [EAAccessoryManager][1], the closest I can find is to check the [connectedAccessories][1] list to see if any devices are currently connected. However, this doesn't directly indicate the Bluetooth adapter's status.

There are plenty of examples on SO pertaining to Core Bluetooth and Bluetooth LE. I'm specifically looking for a solution related to the External Accessory Framework.

like image 737
gonzobrains Avatar asked Nov 07 '25 17:11

gonzobrains


1 Answers

This is not possible with the ExternalAccessory framework. You should use CoreBluetooth, which can give you the information you need on devices that have BLE hardware, i.e. everything released after 2011. The fact that you are using ExternalAccessory for communication with your device does not prevent you from also using CoreBluetooth just for the purpose of knowing whether Bluetooth is turned on.

For older devices there is no way to get this info with public APIs, however if you are not intending to publish your app on the App Store you can use the private BluetoothManager framework.

To get the info with CoreBluetooth you will need to instanciate a CBCentralManager instance, for example like this :

centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];

and then implement the following delegate method to get the relevant info :

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    BOOL bleAvailable = central.state != CBCentralManagerStateUnsupported;
    if (bleAvailable) {
        BOOL bluetoothTurnedOn = central.state != CBCentralManagerStatePoweredOff;
        // Do something with the info
    }
    else {
        // Out of luck... We won't be able to determine whether BT is on or off
    }
}
like image 63
Samuel Peter Avatar answered Nov 09 '25 09:11

Samuel Peter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!