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:
I add "bluetooth central" in background mode in info.plist, so the BLE runs in background.
start centralManager in my iphone No.1. start scan.
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.
start a beacon with another iphone No.2 and start broadcasting.
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…