Reputation: 31
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
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
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
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.
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