Android BLE处理断开连接超时 [英] Android BLE handling disconnect timeout

查看:233
本文介绍了Android BLE处理断开连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用博客帖子,例如这一个.我将假定这是一般行为(而不是我这边的错误).常见的答案似乎可以起到以下作用:更改BLE固件,使其主动断开连接."

Referencing blog posts like this one, and SO questions like this one. I am going to assume that this is a general behavior (and not a bug on my side). The common answer seems to be something to the effect of: "Change the BLE firmware so it actively disconnects."

一个未得到很好解决的问题是Android应用如何处理必须很常见的情况?由于范围"(即I.E.),连接意外丢失.无线电信号强度.

The question which is not well addressed is how Android apps handle what must be a very common occurrence? Connection is lost unexpectedly due to "range", I.E. radio signal strength.

有没有一种方法可以在断开连接时立即通知应用程序?

Is there a way for an app to be notified "immediately" on the loss of connection?

似乎所有应用程序都只是坐在那里等待20秒之类的时间(这被称为核心OS超时值)似乎是不现实的.即使我在iOS上的同等应用程序知道连接丢失的时间少于1秒,这也是我们所有人应该做的吗?

It does seem unrealistic that all apps just sit there for something like the 20 seconds which is mentioned as a core OS timeout value. Is that what we should all be doing even though my equivalent app on iOS knows about the loss of connections in less than 1s?

BLE设备的一种常见类型是查找我的密钥"类型.它们中的许多功能都可以在您无意间离开键"时提醒用户.我假设这使用连接断开来指示您走得太远.对吧?

One common type of BLE device is the "Find my keys" type. Many of them have a feature to alert the user when you leave the "keys" unintentionally. I assume that this uses the connection going down as an indicator of you walking too far away. Right?

应该通知您的应用有关设备上某个特性的值更改.例如,这将是您跨越某个阈值的任何类型的传感器数据.我可以想到很多例子,您想立即知道您的传感器"超出范围了.

Your app is supposed to be notified of of value changes from a characteristic on the device. This would be any kind of sensor data where you have some threshold is being crossed for example. I can think of plenty of examples where you'd want to know immediately that your "sensor" is out or range.

我已经看到一种解决方法,它可以不断监控BLE设备的RSSI,但似乎会消耗大量电池.同样,也可以使用写入特征的任何失败(通常会成功),而电池寿命又要付出代价.

I have seen one workaround amounting to constantly monitoring the RSSI to the BLE device but that seems like it would eat up a lot of battery. Similarly any failure to write to a characteristic (that normally succeeds) could also be used, again with battery life paying a price.

某种方法可以为这些问题提供一个明确的答案,似乎是一种很好的资源.

Something approaching a definitive answer to this questions seems like a good resource.

推荐答案

在最新版本的Android中,他们将默认超时时间降低到5秒,这当然更好.

In latest versions of Android they lowered the default timeout to 5 seconds, which is of course much better.

我想大多数外设都会发送自己的连接更新请求,在其中可以设置不同的超时值,从而解决了该问题(连接建立到连接更新完成之间的时间间隔很小).

I guess most peripherals send their own Connection Update request where they can set a different timeout value, which solves that issue (except for the small time between connection setup and until the connection update has gone through).

根据我的说法,所谓的超出范围"检测器应用程序非常没用,而且很难完善,因为BLE设备有时可能会暂时断开连接,即使它们在有效范围之内.

So called "out of range" detector apps are according to me pretty useless and hard to make good because BLE devices will probably sometimes temporarily disconnect anyway, even though they are in range.

对于您的实际问题,是否有可能立即"收到连接丢失的通知,基本答案是否",因为蓝牙控制器不会向主CPU发送任何数据包丢失的信息(这就是全部)超时的目的,这样您就可以丢包).但是,您当然可以尝试轮询rssi或设置一个流程,在该流程中,外围设备每隔固定间隔发送一次通知,然后您可以在您的应用中检测经过一定时间后是否没有收到通知.但是在这种情况下,使用连接参数更新过程设置自己的超时时间(从外围设备)会更明智.

For your actual question if it's possible to "immediately" get notified on the loss of the connection, the basic answer is no, because the Bluetooth controller doesn't send anything to the main cpu that packets were lost (that's the whole purpose of having a timeout so you are allowed to have packet losses). But you can of course try to poll the rssi or set up a flow where the peripheral every constant interval sends a notification and then you can in your app detect if you don't get the notification after some amount of time. But in this case it's smarter to just set your own timeout (from the peripheral) using the connection parameter update procedure.

这篇关于Android BLE处理断开连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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