CoreBluetooth状态保存

CoreBluetooth状态保存

本文介绍了CoreBluetooth状态保存:还原CBCentralManager的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当由于状态保存事件而使应用程序吃完午餐时,从AppDelegate恢复CBCentralManager的正确方法是什么?

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


推荐答案

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

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.

这篇关于CoreBluetooth状态保存:还原CBCentralManager的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!