Reputation: 41
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 ?
Upvotes: 4
Views: 7947
Reputation: 53830
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.
Upvotes: 10