android - 如何检测 Android 蓝牙 connectGatt() 自动连接完成
问题描述
(*** 编辑 #2:尝试连接多个设备***)
我正在使用自动连接设置为 true 的 BluetoothDevice.connectGatt() 来连接到多个设备:
bluetoothDevice1.connectGatt(context, true, BLGattCallback(context), BluetoothDevice.TRANSPORT_LE)
bluetoothDevice2.connectGatt(context, true, BLGattCallback(context), BluetoothDevice.TRANSPORT_LE)
(*** Edit #1 ***) 我在 BluetoothGattCallback() 中使用 onConnectionStateChange():
internal class BLGattCallback(val context: BTLeashService) : BluetoothGattCallback() // Todo: Convert to LiveData
{
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int)
{
super.onConnectionStateChange(gatt, status, newState)
val connectionStateChange = BLBluetoothManager.ConnectionStateChange(gatt, status, newState)
BLBluetoothManager.connectionStateChange.postValue(connectionStateChange)
}
(结束编辑#1)
使用这种方法,我必须在执行下一个 connectGat() 之前使用延迟,否则前一个会中止。为什么是这样?我认为它不应该等待,只要它连接就回电......
当第一个 connectGatt 完成时是否有回调让您知道,这样我就不需要临时延迟?
*** 编辑 #3 ***
“第一个 connectGatt 完成”并不是说建立了连接,只是我现在可以执行下一个命令而不会中止上一个命令。
(结束编辑#3)
解决方案
推荐阅读
- elasticsearch - 如何使用可选参数构造 NEST 查询?
- angular - 如何绑定并获取使用 angular4 datepicker 动态生成的 datepicker 的值?
- android - onRequestPermissionsResult 正在片段下编译
- react-native - 获取 someUrl.html 在 React Native / Expo 中不起作用?
- haskell - 当“修改”具有相同值的字段时,无法将类型“HandlerSite m0”与“HandlerSite m”匹配
- python - 如何将编辑的txt文件保存到新的txt文件中?
- java - 在这种情况下仅调整图像(png)的大小时,BitmapFactory 真的有必要吗?
- python - 如何获取所有可能响应模式的容器名称?
- ios - Facebook登录按钮不会立即更改为注销按钮在swift中登录后立即
- java - 如何在 Java 运行时生成现有类的字节码?