user3427366
user3427366

Reputation: 3

App communicates using CoreBluetooth in background

I have worked on an app which read heart rate from server kinds of bluetooth low energy HRM(Heart Rate Monitor)by bluetooth notification. And I have added the "App communicates using CoreBluetooth" into the app's "Background Modes". It works fine when the app is in foreground, but when the app is in background, one of the Heart Rate Monitors is always to be disconnect with the app in sever minutes.

So I debugged the app and find that, this kind of HRM is always push notification irregularly.The interval between notifications is from 1 second to 20+ second. I know that when the app is in background, iOS will stop the bluetooth connection if it is too long between bluetooth notifications.

So my question is: Is it possible to keep the connection when the app is in background? Is reconnection the only way to solve my problem?

Upvotes: 0

Views: 882

Answers (1)

Paulw11
Paulw11

Reputation: 115031

Detecting the disconnection and reconnecting to the peripheral will make your app more robust as it will handle the case where the peripheral goes out of range and then returns in addition to the case you are seeing.

When the peripheral is disconnected your centralManager:didDisconnectPeripheral method will be called on your delegate. In this method you can call connectPeripheral:options again to re-establish the connection. iOS will either do this immediately if the device is still in range or automatically later, once the device comes back into range.

Once your centralManager:didConnectPeripheral method is called you can re-establish the characteristic monitoring.

Edit Clarify that you can call connectPeripheral immediately

Upvotes: 1

Related Questions