2015-10-14 88 views
1

當應用程序由於狀態保存事件而與選項共享時,從AppDelegate恢復CBCentralManager的正確方法是什麼?CoreBluetooth狀態保存:正確的恢復方式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; 
} 

回答

0

當你標識符的列表,你必須遍歷通過這個列表,並初始化的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]; 
} 

欲瞭解更多詳情,請參考Core Bluetooth Programming Guide中的State Preservation and Restoration

+0

謝謝。我閱讀指南,但我不明白何時調用此方法。我無法添加一個斷點,我不知道如何測試這個..謝謝 – mm24