2

what is the correct way to restore the CBCentralManager from AppDelegate when the App gets lunched with options due to a state preservation event?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // The system provides the restoration identifiers only for central managers that had active or pending peripheral connections or were scanning for peripherals.
    NSArray * centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];

    if (centralManagerIdentifiers != nil) {
        for (int i=0; i<[centralManagerIdentifiers count]; i++) {
            NSString * identifier = [centralManagerIdentifiers objectAtIndex:i];
            NSLog(@"bluetooth central key identifier %@", identifier);
            // here I expect to re-instatiate the CBCentralManager but not sure how and if this is the best place..
        }
    }

    // Override point for customization after application launch.
    return YES;
}
mm24
  • 9,280
  • 12
  • 75
  • 170

1 Answers1

1

When you get list of identifiers, you have to iterate thru this list and initialise instance(s) of CBCentralManager for each identifier. List contains NSStrings objects.

NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];

for (NSString *centralManagerIdentifier in centralManagerIdentifiers) {
    CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self
                                                                            queue:nil
                                                                          options:@{CBCentralManagerOptionRestoreIdentifierKey: centralManagerIdentifier}];

    [self.cenralManagers addObject:centralManager];
}

For more details please refer to State Preservation and Restoration in Core Bluetooth Programming Guide.

psci
  • 903
  • 9
  • 18
  • Thanks. I read the guide but I fail to understand when this method is called. I am unable to add a breakpoint to it and I am not sure how to test this.. thanks – mm24 Oct 14 '15 at 15:30