【问题标题】:CoreBluetooth state preservation: correct way to restore CBCentralManagerCoreBluetooth 状态保存:恢复 CBCentralManager 的正确方法
【发布时间】:2016-01-12 12:56:52
【问题描述】:

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

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

【问题讨论】:

    标签: ios cbcentralmanager ble-state-preservation


    【解决方案1】:

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

    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

    【讨论】:

    • 谢谢。我阅读了指南,但我不明白何时调用此方法。我无法为其添加断点,我不知道如何测试它.. 谢谢
    猜你喜欢
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 2019-09-24
    • 2011-07-23
    相关资源
    最近更新 更多