5

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.

brigadir
  • 6,874
  • 6
  • 46
  • 81

2 Answers2

2

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.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
  • Marcus, thanks for answer. Good approach, but my question is a little bit different: can we use BLE connection that has been established in the app, in today-widget? And vice-versa. Ex.: we launch app, connect BLE, hide app, launch widget and continue the connection there. – brigadir Mar 03 '16 at 13:55
  • 1
    Please refine your answer because that is not what you asked. No, the widget and the app can't share a 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. – Marcus Adams Mar 03 '16 at 13:59
  • Marcus, thanks for explanations and an idea to use BLE in background. I will try it... – brigadir Mar 09 '16 at 10:28
1

Yes this is possible. Take a look at retrieveConnectedPeripheralsWithServices: on the CBCentralManager.

Basically what this method does is it collects all of the peripherals connected across the entire iOS device. Use this when launching your second app on the iOS device.

When an app is in the background, its BLE connections stay alive, so you may hide app 1, launch app 2, use this method to identify the peripheral you're interested in, and use it independently of the first app.

  • So, after `retrieveConnectedPeripheralsWithServices` in second app I should connect retrieved peripheral, as mentioned in reference. In that case does existing connection in the first app stay alive? If yes, how data transfer performs with two simultaneous connections? – brigadir Mar 18 '16 at 22:20
  • Yes, the existing connection stays alive. I'm not sure on performance. – Shay Thompson Aug 04 '16 at 19:23