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 `beginBackgroundTaskWithExpirationHandler` running on `applicationWillResignActive`, which was giving me pseudo-background activity. When I commented that out, the app no longer un-suspends on bluetooth notification from my device.
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.
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 `launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey]` to be sent to `application(_:didFinishLaunchingWithOptions:)`, but it is never sent. And `centralManager(_:willRestoreState:)` is never called.
- I have "Uses Bluetooth LE accessories" enabled
- I'm using CBCentralManagerOptionRestoreIdentifierKey:
manager = CBCentralManager(delegate: self, queue: nil, options:
[CBCentralManagerOptionRestoreIdentifierKey: "TTcentralManageRestoreIdentifier",
CBConnectPeripheralOptionNotifyOnDisconnectionKey: NSNumber(bool: true),
CBConnectPeripheralOptionNotifyOnConnectionKey: NSNumber(bool: true),
CBConnectPeripheralOptionNotifyOnNotificationKey: NSNumber(bool: true)])
I'm subscribing to the characteristic notification:
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)
}
}
}
...
}
`UIApplicationLaunchOptionsBluetoothCentralsKey` is never called.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
let centralManagerIdentifiers = launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey]
if centralManagerIdentifiers != nil {
print(" ---> centralManagerIdentifiers: \(centralManagerIdentifiers)")
}
...
}
Finally, `centralManager(_:willRestoreState:)` is never called:
func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) {
manager = central
let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey] as! [CBPeripheral]
print(" ---> Restoring state: \(peripherals)")
...
}