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:
Instantiating the central manager as directed:
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{ CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManagerIdentifier" }];
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?)
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?