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.

Parents
  • The answer to your question is actually in the link you posted:)

    You don't manually connect to peripherals like that, the state restoration is only used when your app is force quit by iOS to preserve memory for example.

    iOS handles the connection/disconnection for your peripherals when the app is in background, if you are connected, and the app is closed, the connection will remain open to the peripheral and won't be disconnected, you can also scan and connect new peripherals in the background.

    You must also set the bluetooth background mode in your app's capabilities

    Please read those points thoroughly :

    if something is unclear please let us know.


    Edit1: Added more info to answer first comment

    To answer all your questions please refer to this page in the docs

    • Note that some peripherals might be connected even if your app is killed, but some might not if you went out of range, etc.., you can find how to find which in the link i posted above, but there are two methods to find the peripherals that are connected only and to find all peripherals that were discovered
    • Yes you will need to set a delegate for your peripherals again to be able to get events from them

    Here is a nice flowchart from the link above to explain a bit how to reconnect when you are restoring your app from a killed state: image description

  • If my all peripherals are connected and then my application get terminated by system due to memory. then I am able to get the peripherals in my willRestorePeripherals method. Once I get all the peripherals do I only need to set the peripherals.delegate = self and I can get the event of peripherals ? or do I need to send the connect command to those restored peripherals again ? am I missing something ?

    Also do we need to ReInitialize the Central Manager in AppDelegate again when I get a callback in my AppDelegate ? I know I am asking so many questions but It will be very helpful. Thanks.

Reply
  • If my all peripherals are connected and then my application get terminated by system due to memory. then I am able to get the peripherals in my willRestorePeripherals method. Once I get all the peripherals do I only need to set the peripherals.delegate = self and I can get the event of peripherals ? or do I need to send the connect command to those restored peripherals again ? am I missing something ?

    Also do we need to ReInitialize the Central Manager in AppDelegate again when I get a callback in my AppDelegate ? I know I am asking so many questions but It will be very helpful. Thanks.

Children
No Data
Related