OStack程序员社区-中国程序员成长平台

标题: ios - CoreBluetooth状态保存: correct way to restore CBCentralManager [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:20
标题: ios - CoreBluetooth状态保存: correct way to restore CBCentralManager

当应用由于状态保存事件而获得选项午餐时,从 AppDelegate 恢复 CBCentralManager 的正确方法是什么?

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)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;
}



Best Answer-推荐答案


当您获得标识符列表时,您必须遍历此列表并为每个标识符初始化 CBCentralManager 的实例。列表包含 NSString 的对象。

NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];

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

    [self.cenralManagers addObject:centralManager];
}

更多详情请引用State Preservation and Restoration在核心蓝牙编程指南中。

关于ios - CoreBluetooth状态保存: correct way to restore CBCentralManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125295/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4