Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

ios - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...