Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Detect Bluetooth connection/disconnection

Is it possible to be notified when a Bluetooth Device is connected or disconnected from iOS even when my app is in background ? On Android, I use the ACTION_ACL_CONNECTED and ACTION_ACL_DISCONNECTED events. But I cannot find equivalents for iOS.

I found the CBCentralManager that can be used to monitor Bluetooth events, but my functions aren't called when a bluetooth device is connected/disconnected, only when I enable/disable the bluetooth. Is it an error on my side or is it normal ?

I also found the doc about Audio Route changes, that can also be an idea to detect the bluetooth connections/disconnections. Just check the kind of new route and detect the connected bluetooth devices at that time.

In the doc, I also found NSNotification types like IOBluetoothHostControllerXXX but nothing is explained about them. Did someone already used them ?

Is there something better or am I missing something ?

like image 594
Gp2mv3 Avatar asked Jan 16 '18 15:01

Gp2mv3


People also ask

Why does my iPhone keep disconnecting my Bluetooth devices?

Ensure that Bluetooth is enabled on your iPhone (Settings > Bluetooth). Also, ensure that your Bluetooth accessory is turned on and in pairing mode. You may need to review your Bluetooth accessory documentation for help if needed. Ensure that Airplane Mode is not enabled.

Why do I keep getting disconnected from Bluetooth?

There may be too many apps running in the background of the device the Bluetooth is attempting to pair with. Certain applications cause interference with the connection, and some devices are limited in the number of applications that can run concurrently. If in doubt, check with the headset manufacturer.

Can Bluetooth be disconnected?

Select the device which you want to disconnect from the list. In the device dialog box, switch the Connection switch to off, or to remove the device from the Devices list, click Remove Device.


1 Answers

You cannot receive notifications about the connection and disconnection of Bluetooth peripherals generally. You can get connection and disconnection events for BLE peripherals that your app connects to.

For example, if your app initiates a connection to a heart rate sensor then you will get a call to your CBCentralManagerDelegate connection function when the connection succeeds. If that device subsequently is switched off or goes out of range then you will get a call to the disconnection delegate method.

If some other app initiates and makes the connection then you will not get a callback.

As you mentioned, you can monitor audio route changes to infer that a Bluetooth audio device has been connected/disconnected, but this will also fire when headphones are plugged in.

like image 133
Paulw11 Avatar answered Nov 16 '22 03:11

Paulw11