简体   繁体   English

应用程序在后台使用CoreBluetooth进行通信

[英]App communicates using CoreBluetooth in background

I have worked on an app which read heart rate from server kinds of bluetooth low energy HRM(Heart Rate Monitor)by bluetooth notification. 我开发了一个应用程序,该应用程序通过蓝牙通知从服务器种类的蓝牙低功耗HRM(心率监视器)读取心率。 And I have added the "App communicates using CoreBluetooth" into the app's "Background Modes". 我已经将“使用CoreBluetooth进行应用通信”添加到应用的“背景模式”中。 It works fine when the app is in foreground, but when the app is in background, one of the Heart Rate Monitors is always to be disconnect with the app in sever minutes. 当应用程序在前景中时,它可以正常工作,但是当应用程序在背景中时,其中一台心率监视器总是要在数分钟内与应用程序断开连接。

So I debugged the app and find that, this kind of HRM is always push notification irregularly.The interval between notifications is from 1 second to 20+ second. 因此我调试了该应用程序,发现这种HRM总是不规则地推送通知,通知之间的间隔从1秒到20+秒。 I know that when the app is in background, iOS will stop the bluetooth connection if it is too long between bluetooth notifications. 我知道当应用程序在后台运行时,如果两次蓝牙通知之间的间隔太长,iOS将停止蓝牙连接。

So my question is: Is it possible to keep the connection when the app is in background? 所以我的问题是:当应用程序处于后台时是否可以保持连接? Is reconnection the only way to solve my problem? 重新连接是解决我的问题的唯一方法吗?

Detecting the disconnection and reconnecting to the peripheral will make your app more robust as it will handle the case where the peripheral goes out of range and then returns in addition to the case you are seeing. 检测到断开连接并重新连接到外围设备将使您的应用程序更强大,因为它可以处理外围设备超出范围的情况,然后再返回您看到的情况。

When the peripheral is disconnected your centralManager:didDisconnectPeripheral method will be called on your delegate. 当外围设备断开连接时,将在您的委托上调用centralManager:didDisconnectPeripheral方法。 In this method you can call connectPeripheral:options again to re-establish the connection. 在此方法中,您可以再次调用connectPeripheral:options以重新建立连接。 iOS will either do this immediately if the device is still in range or automatically later, once the device comes back into range. 如果设备仍在范围内,iOS会立即执行此操作,或者一旦设备返回范围内,iOS会稍后自动执行此操作。

Once your centralManager:didConnectPeripheral method is called you can re-establish the characteristic monitoring. 调用centralManager:didConnectPeripheral方法后,您可以重新建立特征监视。

Edit Clarify that you can call connectPeripheral immediately 编辑说明您可以立即调用connectPeripheral

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM