Andy Novak
Andy Novak

Reputation: 388

iOS Bluetooth connected according to Settings, but my app has no connected peripherals

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:

[CoreBluetooth] XPC connection invalid

Upvotes: 0

Views: 1120

Answers (1)

Anil Gupta
Anil Gupta

Reputation: 1225

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.

Upvotes: 0

Related Questions