Android 蓝牙:已连接?
有没有办法确定您是否已连接到蓝牙设备?
我的应用程序连接、发送/接收都很好。但是,我需要一种方法来查看我是否仍然处于连接状态,比如说……如果我走出范围并走回范围。
我注意到蓝牙套接字中没有 isConnected 函数,就像 TCP 中的函数一样...有没有办法查看您是否已连接,或者是否与您应该连接的设备进行通信?
Is there a way to figure out if you are connected to a Bluetooth Device?
I have my app connecting, sending/receiving just fine. However I need a way to see if I'm still connected say.. if I walk out of range and walk back into range.
I noticed there isnt an isConnected function in the Bluetooth Socket, like there is in the TCP stuff... is there a way to see if you are connected, or communicating to the device you're supposed to be connected with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能够解决这个问题的唯一方法是每秒发送一次“心跳”消息。如果没有通过,那么我认为蓝牙已断开连接。
The only way I was able to get around this was to send a "heartbeat" message once per second. If it doesn't go through, then I assume Bluetooth is disconnected.
发送尽可能少的数据,看看是否得到响应。如果你不这样做,那么你就没有联系。
Send the smallest amount of data that you can and see if you get a response. If you don't, well, you don't have a connection.
以下广播接收器值应该告诉您何时任何 BT 设备断开连接:
如果您对特定设备感兴趣,您可能应该实现一个 BluetoothProfile.ServiceListener 代理侦听器:
The following broadcast receiver values should tell you when any BT device is disconnected:
You should probably implement a BluetoothProfile.ServiceListener proxy listener if you are interested in a specific device: