iOS如何在后台重新连接到BLE设备? [英] iOS how to reconnect to BLE device in background?

查看:143
本文介绍了iOS如何在后台重新连接到BLE设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多相关问题,但(显然)没有答案。所以...



我的iOS应用程序 在应用程序处于后台时从我的BLE设备获取更新。如果我失去了与BLE设备的联系,那么在 centralManager:didDisconnectPeripheral:我打电话 - [CBCentralManager cancelPeripheralConnection:] - - 否则我永远不会重新连接丢失的外围设备。然后我调用[(重新)调用 - [CBCentralManager scanForPeripheralsWithServices:options:]



记录显示 didDisconnectPeripheral 调用及其包含的调用在后台发生 。但是,只有当应用程序从后台唤醒时才会重新连接。



所以我可以在后台与所连接的BLE设备进行通信(是的!)但不是重新连接。这对我的应用程序非常重要,并且(人们会想到)其他应用程序。建议欢迎。

解决方案

保罗是对的,我不需要取消连接,而且我不需要重新扫描,我需要做的就是调用connectPeripheral。但是......我没做的是:

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

我为队列传递了nil,这意味着我的CBCentralManagerDelegate回调正在主线程上运行。 / p>

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. 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. Then I call [(re)call -[CBCentralManager scanForPeripheralsWithServices:options:].

Logging shows me that the didDisconnectPeripheral call, and its contained calls, are both happening while in background. 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. This is quite important to my app, and (one would think) to other apps. Suggestions welcome.

解决方案

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. 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.

这篇关于iOS如何在后台重新连接到BLE设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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