Periodic BLE background fetch

I have a BLE device that logs data, and I want to periodically connect and download the log. Should I be using the

fetch and bluetooth-central
key on
UIBackgroundModes
and then issue a connectPeripheral:options: in application:performFetchWithCompletionHandler:? Or is there a better way to periodically connect to a BLE device in the background?


Thanks

-Stephen

The proper way to implement periodic connect and download data from a BLE peripheral is to give the peripheral the control.

The peripheral can periodically notify your app through a characteristic and then a download can ensue.


I understand you would want this to work while your app is inactive the background. There is no time based way for your app to wake up and connect to the peripheral. This procedure must be initiated by the peripheral.


Gualtier

That's too bad, it seems very inefficient to keep the connection constantly alive for 24 hours when I know it will only be used for a few seconds to download a log.


Would it be advisable to send local notifications and require the user to open up the app to start the connection and download process?

Accepted Answer

Actually you would not need to keep the connection alive all the time. If all you need is a few seconds within a 24 hour period, you can break the connection and have your peripheral sleep until it is time to send the log data.


Then your peripheral can wake up and start advertising - while your app is pending on a connect request (don't forget to use CoreBluetooth state preservation on the app side). Once the iOS device detects the advertising peripheral, you can then in your app connect, download the data, and signal the peripheral to go to sleep again.


Important things to note:

- you must use CoreBluetooth state preservation/restoration so your app gets launched even if it has been terminated by the OS

- the peripheral must advertise in full power and frequency as it is likely that the iOS device will be in low power passive scan mode at the time the peripheral wakes up


Using local notifications to ask the user to open the app and download your data periodically would work, if your user experience model fits that.

Keep in mind, though, in such an implementation, your peripheral would need to keep advertising all that time as there would be no telling when the app would be launched and the transfer requested.

Great explanations, thanks for your help!

Gualtier, can you describe under what conditions the OS will relaunch the app for CB state restoration?


I would take a guess that under a memory warning if the OS terminated your app in the background this might qualify. How about device reboots either user initiated or an OS update initiated?


Thanks in advance!

Periodic BLE background fetch
 
 
Q