简体   繁体   English

告诉两个Android蓝牙设备是否不再实际连接到蓝牙插座

[英]tell if two android bluetooth devices are no longer actually connected to bluetooth socket

in my app I have a service that runs a thread that reads/writes to a BluetoothSocket . 在我的应用程序中,我有一项服务,该服务运行读取/写入BluetoothSocket的线程。 If i explicitly close the connection on one device, the other device's thread throws an IOException immediately. 如果我显式关闭一个设备上的连接,则另一设备的线程会立即引发IOException This is ideal because I can tell when the connection has been lost. 这很理想,因为我可以告诉您何时断开连接。

However, when the devices get extremely far apart (way past the range of bluetooth) the thread does not seem to notice. 但是,当设备相距太远(超过蓝牙范围)时,似乎看不到线程。 It throws no exceptions, and seems to be in its normal connected state. 它不会引发任何异常,并且似乎处于其正常连接状态。 The socket is only written to when an sms is received. 仅在收到短信时才将套接字写入。 So perhaps when no sms has been received, no attempts to read/write to the socket have occured, and therefore no exceptions have yet been thrown. 因此,也许在没有收到任何短信的情况下,没有发生任何尝试读写套接字的消息,因此还没有引发异常。

How can I tell when to close the connection if the devices have become too far apart? 如果设备之间的距离太远,该如何判断何时关闭连接? Perhaps using a timer to periodically attempt to write to the socket would force an exception to be thrown? 也许使用计时器定期尝试写入套接字会强制引发异常?

Bluetooth tries to maintain connections even when far apart by increasing its transmitter power. 蓝牙试图通过增加其发射器功率来保持连接,即使相距很远。 Your best bet is to make a timer to see if they are no longer communicating. 最好的选择是安排一个计时器,以查看他们是否不再通信。

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

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