I am making an app for a custom Bluetooth device. The app connects/communicates correctly with the device, however some strange connection/reconnection problems occur sometimes.
One reproducible situation of this issue is when the battery dies:
- App working well with BT Device
- BT Device's battery dies, disconnects from app
- BT Device battery charged, attempts to reconnect to app
- BT Device connects to OS (Apple's BT Settings says the device is connected)
- App discovers all the CBCharacteristics, and the characteristics' isNotifying property is false
- App performs setNotifyValue to true on the required characteristics
- Problem: This function is never called: func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
- Problem: self.manager.retrieveConnectedPeripherals(withServices: [identifier]) returns no connected peripherals, even though Apple's BT Settings says that the device is connects.
This is how the manager in step 8 is initiated:
fileprivate override init() {
super.init()
self.manager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey : Constants.cbCentralManagerOptionRestoreIdentifierKey])
}
Question: Does anyone know what the cause of this could be? Or what the fix is?
Possible Clues:
I need to select Forget the device twice to forget the device in Apple's BT Settings (after the first time forgetting, the device is still connected with the info button displayed)
I have background bluetooth enabled
I get the following message in my debugger:
[CoreBluetooth] XPC connection invalid
- I am on Swift 3, Xcode 8
peripheral.connect()
indidDisconnect
– Paulw11self.manager.connect(peripheral, options: nil)
(where self.manager is the same as in step 8 above) when centralManager didDisconnectPeripheral is called – Andy Novak