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?
}
}
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.
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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With