I'm developing an app that has to connect with a BLE device in the foreground. Then, when the user starts a process and locks the phone, the app has to send a command to the BLE device in the background every ~30-180 seconds. It has to do this for hours (~8 hours). It's a very simple process that shouldn't drain the battery or require a lot of memory. Is it possible to do this without the app getting terminated by the system? Any help would be really appreciated!
Communicating with BLE device in background for a long time
How often do you advertise?
What is the Connection Interval of your BLE device?
What is the Supervisory Timeout of your BLE device?
Do you use Indications or Notifications?
Did you enable the background mode for connecting to BLE accessories?
If you require your device to STAY connected, good luck. What if the user sets down their phone and walks away?
If you are okay with a connection loss in the background followed by a reconnection, then you're in business. I don't see why you'd not be able to do this with the API.
Note: If your app uses the Data Protection capability feature (file system locked/encrypted when phone is locked) you may need to add an ignore flag on the files you would presumably be interacting with during background BLE connection.
Implementation advice would be
When device disconnects with error, issue reconnect.
Opt-in on the State Restoration (init with options key).
State Restore Key:
https://developer.apple.com/documentation/corebluetooth/cbcentralmanageroptionrestoreidentifierkey
Info on restore conditions:
https://developer.apple.com/library/archive/qa/qa1962/_index.html
General Bluetooth background guide:
https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html