4

I am trying to run my app in background when the BLE is disconnected.But system will kill the app after a period of time.So I read apple's document --《Core Bluetooth Programming Guide》。The last I found the following passage at “Core Bluetooth Background Processing for iOS Apps”。

“Performing Long-Term Actions in the Background

Some apps may need to use the Core Bluetooth framework to perform long-term actions in the background. As an example, imagine you are developing a home security app for an iOS device that communicates with a door lock (equipped with Bluetooth low energy technology). The app and the lock interact to automatically lock the door when the user leaves home and unlock the door when the user returns—all while the app is in the background. When the user leaves home, the iOS device may eventually become out of range of the lock, causing the connection to the lock to be lost. At this point, the app can simply call the connectPeripheral:options: method of the CBCentralManager class, and because connection requests do not time out, the iOS device will reconnect when the user returns home. Now imagine that the user is away from home for a few days. If the app is terminated by the system while the user is away, the app will not be able to reconnect to the lock when the user returns home, and the user may not be able to unlock the door. For apps like these, it is critical to be able to continue using Core Bluetooth to perform long-term actions, such as monitoring active and pending connections.”

From this passage we know that “monitoring active and pending connections can solve the problem But I can't find any way to implement monitoring active and pending connections at

Core Bluetooth Framework Reference

How can I implement that keep my app running in the background when the BLE is disconnected?

Does anyone have any idea ?

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
cocode
  • 41
  • 1
  • 2

1 Answers1

10

If you register your app using bluetooth-central Background Execution Mode, your app will be fired up if it's not running, and will get it's delegate methods called for handling discovery and connections, such as centralManager:didDiscoverPeripheral, from which you can call connectPeripheral:options:.

It's true that the system may kill your app in the background, but it will relaunch it for these events.

The only time that the system won't relaunch your app for background BLE execution is if the user force killed the app on the previous launch. The system will remember this and won't relaunch the app again until the user manually relaunches it the first time.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
  • 2
    The system will relaunch the app only in case that the user did not close the app manually (by double clicking the home button and swiping it out). In this case, the system won't relaunch your app giving it a chance to reconnect (because the system assumes that the user doesn't want this app to run in this case). – Despotovic Apr 15 '16 at 11:34
  • "It's true that the system may kill your app in the background, but it will relaunch it for these events." This in fact is FALSE. I have tested this case. Can you provide sources to this statement? – BangOperator Feb 23 '17 at 18:44
  • @BangOperator, what part did you test and is false? That the system may kill it in the background? According to Apple: "Upon being woken up, an app has around 10 seconds to complete a task. Ideally, it should complete the task as fast as possible and allow itself to be suspended again. Apps that spend too much time executing in the background can be throttled back by the system or killed." and "...the system wakes up your app when any of the CBCentralManagerDelegate or CBPeripheralDelegate delegate methods are invoked, allowing your app to handle important central role events..." – Marcus Adams Feb 23 '17 at 21:15
  • Apple's [Core Bluetooth Background Processing for iOS Apps](https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html) – Marcus Adams Feb 23 '17 at 21:20
  • @MarcusAdams with background execution mode, the app will not be relaunched. We need restoration for that. Restoration is no where mentioned in answer, but relaunching without restoration cannot happen. – BangOperator Feb 24 '17 at 04:36
  • And "it will relaunch it for these events" can also be expected at some case. In rest there might be an BT EVENT WITH app not launching. – BangOperator Feb 24 '17 at 04:38
  • 1
    @Despotovic is you comments documented in Apple documentation. – Rohit Pradhan Sep 12 '17 at 12:48
  • You only need "restoration" if you're trying to restore a connection. The discovery events occur without that. – Marcus Adams Sep 12 '17 at 16:16
  • 1
    @RohitKP Under [State Preservation and Restoration](https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW5), its stated that "when the system is about to terminate your app to free up memory...". So I cant find direct documented qoute about this claim. This is the best I could find in docs for now. But I can tell you that this fact is confirmed by my tests... – Despotovic Sep 13 '17 at 11:52
  • @MarcusAdams Will the app be launched in the background if the user toggles his bluetooth and central's state changes? Complete question : https://stackoverflow.com/questions/48030480/turning-bluetooth-radio-back-on-after-the-app-is-suspended-doesnt-call-centralm – Saleh Jan 03 '18 at 19:22
  • @MarcusAdams is there any Background Execution mode that can launch app from the terminated state by a user after Force Kill? – khunshan Mar 08 '18 at 11:58
  • @khunshan, region monitoring (geofencing) will. Nothing Bluetooth related will. – Marcus Adams Mar 08 '18 at 13:55
  • @MarcusAdams thanks. I am able to do it with "Location" background mode. Do you think can I by using this or anyway launch the forced close app instantly in the background? I know that's not how iOS works but still if anyway possible. – khunshan Mar 09 '18 at 10:26