CoreBluetooth state preservation: correct way to restore CBCentralManager

浪子不回头ぞ 提交于 2020-01-04 17:16:31

问题


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;
}

回答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.



来源:https://stackoverflow.com/questions/33125295/corebluetooth-state-preservation-correct-way-to-restore-cbcentralmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!