iOS Background Task does not remain alive if launched when phone is locked (sceen off)

Hi,


I have an iOS application which interacts with BLE devices. The Bluetooth LE accessoriesbackground 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)


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