Android立即检测到蓝牙断开连接,最长2秒 [英] Android detect Bluetooth disconnect immediately Max 2 seconds

查看:1054
本文介绍了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日更新

我在您的悬赏评论以及您在下面的评论中都看到您要的是代码示例,但是对于我而言,如果不了解您正在通过套接字连接运行的应用程序协议一事,我很难提供一个示例.或者换句话说,您不理解或无法自己创建代码的第一段(即心跳建议)到底是什么?使用心跳的概念确实非常简单.您将在应用程序协议中定义表示心跳消息的某种消息类型.连接的一端会定期发送此心跳消息,例如每秒发送一次.连接的另一端检查是否每秒收到此心跳消息,并在两秒钟的超时后断开连接.没有比这更具体的信息了,因为我看不到您现有的代码,也不知道您当前正在通过套接字交换哪种消息.

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天全站免登陆