Bluetooth peripheral connected to iOS doesn't work in the background
<p>I’m having an issue with a bluetooth peripheral working with a backgrounded iOS app. I need the app to respond to all bluetooth notifications in real-time. I’ve followed <a href="https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html" rel="nofollow noreferrer">Apple's CoreBluetooth background process guide</a> and it works around 10% of the time my app is backgrounded, but the other 90% of the time the events are queued and sent to my app in batches every 10 minutes.</p>

<p>I just need to fire a quick local subnet network request over wifi after a bluetooth event, so it takes well under the 10s iOS gives apps to work in the background.</p>

<p>As for CoreBluetooth settings, I followed the guide to the letter, and it does work in the background occasionally. But it also batches up notifications and re-launches my app every ten minutes, which makes me think it is configured correctly.</p>

<p>I can confirm the app isn’t being killed. I can keep the debugger attached and it continues to run. In fact, the debugger shows that the notifications get queued and the app enters the foreground every ten minutes. It does work for a few minutes after I use the app. But it inevitably gets fully backgrounded.</p>

<p>I would expect the app to be woken up as soon as a bluetooth notification comes in. Checking the connected devices list in the iOS Settings > Bluetooth screen shows a connected peripheral. And it's clear iOS is capturing the notifications. But it rarely wakes the app to send the notifications.</p>

<p><hr>
<strong>UPDATE Aug 10th, 2016:</strong></p>

<p>So the issue is not in the device itself. It stays connected and even iOS's Settings app shows the device is still connected. I realized though that I had <code>beginBackgroundTaskWithExpirationHandler</code> running on <code>applicationWillResignActive</code>, which was giving me pseudo-background activity. When I commented that out, the app no longer un-suspends on bluetooth notification from my device.</p>

<p>All of the bluetooth notifications are queued and all immediately fire when the app is re-launched manually. Debugging the app shows no activity when the app is in the background. So now it just looks like I haven't configured the app to correctly work with Bluetooth in the background.</p>

<p>So my hunch is that something is mis-configured in following the CoreBluetooth background doc. But there's only a few steps and I have implemented each of them. I would expect <code>launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey]</code> to be sent to <code>application(_:didFinishLaunchingWithOptions:)</code>, but it is never sent. And <code>centralManager(_:willRestoreState:)</code> is never called.</p>

<p>I have "Uses Bluetooth LE accessories" enabled.</p>

<p><a href="https://i.stack.imgur.com/tKpoF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tKpoF.png" alt="Uses Bluetooth LE accessories"></a></p>

<p>I'm using CBCentralManagerOptionRestoreIdentifierKey:</p>

<pre><code>manager = CBCentralManager(delegate: self, queue: nil, options: 
                           [CBCentralManagerOptionRestoreIdentifierKey: "TTcentralManageRestoreIdentifier",
                            CBConnectPeripheralOptionNotifyOnDisconnectionKey: NSNumber(bool: true),
                            CBConnectPeripheralOptionNotifyOnConnectionKey: NSNumber(bool: true),
                            CBConnectPeripheralOptionNotifyOnNotificationKey: NSNumber(bool: true)])
</code></pre>

<p>I'm subscribing to the characteristic notification:</p>

<pre><code>func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {        
    if service.UUID.isEqual(CBUUID(string: DEVICE_V2_SERVICE_BUTTON_UUID)) {
        for characteristic: CBCharacteristic in service.characteristics! {
            if characteristic.UUID.isEqual(CBUUID(string: DEVICE_V2_CHARACTERISTIC_BUTTON_STATUS_UUID)) {
                peripheral.setNotifyValue(true, forCharacteristic: characteristic)
                let device = foundDevices.deviceForPeripheral(peripheral)
                device?.buttonStatusChar = characteristic
            } else if characteristic.UUID.isEqual(CBUUID(string: DEVICE_V2_CHARACTERISTIC_NICKNAME_UUID)) {
                peripheral.readValueForCharacteristic(characteristic)
            }
        }
    }
    ...
}
</code></pre>

<p><code>UIApplicationLaunchOptionsBluetoothCentralsKey</code> is never called.</p>

<pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
    ...
    let centralManagerIdentifiers = launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey]
    if centralManagerIdentifiers != nil {
        print(" ---&gt; centralManagerIdentifiers: \(centralManagerIdentifiers)")
    }
    ...
}
</code></pre>

<p>Finally, <code>centralManager(_:willRestoreState:)</code> is never called:</p>

<pre><code>func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) {
    manager = central
    let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey] as! [CBPeripheral]
    print(" ---&gt; Restoring state: \(peripherals)")
    ...
}
</code></pre>