简体   繁体   English

iOS如何在后台重新连接到BLE设备?

[英]iOS how to reconnect to BLE device in background?

There are many related questions but (apparently) no answers. 有许多相关的问题,但(显然)没有答案。 SO... 所以...

My iOS app does get updates from my BLE device while the app is in background. 当应用程序处于后台时,我的iOS应用程序确实从我的BLE设备获取更新。 If I lose touch with the BLE device, then in centralManager:didDisconnectPeripheral: I call - [CBCentralManager cancelPeripheralConnection:] -- otherwise I will never reconnect to the lost peripheral. 如果我失去了与BLE设备的联系,那么在centralManager:didDisconnectPeripheral:我打电话 - [CBCentralManager cancelPeripheralConnection:] - 否则我将永远不会重新连接到丢失的外围设备。 Then I call [(re)call - [CBCentralManager scanForPeripheralsWithServices:options:] . 然后我调用[(重新)调用 - [CBCentralManager scanForPeripheralsWithServices:options:]

Logging shows me that the didDisconnectPeripheral call, and its contained calls, are both happening while in background . 记录显示didDisconnectPeripheral调用及其包含的调用都在后台发生。 However, the reconnect only happens when the app wakes up from background. 但是,仅当应用程序从后台唤醒时才会重新连接。

So I am able to communicate with the connected BLE device while in background (yay!) but not to reconnect. 所以我能够在后台(连接!)与连接的BLE设备进行通信,但不能重新连接。 This is quite important to my app, and (one would think) to other apps. 这对我的应用程序非常重要,并且(人们会想到)其他应用程序。 Suggestions welcome. 建议欢迎。

You don't need to cancel the connection - it is already disconnected You don't need to rescan for the peripheral - you have already identified your peripheral. 您无需取消连接 - 它已经断开连接您无需重新扫描外围设备 - 您已经识别了外围设备。

In didDisconnectPeripheral you can simply call didDisconnectPeripheral您可以简单地调用

[central connectPeripheral:peripheral options:nil];

Core Bluetooth will reconnect once the peripheral is visible again 一旦外围设备再次可见,核心蓝牙将重新连接

A complete sample is here - https://github.com/paulw11/BTBackground 完整的示例在这里 - https://github.com/paulw11/BTBackground

Paul was correct, that I did not need to cancel the connection, and that I did not need to rescan, and that all I needed to do was call connectPeripheral. Paul是正确的,我不需要取消连接,而且我不需要重新扫描, 我需要做的就是调用connectPeripheral。 BUT... what I was not doing was this: 但是......我没做的是这样的:

_manager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)];

I was passing nil for the queue, which meant my CBCentralManagerDelegate callbacks were running on the main thread. 我为队列传递了nil,这意味着我的CBCentralManagerDelegate回调正在主线程上运行。

I know you probably have everything figured out already. 我知道你可能已经弄明白了。 I my self stumbled on this issue as well. 我自己偶然发现了这个问题。 Doing the following alone would not completely solve the problem (at least for me): 单独执行以下操作并不能完全解决问题(至少对我而言):
[central connectPeripheral:peripheral options:nil];

Instead of "nil" for the options, you will need to provide "options" in the above method. 您需要在上述方法中提供“选项”,而不是选项的“nil”。 There are other things you need to set up as well (such as info.plist for your app). 您还需要设置其他内容(例如应用的info.plist)。 After reading Apple's instruction on how to enable background bluetooth operation for an App. 阅读Apple关于如何为App启用后台蓝牙操作的说明。 I was able to get it working and receiving events for connect,disconnect,update etc., even scan can work in the back ground. 我能够让它工作并接收连接,断开连接,更新等事件,甚至扫描可以在后台工作。 Not to repeat everything what the Apple document has written about, you can take a look at the following link: 不要重复Apple文档所写的所有内容,您可以查看以下链接:
Core Bluetooth Background Processing for iOS Apps 适用于iOS应用的核心蓝牙背景处理

Hope this helps. 希望这可以帮助。

在“功能” - >“背景模式”选项卡中,选择“用户蓝牙LE配件”和“充当蓝牙LE配件”。我不确定它是否可以工作,但您可以尝试一下。

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

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