核心蓝牙状态保存和恢复不起作用,无法重新启动应用程序到后台 [英] Core Bluetooth State Preservation and Restoration Not Working, Can't relaunch app into background

查看:159
本文介绍了核心蓝牙状态保存和恢复不起作用,无法重新启动应用程序到后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力让核心蓝牙唤醒应用程序,即使它没有运行。



正如Apple所说,因为状态保存和恢复是内置的核心蓝牙,您的应用可以选择使用此功能,要求系统保留应用中央和外围设备管理器的状态,并代表他们继续执行某些与蓝牙相关的任务,即使您的应用不再运行。这些任务完成后,系统会将您的应用重新启动到后台,并为您的应用提供恢复其状态和适当处理事件的机会。



我添加了以下代码以选择将这个功能:

  myCentralManager = 
[[CBCentralManager的alloc] initWithDelegate:自队列:无
选项:@ {CBCentralManagerOptionRestoreIdentifierKey:
@myCentralManagerIdentifier}];

但应用程序被唤醒时的回调从未被触发。



'pre> - (BOOL)应用:(UIApplication的*)申请
didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {
}

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

这两个从未被调用过。



我正在测试这个唤醒功能的方式:


  1. 我在info.plist中的后台模式中添加了bluetooth central,因此BLE在后台运行。


  2. 在我的iphone No.1中启动centralManager。开始扫描。


  3. 按回家退出,玩一些内存沉重的游戏,在调试日志中我会看到:
    由于内存终止压力。过程完成,退出代码为0。这是为了模拟ios系统如何因内存压力终止后台应用程序。


  4. 用另一部iphone No.2启动一个信标并开始广播。


  5. 结果:那些重新启动的回调永远不会被调用。


为什么这不起作用的任何想法?如果这是一个API问题,当你的手机接近BLE信标时,有没有其他方法可以将你的应用重新启动到BLE的后台?我已尝试使用ibeacon唤醒应用程序,但核心蓝牙中央管理器将不允许您在后台连接到ibeacon。



谢谢!

解决方案

CoreBluetooth 状态恢复仅适用于连接和外围事件。目前不支持仅依靠扫描。


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!

解决方案

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

这篇关于核心蓝牙状态保存和恢复不起作用,无法重新启动应用程序到后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆