Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreBluetooth backgound - How do i reinstantiate the central manager objects in the appdelegate?

I´m making an app with CoreBluetooth and i want it to run in background and perform bluetooth-related tasks.

Can someone explain me how to reinstantiate the central manager objects in the 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?

      }
}
like image 221
user3347217 Avatar asked Feb 24 '14 15:02

user3347217


People also ask

What is core Bluetooth background execution?

This support is important for apps that interact with Bluetooth low energy devices that deliver data at regular intervals, such as a heart rate monitor. There are two Core Bluetooth background execution modes that an app may declare—one for apps implementing the central role, and another for apps implementing the peripheral role.

How to access core Bluetooth APIs on iOS devices?

To access Core Bluetooth APIs on apps linked on or after iOS 13, include the NSBluetoothAlwaysUsageDescription key. In iOS 12 and earlier, include NSBluetoothPeripheralUsageDescription to access Bluetooth peripheral data. A remote device connected to a local app, which is acting as a peripheral.

Can I subclass the core Bluetooth framework?

Don’t subclass any of the classes of the Core Bluetooth framework. Overriding these classes isn’t supported and results in undefined behavior. Core Bluetooth background execution modes aren’t supported in iPad apps running on macOS.

Why can’t I run core Bluetooth background execution modes on iPad apps?

Core Bluetooth background execution modes aren’t supported in iPad apps running on macOS. Your app will crash if its Info.plist doesn’t include usage description keys for the types of data it needs to access. To access Core Bluetooth APIs on apps linked on or after iOS 13, include the NSBluetoothAlwaysUsageDescription key.


1 Answers

I'm assuming you're wanting to restore multiple centrals in your app delegate as described in the "Reinstantiate Your Central and Peripheral Managers" section of the documentation?

If so, I could see the didFinishLaunchingWithOptions looking something like this:

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

You might not want to keep a reference to all your centrals in the app delegate as I'm doing in this example nor necessarily have your app delegate act as the central's CBCentralManagerDelegate but you get the idea...

like image 117
Tron5000 Avatar answered Sep 28 '22 08:09

Tron5000