iOS核心蓝牙状态保存和恢复问题 [英] iOS Core Bluetooth state Preservation and Restoration issues

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

问题描述

我对iOS 7上的核心蓝牙的状态保存和恢复有疑问。

I have a question regarding state Preservation and Restoration for Core Bluetooth on iOS 7.

我似乎无法让它正常工作。我已经按照苹果在他们的核心蓝牙文档中提到的每个指导以及状态保存的一般文档。

I can't seem to get it to work properly. I have followed every guidline that apple mentions in their documentation for core bluetooth as well as the general documentation for state preservation.

例如这里:核心蓝牙后台处理适用于iOS应用和此处: iOS状态保存和恢复

我可以在设备上使用一般状态保存(用于视图控制器和对象等),但不能用于蓝牙经理。

I can get the general state preservation to work on the device (for view controllers and objects etc.), but not for the bluetooth manager.

据我所知,cheklist看起来像这样:

As far as I know the cheklist look like this:


  1. 选择保护通过在键 CBCentralManagerOptionRestoreIdentifierKey 的选项字典中分配恢复标识符来分配和初始化中央管理器对象时进行和恢复。

  1. Opt in preservation and restoration when you allocate and initialize a central manager object by assigning a restoration identifier in the options dictionary for the key CBCentralManagerOptionRestoreIdentifierKey.

在系统重新启动应用程序后,重新实例化任何中央管理器对象。当应用程序:didFinishLaunchingWithOptions:被调用时,这在app delegate中完成。在这里,我应该在选项字典中查找 UIApplicationLaunchOptionsBluetoothCentralsKey ,然后使用该密钥重新安排CBManager。在这里出现问题,因为该密钥从来没有任何标识符,因此我无法重新发布它。

Reinstantiate any central manager objects after your app is relaunched by the system. This is done in app delegate when the application:didFinishLaunchingWithOptions: is called. Here I am supposed to look for the UIApplicationLaunchOptionsBluetoothCentralsKey in the options dictionary and then reinstatiate the CBManager with that key. It is here where things go wrong since there are never any identifiers for that key, and thus I can not reinstatitate it.

实现适当的恢复委托方法。我也做了这一步,但由于经理从未重新实例化,我从未收到过该委托回调。

Implement the appropriate restoration delegate method. I have done this step also, but since the manager is never re-instantiated I never receive this delegate callback.

该应用程序在后台工作正常,我也已按照该部分的所有步骤进行操作。

The app is working fine in the background and I have followed all the steps for that part as well.

现在,说完所有这些,我并不确定如何测试这个,这可能是问题的一部分。我现在的做法是按下iOS设备上的主页按钮(执行设备),以便将应用程序置于后台并返回主屏幕。在这样做时,我可以通过查看我的日志输出来判断所有常规状态保存调用是否实现。在此之后,我通过按Xcode中的停止按钮退出应用程序以终止后台进程。我现在通过Xcode重新启动应用程序,现在我再次看到正在执行的所有常规状态保存代码,除了蓝牙管理器之外的所有状态都会恢复。

Now, having said all this, I am not entierly sure how to test this and that might be a part of the broblem. The way I do it now is to press the home button on the iOS device (actuall device) so that it puts the app into background and goes back to the home screen. While doing this I can tell that all the regular state preservation calls are acheived by looking at my log output. After this i quit the app by pressing the stop button in Xcode to kill the background process. I now restart the app through Xcode and once again I can now see all the regular state preservation code being executed and the state gets restored on everything except the bluetooth manager.

如果这是错的,请告诉我。但总的来说,我对此非常困惑,因为它在核心蓝牙文档中说,只有当你的应用程序被系统重新启动时才会进行保存。那个的真实意义是什么?我还阅读了Apple开发者论坛上的帖子,自从iOS 7以来,如果用户手动杀死我正在做的应用程序,操作系统现在永远不会重新启动应用程序..

If this is wrong, then please let me know. But in all, I am very confused about this since it says in the Core Bluetooth docs that the preservation occurs only when "your app is relaunched by the system". What does that really mean? I also read a post on the Apple developer forum that since iOS 7 the OS will now never relaunch the app for any reason if the user kills the app manually which I am doing..

对此有任何帮助将不胜感激!

Any help regarding this would be much appreciated!

/ A

推荐答案

当您单击主页按钮将应用程序发送到后台时,它会暂停,并且可以处理蓝牙代表并在后台运行10秒,此功能可以通过在info.plist中的后台模式中添加蓝牙中心来实现,并且不要使用State Preservation&恢复。

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 could be realize by " add bluetooth central in background mode in info.plist", and do not use State Preservation & Restoration.

如果您的应用由于内存压力而被IOS终止,它将无法再处理蓝牙代表。在这种情况下,如果您使用了State Preservation&恢复,您的应用程序可以重新启动到后台再次运行,也只有10秒。 10秒后,它将进入暂停状态。
仅在这种情况下,可以触发CBCentralManager的willRestoreState。

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.

您可以添加代码

[kill(getpid(), SIGKILL);]

按钮动作,当你点击按钮时,你的应用程序将被IOS终止,就像内存压力被杀,然后willRestoreState将被触发。

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.

祝你好运。

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

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