Unable to trigger state restoration code for BLE on iOS
Asked Answered
C

0

19

I am trying to get my app to respond relaunch into the background is response to discovering an advertising peripheral.

The following code currently works in my app (in foreground + background, even after many hours of being backgrounded):

[self.centralManager scanForPeripheralsWithServices:self.services options:nil];

Generally, my app does not have any pending connection requests, as I want to scan for new advertisements in the background. The following documentation leads me to believe that this is still possible:

it is important to keep in mind that the app will be relaunched and restored if and only if it is pending on a specific Bluetooth event or action (like scanning, connecting, or a subscribed notification characteristic), and this event has occurred. (from https://developer.apple.com/library/content/qa/qa1962/_index.html)

and

The system keeps track of...The services the central manager was scanning for (and any scan options specified when the scan started) (from https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html)

Implementation

I am following the steps outlined in this last link ^ under the section "Adding Support for State Preservation and Restoration", by doing the following things:

  1. Instantiating the central manager as directed:

    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{ CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManagerIdentifier" }];

  2. Reinstantiate Your Central and Peripheral Managers

Here I've actually not done anything else, since didFinishLaunchingWithOptions is called and the central will be reinitialized with the same identifier. (Is there something else to be done here that I'm missing?)

  1. Implementing the restoration function:

    (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)state { // Breakpoint set here but never reached... NSLog(@"..."); }

How I am testing state preservation / restoration

I have set a breakpoint in the restore function (from step 3) and also inserted an NSLog.

By clicking "stop", as suggested in How to trigger Core Bluetooth state preservation and restoration, I was hoping that I could trigger the code by advertising with my peripheral as I would do normally (this discovery/connection flow is working perfectly when the app is running in the background). But nothing really happens that I can tell.

My general question is: why is the breakpoint not triggered / NSLogs not printed? Is there a preferred way to debug the restoration feature?

Finally, as a sanity check, am I reading the docs correctly that discovery of an advertising peripheral should trigger the re-launch of the app into the background for the app to handle?

Chuckwalla answered 30/5, 2018 at 10:22 Comment(4)
Do you have state restoration working on other pages? Are you familiar with how to properly debug state restoration?Retroversion
Did you happen to solve the issue ? If yes, please post answer, as I am also looking for something related to this and answer to this question will lead me ahead.Snout
@TusharJ. yes my issue was actually unrelated. My device was running on a VPN that would time out in the background. So state restoration was working the whole time. Be sure you're following the steps in the documentation closely and everything should work.Chuckwalla
@Chuckwalla yes sure, thanks for info.Snout

© 2022 - 2024 — McMap. All rights reserved.