且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS核心蓝牙状态保存和恢复问题

更新时间:2023-10-28 17:02:22

当您单击主页按钮将应用程序发送到后台时,它会暂停,并且可以处理蓝牙代表并在后台运行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.

祝你好运。