I want to keep persistent BLE connection with my peripheral device in central mode in my app and it's widget. So is it possible technically?
The reason is following: when connection has been established in the app, we send it to background, then open today-widget - the widget should continue to operate with connected peripheral device. And wise-versa: if we hide today-widget, launch app - the app should continue to use existing connection.
Yes, you can continue to process BLE events in the background if you enable Bluetooth capability in the background. Simply select your main project file, then select the target, and on the Capabilities tab, turn on Background Modes and enable "Uses Bluetooth LE accessories".
In your main app, you will have a chance to process any BLE events, even while the app is in the background.
One thing to note is that the BLE events initiate the action, so be sure to put the code that you want executed in the background inside a delegate method such as didUpdateValueForCharacteristic
.
Your code will be executed on whichever queue you've specified when you initialized the CBCentralManager
.
The widget and the app can't share a BLE connection. You can communicate between the app and widget with the methods that Apple suggests (NSUserDefaults or key value observing), or, you can create another BLE connection from the widget and communicate via that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With