如何触发 Core 蓝牙状态保存和恢复 [英] How to trigger Core Bluetooth state preservation and restoration

查看:17
本文介绍了如何触发 Core 蓝牙状态保存和恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

状态保存和恢复 因为状态保存和恢复恢复内置于 Core Bluetooth,您的应用可以选择加入此功能要求系统保留应用程序中心状态的功能和外围经理,并继续执行某些代表他们执行与蓝牙相关的任务,即使您的应用程序没有运行时间更长.当其中一项任务完成时,系统将您的应用程序重新启动到后台并为您的应用程序提供有机会恢复其状态并处理事件 对于上述家庭安全应用,系统将监控连接请求,并重新启动应用程序来处理 centralManager:didConnectPeripheral: delegate用户回家和连接请求时的回调完成.

如何触发并测试代码?

我有一个提供服务的配件.我有一个扫描服务的应用程序,我选择了状态保留.但是我不确定如何从逻辑上测试它,因为我不知道我需要什么来触发它.这些是我尝试失败的选项:

A - 从 Xcode 中杀死应用程序B - 手动终止应用程序C - 关闭手机D——别的

在所有这些选项中,我尝试转到 Xcode -> device 并查看日志,但没有看到任何状态恢复日志.

谢谢

解决方案

注意 感谢

State Preservation and Restoration 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. In the case of the home security app described above, the system would monitor the connection request, and re-relaunch the app to handle the centralManager:didConnectPeripheral: delegate callback when the user returned home and the connection request completed.

How can I trigger this and test the code?

I have got an accessory with a service. I have got an app that scans for the service and I opted in state preservation. However I am not sure how to logically test it as I do not know what I need to trigger it. These are the options that I tried unsuccessfully:

A - kill the app from Xcode

B - kill the app manually

C - power off the phone

D - something else

In all these options I tried to go to Xcode -> device and look at the logs, but haven't seen any state restoration logs.

Thanks

解决方案

NB Thanks to user1785784 for sharing Apple's QA1962 - Conditions Under Which Bluetooth State Restoration Will Relaunch An App which describes new Bluetooth behaviour in iOS 11. This document should be considered the answer to this question, although I think it incorrectly claims that iOS 10 would relaunch an app that has been force quit. (I haven't tested that on an iOS 10 device, but it would have been a departure from iOS 9. Can anyone confirm?).

Killing the app manually (B) from the task switcher, ensures your app will not be launched automatically until the user explicitly opens it again.

C doesn't work either, I think only VOIP apps are launched automatically after restart, and then only after the device is unlocked.

I don't know any D.

I use A.

First, to implement Bluetooth State Restoration, make sure you've

  1. added bluetooth-central as a UIBackgroundModes to your Info.plist
  2. set a CBCentralManagerOptionRestoreIdentifierKey when initing your CBCentralManager
  3. implemented the -(void)centralManager:willRestoreState: callback in your CBCentralManager delegate.

Then you're ready to test state restoration:

  1. get the app to some known state (say bluetooth powered on, some device connected/connecting)
  2. kill the app in Xcode
  3. watch the logs or set a launch breakpoint
  4. make a change in the bluetooth state, e.g. by
    • toggling airline mode
    • taking a bluetooth device out of range (to avoid walking, I put mine in a conductor/Faraday Cage/coffee pot)
    • bring the device back into range
    • interacting with device, e.g. by pressing a button/having a pulse
  5. watch your state restoration code be called

NB: application:didFinishLaunchingWithOptions: will be called first, and you must immediately init your CBCentralManager as described above. Then centralManager:willRestoreState: will be called.

这篇关于如何触发 Core 蓝牙状态保存和恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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