Core Bluetooth Events when enabling State Restoration

Hello dear Apple Platform Engineers,

I'm working on an iOS App that connects to a Bluetooth Low Energy Peripheral in the foreground. Whenever the App moves into Background, we want to continue syncing data & therefore implemented State Restoration to be informed about Discovery, Connect, Disconnect or Characteristic changes in the Background in order to wake the App up whenever it was "Terminated due to memory issue".

I consulted https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW1

for this & assigned:

  • CBCentralManagerOptionRestoreIdentifierKey
  • Reinstantiate the dedicated CentralManager &
  • implemented the centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) Delegate

Additionally, I assigned the following keys on connect(_ peripheral: CBPeripheral, options: [String : Any]? = nil)

  • CBConnectPeripheralOptionNotifyOnConnectionKey
  • CBConnectPeripheralOptionNotifyOnDisconnectionKey
  • CBConnectPeripheralOptionNotifyOnNotificationKey

However, from my experience the App gets woken up, but neither the willRestoreState nor the didDiscover, didConnect or didDisconnectPeripheral is called even though launchOptions?[UIApplication.LaunchOptionsKey.bluetoothCentrals] as? [String] is not empty.

What might I be missing when trying to make this approach work? Thanks a lot for any insights & support!

First of all, to clarify, willRestoreState() will only be called if the app has been terminated (for example due to memory pressure) and is being launched. If it is being just "woken up", the only function that is going to be called will be the one that is related to the triggering event (like didDiscoverPeripheral(), didConnect(), etc.)

if willRestoreState() (or others) is not being called there must be something structurally missing. If the app is indeed being launched for Bluetooth events, and the callback's are not happening, you should check the following:

  • that you are instantiating the CBCentralManager with the same restore identifier as before (that must be the first thing you do)
  • that the delegate functions are in the correct class, and that class is one that is correctly declares the CBCentralManagerDelegate protocol
  • none of your logic is being hidden or ignored due to a code branch that doesn't get executed during a background launch.

Thanks a lot for your quick reply. I made sure that the requirements you mentioned are met & which left me with the following scenario:

  1. Peripheral is connected in the Foreground
  2. App moves in the Background
  3. At some point the App is terminated because of using too much memory
  4. Peripheral moves out of range & disconnects
  5. App is launched in the Background and willRestoreState is called correctly
  6. App is again suspended and terminated at some point
  7. Peripheral moves into range and should re-connect by launching the App in the Background again

Up until 7. everything works as expected. So I assume there needs some work to be done in willRestoreState() to make sure the Peripheral can connect again. Unfortunately, the documentation is very sparse at this point.

Thanks a lot for your help again! Highly appriciated

Core Bluetooth Events when enabling State Restoration
 
 
Q