Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS CoreBluetooth: active connection to the same target peripheral in app and widget simultaneously?

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.

like image 859
brigadir Avatar asked Oct 18 '22 14:10

brigadir


1 Answers

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.

like image 87
2 revs Avatar answered Oct 21 '22 07:10

2 revs