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".
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
CBCentralManagerDelegateprotocol - none of your logic is being hidden or ignored due to a code branch that doesn't get executed during a background launch.