0
votes

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:

  1. App working well with BT Device
  2. BT Device's battery dies, disconnects from app
  3. BT Device battery charged, attempts to reconnect to app
  4. BT Device connects to OS (Apple's BT Settings says the device is connected)
  5. App discovers all the CBCharacteristics, and the characteristics' isNotifying property is false
  6. App performs setNotifyValue to true on the required characteristics
  7. Problem: This function is never called: func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
  8. 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
1
How do you initiate the connection in your IOS code when then connection is lost? You should simply call peripheral.connect() in didDisconnectPaulw11
To reconnect, I call self.manager.connect(peripheral, options: nil) (where self.manager is the same as in step 8 above) when centralManager didDisconnectPeripheral is calledAndy Novak

1 Answers

0
votes

If your BLE device get disconnected then you need to call this function

[centralObj connectPeripheral:peripheral options:nil];

in didDisconnectPeripheral in delegate methods. You don't need to call scanForPeripheralsWithServices every time.