如何检测Android蓝牙connectGatt()自动连接完成

问题描述

(*** 编辑 #2:尝试连接多个设备 ***)

我正在使用 BluetoothDevice.connectGatt() 并将自动连接设置为 true 以连接到多个设备:

bluetoothDevice1.connectGatt(context,true,BLGattCallback(context),BluetoothDevice.TRANSPORT_LE)
bluetoothDevice2.connectGatt(context,BluetoothDevice.TRANSPORT_LE)

(*** 编辑 #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,newState)
        BLBluetoothManager.connectionStateChange.postValue(connectionStateChange)
    }

(结束编辑 #1)

使用这种方法,我必须在执行下一个 connectGat() 之前使用延迟,否则前一个会中止。为什么是这样?我认为它不应该等待,只要连接就回电...

是否有回调让您知道第一个 connectGatt 何时完成,以便我不需要临时延迟?

*** 编辑 #3 ***

我所说的“第一次 connectGatt 已完成”并不是说建立了连接,只是我现在可以执行下一个命令而不会中止前一个命令。

(结束编辑 #3)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)