This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to connect multiple peripherals when app gets suspended or kills due to memory ?

I have added below code.

//Central manager Initialization

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{ CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManager"}];

//Appdelegate.m

 //State restoration
    NSArray *centralManagerIdentifiers =
    launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];
    
    for (NSString *identifier in centralManagerIdentifiers) {
        if ([identifier isEqualToString:@"myCentralManager"]) {
        }
    }

I have only one central manager, So, I am not sure that do I still need to Initialization the Central manager again.

To connect to restored peripherals I am sending connect command to each peripheral. But I am unable to connect all the peripherals. As per the Apple documentation developer.apple.com/.../PerformingTasksWhileYourAppIsInTheBackground.html

We have only 10 sec to complete the task, But I am not able to complete my task within 10 sec as We need to connect to multiple peripherals and write one id on it. which is time taking and not able to complete task for all the peripherals.

How I can able to Connect to all the peripherals when they gets restored ?

- (void)centralManager:(CBCentralManager *)central
      willRestoreState:(NSDictionary *)state {
    
    NSArray *peripherals =
    state[CBCentralManagerRestoredStatePeripheralsKey];
    
  if ([peripherals count] > 0) {
        for (CBPeripheral *peripheral in peripherals) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.centralManager connectPeripheral:peripheral options:nil];
            });
        }
    }
 }

Thanks in advance.