Search code examples
iosbluetooth-lowenergybackground-process

Unable to make Background API call in IOS at regular intervals to push data received in app from a BLE device


I have a weighing scale from Acaia(Peripheral) that emits weight data at 5 points/sec. I used Acaias iOS SDK to make an app that can connect to it to receive the data points.

I want to make the connection alive even in the background state so I have enabled the Background State mode in Capabilities for Bluetooth. Now the App keeps receiving data using NSNotifications.

Requirement and my work

I have to push this data to our servers for that I am storing the Data in an array and at every 10 seconds, I am making an API POST call to push the content available in array to our servers, and then I am resetting the array.

  1. Its working as expected in Foreground state of app.
  2. The API logs aren't regular when the screen is locked or the app is in background state which means data is not getting pushed at regular intervals.
  3. while bringing the app from background to foreground lets say after 30 min, the app freezes and the connection is lost.

I have read Apple developer guide which says its possible to keep the connection alive with a Peripheral device permanently in background if data points keep coming. Now how do I ensure that I don't lose those weights data? Is it possible to push all these in the server, maybe in Batch API Call?

Ref:

https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

https://acaia.co/blogs/news/new-ios-sdk-api-release

Sending BLE device data to server in background


Solution

  • Background processing is possible in IOS. But it should do only the work for which it is held on in background state from suspended state. Also, what i have found is that BLE peripheral devices that constantly keep pushing data to IOS apps prevent app from getting in suspended state, And thus its possible for app to be in background forever, Unless IOS tries to kill it for memory and other issues.

    1. There is option for making App run in Background State in the Capabilities section.
    2. Also ensure that proper Peripheral Delegate methods are handled.
    3. And API calls are made in Batches. I would rather save data captured from BLE in local storage in background and whenever app comes in foreground and is connected to internet, will give option to user to push to server.