CoreBluetooth backgound - 如何重新实现appdelegate中的中央管理器对象?

use*_*217 6 bluetooth objective-c ios core-bluetooth

我正在使用CoreBluetooth制作应用程序,我希望它在后台运行并执行与蓝牙相关的任务.

有人可以解释我如何重新实现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?

      }
}
Run Code Online (Sandbox Code Playgroud)

Tro*_*000 3

我假设您想要恢复应用程序委托中的多个中心,如文档的“重新实例化您的中心和外设管理器”部分中所述?

如果是这样,我可以看到 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;
}
Run Code Online (Sandbox Code Playgroud)

您可能不想像我在本示例中所做的那样在应用程序委托中保留对所有中心的引用,也不一定让您的应用程序委托充当中心的 CBCentralManagerDelegate 但您明白了...