当应用程序被用户终止时重新连接蓝牙设备 [英] Re-connect bluetooth device when app is terminated by user

查看:233
本文介绍了当应用程序被用户终止时重新连接蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个蓝牙设备,上面有一个按钮。单击设备的按钮时,通知特性更新和我的应用程序显示本地通知。就这么简单。



我在前台连接蓝牙设备如下:

  var restoreOptions = [CBPeripheralManagerOptionRestoreIdentifierKey:customIdentifier] 
centralManager.connect(peripheral,options:restoreOptions)

然后设置的通知特征值为true:

  peripheral.setNotifyValue(true,for:notificationCharacteristic )

当通知特征更新时,应用会显示本地通知:

  func peripheral(_ peripheral:CBPeripheral,didUpdateValueFor characteristic:CBCharacteristic,error:Error?){
showLocalNotification()
}

当通知特征发生变化时,会触发didUpdateValueFor并显示本地通知。前景和背景没有问题。但是当用户终止应用程序时,蓝牙设备断开连接并且didUpdateValue为未触发。当特征改变甚至app被用户终止时我想重新连接蓝牙设备。我怎样才能做到这一点 ?我希望我能告诉你我的问题。谢谢。

解决方案

您需要启用并编程应用程序来处理核心 - 蓝牙背景模式



<这样,如果您的应用程序被操作系统杀死以释放一些资源,您就可以重新连接到设备。



即使您的应用支持一个或两个核心蓝牙后台执行模式,它不能永远运行。在某些时候,系统可能需要终止你的应用程序,以释放当前前台应用程序的内存 - 导致任何活动或挂起的连接丢失,例如。从iOS 7开始,Core Bluetooth支持保存中央和外围管理器对象的状态信息,并在应用程序启动时恢复该状态。您可以使用此功能支持涉及蓝牙设备的长期操作



但如果用户已终止您的应用程序,则无法重新连接。


I have a bluetooth device with a button on it. When clicking device's button, notification characteristic updating and my app shows local notification. It's that simple.

I connect bluetooth device in foreground as below :

   var restoreOptions = [CBPeripheralManagerOptionRestoreIdentifierKey: "customIdentifier"]
   centralManager.connect(peripheral, options: restoreOptions)

Then setted notification characteristic value is true :

   peripheral.setNotifyValue(true, for: notificationCharacteristic)

When notification characteristic update, app shows local notification :

   func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
       showLocalNotification()
   }

When notification characteristic is change , didUpdateValueFor is triggered and local notification shows. There is no problem in foreground and background. But when application is terminated by user, bluetooth device is disconnected and didUpdateValueFor not triggered. I want to re-connect again bluetooth device when characteristic change even app is terminated by user. How can I do this ? I hope I can tell you my problem. Thank you.

解决方案

You need to enable and program you App to handle Core-Bluetooth Background Mode

this way you will be able to reconnect to the device if your App is killed by the OS to free some resources.

"Even if your app supports one or both of the Core Bluetooth background execution modes, it can’t run forever. At some point, the system may need to terminate your app to free up memory for the current foreground app—causing any active or pending connections to be lost, for instance. As of iOS 7, Core Bluetooth supports saving state information for central and peripheral manager objects and restoring that state at app launch time. You can use this feature to support long-term actions involving Bluetooth devices"

but you cannot reconnect if the user has terminated your App.

这篇关于当应用程序被用户终止时重新连接蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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