Android立即检测蓝牙断开最多2秒 [英] Android detect Bluetooth disconnect immediately Max 2 seconds

查看:55
本文介绍了Android立即检测蓝牙断开最多2秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来在蓝牙设备断开连接后立即检测它(最多 2 秒),通常是在设备太远"的情况下或设备电池没电了.目前我可以通过获取 BluetoothDevice.ACTION_ACL_DISCONNECTED 使用 BroadcastReceiver 检测它,但它需要大约 16 到 20 秒才能触发.

I'm looking for a way to detect the disconnection of a Bluetooth device immediately after it has happened (2 second max), typically in a "device too far" scenario or Device battery is dead. Currently I can detect it with a BroadcastReceiver by getting a BluetoothDevice.ACTION_ACL_DISCONNECTED, but it takes about 16 to 20 seconds to fire.

  1. 有没有办法在最多 2 秒内得到通知.
  2. 我使用了 BroadcatReceiver,但它的速度不够快,无法在 2 秒内收到警报,所以有没有其他方法可以快速获取蓝牙断开连接的通知.
  3. 我使用这个 createRfcommSocketToServiceRecord(UUID);连接配对设备,我必须使用 UUID 使用它.

我访问了很多关于这个问题的链接,但没有一个符合我的需求.这就是为什么任何帮助都会受到赞赏的原因.

I have visited a lot of links regarding this issue, but no one matches with my needs.that's why any help would be appreciated.

谢谢.

推荐答案

我认为,您可以快速(在两秒内)可靠地感知连接丢失的唯一方法是通过您自己的应用程序协议,该协议通过蓝牙连接使用.例如,您的应用程序协议可能会实现每 500 毫秒发生一次的心跳.如果您在两秒内没有看到心跳,那么您可以触发自己的事件.

I think the only way you can reliably sense loss of connection quickly (within two seconds) is via your own application protocol that you use over the Bluetooth connection. For example, your application protocol might implement a heartbeat that occurs every 500ms. If you don't see a heartbeat within two seconds then you could trigger your own event.

蓝牙是一种基于套接字的流协议,旨在通过不可靠的介质(即无线电)工作,因此必须容忍数据包中的错误(或丢失).出于这个原因,您的蓝牙堆栈在声明它已放弃并断开设备连接之前将花费 2 秒以上的时间,如您所见.

Bluetooth is a socket-based stream protocol that is designed to work over an unreliable medium (i.e. radio), and as such has to tolerate errors in (or loss of) packets. For this reason it will take significantly more than 2 seconds before your Bluetooth stack declares it has given up and disconnected the device, as you have found.

我在 Play 上有一个应用程序,该应用程序旨在通过蓝牙与汽车 ECU 通信,我检测断开连接的策略与我在第一段中的建议完全相同.

I have an application on Play which is designed to talk with an automotive ECU via Bluetooth and my strategy for sensing disconnection is exactly as I suggested in my first paragraph.

6 月 20 日 14 日更新

我在您的赏金评论和下面的评论中看到您要求提供一个代码示例,但我很难在不了解您通过套接字连接运行的应用程序协议的情况下提供一个代码示例.或者换句话说,我的第一段(即心跳建议)究竟是什么你不理解或无法为自己创建代码?使用心跳的概念真的很简单.您将在应用程序协议中定义某种消息类型来表示心跳消息.连接的一端定期发送此心跳消息,例如每隔一秒.连接的另一端每隔一秒左右检查是否收到此心跳消息,并在两秒超时后断开连接.没有比这更具体的了,因为我看不到您现有的代码,而且我不知道您目前正在通过套接字交换什么样的消息.

I see in your bounty comment and also your comment below that you're asking for a code example, but it's kind of difficult for me to provide one without knowing anything about the application protocol that you're running over the socket connection. Or to put it another way, what exactly is it about my first paragraph (i.e. the heartbeat suggestion) that you do not understand or cannot create code for yourself? The concept of using a heartbeat really is quite simple. You would define a certain message type in your application protocol that represents a heartbeat message. One end of the connection sends this heartbeat message periodically, say every one second. The other end of the connection checks that this heartbeat message is received every second or so and drops the connection after a two-second time-out. It is impossible to be any more specific than that, because I can't see your existing code and I don't know what kind of messages you are currently exchanging over the socket.

这篇关于Android立即检测蓝牙断开最多2秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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