IOS CoreBluetooth: active connection with the same target peripheral device in the application and widget at the same time?

I want to maintain a permanent BLE connection with my peripheral device in central mode in my application and its widgets. So is it technically possible?

The reason is this: when the connection is established in the application, we send it to the background, and then open today's widget - the widget should continue to work with the connected peripheral device. And vice versa: if we hide today's widget, launch the application - the application should continue to use the existing connection.

+5
source share
2 answers

Yes, you can continue to process BLE events in the background if you enable the Bluetooth function in the background. Just select your main project file, then select the target and the “Features” tab, turn on the background and turn on “Use Bluetooth LE Accessories”.

In the main application, you will have the opportunity to handle any BLE events, even if the application is in the background.

It should be noted that BLE events trigger an action, so be sure to put the code that you want to execute in the background inside the delegate method, for example didUpdateValueForCharacteristic .

Your code will be executed depending on your queue when you initialized the CBCentralManager .

The widget and application cannot use the BLE connection. You can communicate between the application and widgets using the methods that Apple offers (NSUserDefaults or monitoring key values), or you can create another BLE connection from the widget and exchange data through it.

+2
source

Yes it is possible. See retrieveConnectedPeripheralsWithServices: on the CBCentralManager.

Basically, what this method does is it collects all peripheral devices connected through the entire iOS device. Use this when launching a second application on an iOS device.

When the application is in the background, its BLE connections remain alive, so you can hide application 1, start application 2, use this method to determine the peripheral device you are interested in and use it regardless of the first application.

0
source

Source: https://habr.com/ru/post/1243846/


All Articles