1

I have an iOS application which interacts with BLE devices. The Bluetooth LE accessories background mode is active so that the application is able to connect and interact with BLE devices even when it's put in the background. When a BLE device is connected, the application executes code in background responding to BLE-related callbacks, but after the BLE device disconnects the application can't run code in background anymore (the application remains alive only for a few seconds after BLE device disconnection).

What I need is a way to perform a finite-length task (1-2 min duration roughly) after the BLE device disconnects.

Reading Apple Documentation I tried using the beginBackgroundTaskWithExpirationHandler: method. Calling this method, the application should request some additional execution time (roughly 3 minutes).

Calling either of these methods delays the suspension of your app temporarily, giving it a little extra time to finish its work.

I tried to register the background task by calling the beginBackgroundTaskWithExpirationHandler responding to the BLE disconnection callback and I found out a strange behavior which depends on wether the phone is locked or not when the beginBackgroundTaskWithExpirationHandler is called:

  • If the phone is not locked (i.e., screen is on, app can be both visibile or in background) when beginBackgroundTaskWithExpirationHandler is called, the background task works as expected and remains alive for roughly 3 minutes when the application is later put in background and the screen is turned off.
  • If the phone is locked (screen off) when beginBackgroundTaskWithExpirationHandler is called, the background task do not remain active in background while the screen is off and it's somehow resumed when the screen is turned on again

(I tried with iPhone 6, iPhone 8 Plus, iPhone 5s all with iOS 12.1.4)

The approach I'm using is very similar to that described here, I also found here another similar problem.

Is that the intended behavior for background tasks? Do the OS suspend a background task if launched when the phone is in locked state?

Is there another way to start a finite-length task, with 1-2 minutes duration, from a callback which is triggered when the app is in background and the phone is locked?

Thank you in advance,

Andrea Gorrieri
  • 1,704
  • 2
  • 22
  • 36
  • 1
    Indended behavior, otherwise you would be able to always start a backgroundTask from another task to keep your app running. You can also check https://developer.apple.com/documentation/uikit/uiapplication/1623029-backgroundtimeremaining how much time you have left on this if you actually start it from the foreground. – MartinM Apr 03 '19 at 09:34
  • Hi MartinM, very interesting observation indeed. However, it's curious that the task remain active if launched when the application is in background with screen on. In that settings I would be able to keep app running in background as you say... – Andrea Gorrieri Apr 03 '19 at 10:19

1 Answers1

0

According to :

application(_:performFetchWithCompletionHandler:)

When this method is called, your app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block. In practice, your app should call the completion handler block as soon as possible after downloading the needed data. If you do not call the completion handler in time, your app is terminated. More importantly, the system uses the elapsed time to calculate power usage and data costs for your app’s background downloads. If your app takes a long time to call the completion handler, it may be given fewer future opportunities to fetch data in the future.

So you have 30 seconds to finish any task you need in background.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
  • Hi Zaid, thank you for your response. This seems to apply to the Background Fetch feature. I do not have currently this background feature active on my application, I'm using the beginBackgroundTaskWithExpirationHandler: instead. Maybe I can try using background fetch to run task in background when BLE peripheral disconnects but unfortunately I need more than 30 seconds :( . – Andrea Gorrieri Apr 03 '19 at 09:25