Android BLE GATT已断开与设备已断开 [英] Android BLE GATT Disconnected vs. Device Disconnected

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

问题描述

我正在使用与自定义蓝牙设备通信的Android应用程序.调用 BluetoothGatt.Disconnect()之后,我看到调用了 OnConnectionStateChange 回调,并且新状态为 Disconnected ,但是,似乎在这种情况发生与实际断开设备本身之间存在一定的滞后时间.例如,如果我用已连接的设备调用BluetoothManager.GetConnectionState(...),它仍将返回Connected.有时, GetConnectionState 返回 Disconnected 可能要花费几秒钟的时间.这正常吗?我的应用程序可能做错了什么可能导致这种情况吗?例如从非UI界面断开连接,或类似的东西?或者,可能是物理蓝牙设备本身未正确处理断开连接,并且可能未及时完成断开事件?

I'm working on an Android app that communicates with a custom bluetooth device. After calling BluetoothGatt.Disconnect() I am seeing that the OnConnectionStateChange callback is called, and the new state is Disconnected, however, there seems to be a lag between when that happens and when the Device itself is actually disconnected. For example, if I call BluetoothManager.GetConnectionState(...) with the device that was connected, it still returns Connected. Sometimes it takes several seconds before GetConnectionState returns Disconnected. Is this normal? Is it possible that I am doing something wrong in my application that could be causing this? e.g. disconnecting from a non-UI tread, or something like that? Or, is it possible that the physical bluetooth device itself is not handling the disconnect properly and maybe not completing the disconnect event promptly?

推荐答案

调用"disconnect()"时,仅断开客户端对象(BluetoothGatt对象)的连接.您可以将多个BluetoothGatt对象连接到同一物理设备.多个应用程序也可以将自己的BluetoothGatt对象连接到同一设备.

When you call "disconnect()" you only disconnect your client object (BluetoothGatt object). You can have multiple BluetoothGatt objects connected to the same physical device. Multiple apps can also have own BluetoothGatt objects connected to the same device.

一旦您调用"disconnect()",该请求就会在系统中的蓝牙堆栈中进行处理,并在处理完请求后立即在您的应用程序中调用onConnectionStateChange回调.但是,除非所有其他客户端都断开连接,否则它不会断开链接.较新版本的Android也将物理断开连接延迟了几秒钟(不确定原因).同样,一旦断开请求已发送至蓝牙控制器,由于远程设备需要确认断开(或超时),因此可能需要花费一些时间才能真正断开连接.默认超时时间为20秒,直到最近在最新的Android版本中更改为5秒为止.

As soon as you call "disconnect()" the request is processed in the Bluetooth stack in the system and it immediately then calls the onConnectionStateChange callback in your app when it has completed processing the request. However, it will not disconnect the link until all other clients have disconnected. Newer versions of Android also delay the physical disconnection a few seconds (not sure why). Also, once the disconnect request has been sent to the Bluetooth controller it may take some time to actually disconnect since the remote device needs to acknowledge the disconnection (or time out). The default time out was 20 seconds until it was recently changed to 5 seconds in the latest Android version.

这篇关于Android BLE GATT已断开与设备已断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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