Reputation: 18237
I wish to keep scanning for Bluetooth Devices and I want app to be alive in the background. Is it possible? It seems like if I have a connected Bluetooth device, and if there is data transfer, then the app stays awake in the background. However, if I am just scanning in the background, it seems like even if I have Uses Bluetooth LE accessories
checked(bluetooth-central
in plist), the app ends up not running.
Is there a way to keep the app alive and continuous scan for devices when it is in the background?
Upvotes: 13
Views: 23968
Reputation: 3396
From CBCentralManager
:
/**
* @method scanForPeripheralsWithServices:options:
*
* @param serviceUUIDs A list of <code>CBUUID</code> objects representing the service(s) to scan for.
* @param options An optional dictionary specifying options for the scan.
*
* @discussion Starts scanning for peripherals that are advertising any of the services listed in <i>serviceUUIDs</i>. Although strongly discouraged,
* if <i>serviceUUIDs</i> is <i>nil</i> all discovered peripherals will be returned. If the central is already scanning with different
* <i>serviceUUIDs</i> or <i>options</i>, the provided parameters will replace them.
* Applications that have specified the <code>bluetooth-central</code> background mode are allowed to scan while backgrounded, with two
* caveats: the scan must specify one or more service types in <i>serviceUUIDs</i>, and the <code>CBCentralManagerScanOptionAllowDuplicatesKey</code>
* scan option will be ignored.
*
* @see centralManager:didDiscoverPeripheral:advertisementData:RSSI:
* @seealso CBCentralManagerScanOptionAllowDuplicatesKey
* @seealso CBCentralManagerScanOptionSolicitedServiceUUIDsKey
*
*/
open func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?, options: [String : Any]? = nil)
The important bit here is Applications that have specified the bluetooth-central background mode are allowed to scan while backgrounded, with two caveats (...)
@huggie, did you achieve anything?
Upvotes: 1
Reputation: 655
As per apple developer documentation technical Q&A.
Can I use an iOS device to issue iBeacon advertisements while my app is in the background? Answer: No. For an iOS device to issue iBeacon advertisements, the app requesting this functionality must be frontmost, with the screen turned on and the device unlocked.
Reference - https://developer.apple.com/ibeacon/Getting-Started-with-iBeacon.pdf
I found this article which provides some details to connect BLE in background for some specific case. https://medium.com/@cbartel/ios-scan-and-connect-to-a-ble-peripheral-in-the-background-731f960d520d
Upvotes: 1