iosbluetooth-lowenergycore-bluetoothcbperipheralios-bluetooth

Backgrounded CBPeripheral doesn't stay readable


I have an app that broadcasts a CBPeripheral with a small amount of text. I use the bluetooth-peripheral background mode to allow it to broadcast in the background.

When I scan from another device, in the foreground it works fine. I detect the peripheral, connect, discover the services then read the data from the service.

In the background however, there is something not quite right. I scan for peripherals with my service, and it finds it fine. I connect which also goes fine. I then discoverServices with my service which also goes fine as the didDiscoverServices callback is called. However in this callback, the peripheral has no services! peripheral.services is empty. It wouldn't have got this far if it didn't have the service, so what gives? The error in this callback is also nil

Everything works fine when broadcasting the CBPeripheral on iOS 12

EDIT ----- Spoke too soon, it seems that now my iOS 12 device has started showing the same symptoms. After a short while the CBPeripheral doesn't work.


Solution

  • The problem was fixed by adding the CBAdvertisementDataLocalNameKey key when calling startAdvertising on the CBPeripheralManager. It seems this key is required if you want your CBPeripheral to keep broadcasting in the background. Apple docs fail to mention this.