2014-02-24 41 views
5

我正在製作一個CoreBluetooth的應用程序,我希望它可以在後臺運行並執行藍牙相關的任務。CoreBluetooth backgound - 我如何重新實現appdelegate中央管理器對象?

有人可以解釋我如何重新實例化appdelegate中央管理器對象嗎?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey]; 

    for (NSString *identifier in centralManagerIdentifiers) { 

     if ([identifier isEqualToString:@"myCentral"]) { 

     // what to do here? 

     } 
} 

回答

3

我假設你想作爲"Reinstantiate Your Central and Peripheral Managers" section of the documentation描述你的應用程序代理還原多個切牙?

如果是的話,我可以看到didFinishLaunchingWithOptions看起來像這樣:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.referencesToCentrals = [NSMutableArray array]; 

    NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey]; 
    if ((centralManagerIdentifiers) && (centralManagerIdentifiers.count > 0)) { 
     // The system knows about one or more centrals that need to be restored. 
     for (NSString *identifier in centralManagerIdentifiers) { 
      CBCentralManager *manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBPeripheralManagerOptionRestoreIdentifierKey : identifier}]; 
      [self.referencesToCentrals addObject:manager]; 
     } 
    } 
    else { 
     // No centrals need to be restored. If desired, create one for use and save a reference like this: 
     CBCentralManager *manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBPeripheralManagerOptionRestoreIdentifierKey : [[NSUUID UUID] UUIDString]}]; 
     [self.referencesToCentrals addObject:manager]; 
    } 

    // Set up window, etc... 
    return YES; 
} 

你可能不希望保留在應用程序委託你所有的中切牙的參考,因爲我在這個例子中,也不一定現在做讓你的應用程序代理充當中央的CBCentralManagerDelegate,但你明白了...

+0

我看到你正在初始化應用程序委託中央管理器,但是ifcentral管理器在不同的類中呢?我有中央經理的共享管理器。 – Paragon

+0

通過在初始化中將代理設置爲「self」,您意味着AppDelegate就是代表。它是否正確? – vikzilla

相關問題