Nirav Pandya
Nirav Pandya

Reputation: 31

iOS BLE device paired while app is in background or killed or suspend

I am able to pair/connect BLE device with my app while app is open. But when my app goes to background/suspended mode, I am not able to keep connection alive. Is is possible to keep connection alive and get notified for disconnect event for device?

Upvotes: 2

Views: 5612

Answers (3)

iOS_Maccus
iOS_Maccus

Reputation: 397

For supporting the Core-Bluetooth in suspended / background mode Enable your application for backdrops support.

Goto your Project --> Target --> Capabilities --> Switch On Background Modes --> Select Use Bluetooth LE accessories.

But iOS system may kill your background application at any time for free the memory for foreground application.

For getting the connected/Disconnected events use below line of code.

central.connect(peripheral, options: [CBConnectPeripheralOptionNotifyOnConnectionKey:true, CBConnectPeripheralOptionNotifyOnDisconnectionKey: true])

Upvotes: 3

Andrea
Andrea

Reputation: 26383

Sure it is.
First things first: you can scan peripheral and connect to them in background as long as they expose at least one service(this service must be indicated while you are launching the scan command). You must specify that in your target capabilities, just by checking Uses Bluetooth LE accessories.
To detect bluetooth disconnection you just need to implement the CBCentralManagerDelegate method func centralManager(CBCentralManager, didDisconnectPeripheral: CBPeripheral, error: Error?).
The connection is kept alive in background as long as your peripheral does not require a keep alive signal, if you need to do some operations in background the Uses Bluetooth LE accessories check is required.
To simplify working with BLE I suggest you to check out this library.

Upvotes: 2

Vasily Avilov
Vasily Avilov

Reputation: 163

For background applications there is limitations. Not all functions may be used in background. Then you send application to moderation with unreasonable background functions you will be failed.

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that record audio content while in the background
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Apps that need to download and process new content regularly
  • Apps that receive regular updates from external accessories

Here you will find more description. May be your case is "Apps that receive regular updates from external accessories" with "Acts as a Bluetooth LE accessory" mode.

In google also you will find a lot of information.

Upvotes: 1

Related Questions