Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Bluetooth State Preservation and Restoration Not Working, Can't relaunch app into background

I'm trying to make core bluetooth wake up the app even when it's not running.

As Apple stated, "Because state preservation and restoration is built in to Core Bluetooth, your app can opt in to this feature to ask the system to preserve the state of your app’s central and peripheral managers and to continue performing certain Bluetooth-related tasks on their behalf, even when your app is no longer running. When one of these tasks completes, the system relaunches your app into the background and gives your app the opportunity to restore its state and to handle the event appropriately."

I added following code to opt in to this feature:

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

But the callbacks when app is woke up never got triggered.

-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}

-(void)centralManager:(CBCentralManager *)central
      willRestoreState:(NSDictionary *)state {
}

These two are never called.

The way I'm testing this wake up function:

  1. I add "bluetooth central" in background mode in info.plist, so the BLE runs in background.

  2. start centralManager in my iphone No.1. start scan.

  3. press home and get out, play some memory heavy game, in the debug log i will see: "Terminated due to Memory Pressure. Process finished with exit code 0". This is to simulate how ios system terminate the background app due to memory pressure.

  4. start a beacon with another iphone No.2 and start broadcasting.

  5. result: those relaunch callbacks never get called.

Any ideas why this is not working? If it's an API problem, is there any other approach to relaunch your app into background with BLE when your phone gets close to BLE beacon? I've tried with using ibeacon to wake up the app, but core bluetooth central manager won't allow you to connect to ibeacon in background.

Thanks!

like image 426
woof Avatar asked Dec 11 '13 20:12

woof


2 Answers

When you click home button to send app to background, it it suspended, and can handle Bluetooth delegates and run in background for 10s, this feature can be achieved solely by "add bluetooth central in background mode in info.plist", and does not use State Preservation & Restoration.

If your app is terminated by IOS, due to memory pressure, it can't handle bluetooth delegates anymore. In this case, if you used State Preservation & Restoration, your app can be relaunched to background to run again, also for only 10s. After 10s, it would move to suspended state. Only in this situation, CBCentralManager's willRestoreState can be triggered.

You can add code

kill(getpid(), SIGKILL);

to a button action, when you click the button, your app will be terminated by IOS just like killed by memory pressure, and then "willRestoreState" will be triggered.

Good luck.

like image 71
Roy Zhang Avatar answered Oct 04 '22 09:10

Roy Zhang


CoreBluetooth state restoration only applies to connection and peripheral events. Solely relying on scanning is not currently supported.

like image 26
Tommy Devoy Avatar answered Oct 04 '22 10:10

Tommy Devoy