iOS Detect Bluetooth connection/disconnection
Asked Answered
H

1

10

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 ?

Heda answered 16/1, 2018 at 15:31 Comment(0)
I
9

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.

Impressment answered 16/1, 2018 at 19:55 Comment(8)
Thanks for your detailed answer, I tried with the Audio route change but unfortunately, this doesn't work in Background if my app isn't playing sound (which is not the case)... It seems it will be difficult to do with all those limitations...Heda
Yes, it won't work in the background; if you aren't the active sound app then iOS considers that you don't need any information about audio routes.Impressment
Different approaches. Apple considers the user experience to be most important. Background execution is limited because it can negatively impact battery life and overall system performance. If your app isn't actively using an audio peripheral then you don't need to know about connection/disconnections.Impressment
Yes but in my case, what I want to do is detect the bluetooth connection/disconnection to know when the user enters/leaves his car, it's a big improvement in the UX for my app. It should be possible to do it but I just can't find how to do it with all those limitations...Heda
It's not possible. Ios will learn if the user typically uses your app when they connect to a specific peripheral such as a car audio system and offer your app as a Siri suggestionImpressment
Is there any improvement regarding this topic from apple side with iOS 11 / iOS 12? I'm currently also struggling on iOS side, to identify, if an user connected or disconnected to a specific bluetooth connection.Baggs
Nothing has changed. I wouldn’t expect it to change in the futureImpressment
@AlexWoe89 This might help https://mcmap.net/q/1166183/-headphones-plugin-out-detection-in-swiftCathcart

© 2022 - 2024 — McMap. All rights reserved.