【问题标题】:Android 12 BLE 在断开连接时始终返回状态 0
【发布时间】:2022-01-20 01:13:08
【问题描述】:

在使用 Android 12 设备进行测试时,只要我的蓝牙设备断开连接,无论我以编程方式断开连接还是设备超出范围,我都会收到状态 0。根据我对以前 Android 版本的理解,状态 0 以编程方式断开连接,状态 8 表示设备超出范围。

 return object : BluetoothGattCallback() {

            override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
                val name = gatt.device.name
                Log.i("onConnectionStateChange", name + "\t" + status + "\t" + newState)

                when (newState) {
                    BluetoothGatt.STATE_CONNECTED -> {
                        gatt.discoverServices()
                    }
                    BluetoothGatt.STATE_CONNECTING -> {

                    }
                    BluetoothGatt.STATE_DISCONNECTED -> {
                    }
                    BluetoothGatt.STATE_DISCONNECTING ->  {
                        // status 0 (Programmatically disconnected)
                        if (status == BluetoothGatt.GATT_SUCCESS) {
                            // Always going in here
                          ...
                        }
                        // Deivce went out of range
                        else if(status == 8){
                           // Never in here
                          ...
                        }
                    }

                }


            }

          override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
                    super.onServicesDiscovered(gatt, status)
            }
}

有没有人遇到过同样的问题并想出如何正确显示状态或找到另一种方法来确定设备是否超出范围或以编程方式断开连接?

【问题讨论】:

    标签: android bluetooth android-bluetooth android-12


    【解决方案1】:

    我确认您对 Android 12 所说的话:在 BLE 断开连接时,onConnectionStateChange() 中传递的状态参数的值为 0(即 BluetoothGatt.GATT_SUCCESS),无论断开连接是通过编程方式完成还是因为设备超出范围,或电气关闭。

    与以前版本的 android 相比,这是一个缺点,当设备超出范围(或被电气关闭)时,状态 8 (0x08 = GATT CONN TIMEOUT) 和以编程方式成功关闭连接时状态 0。

    【讨论】:

      【解决方案2】:

      不幸的是,在 Android 12 更新之后,我的 Pixel 3 上出现了相同的行为,这有点令人难过。他们有意或无意地改变了这一点。在任何情况下,除了“操作成功”时应使用 GATT_SUCCESS (0) 之外,任何地方都没有记录状态代码应包含的内容,没有定义“成功”的含义。我认为您应该向 AOSP 提交错误报告。

      【讨论】:

        猜你喜欢
        • 2011-05-07
        • 1970-01-01
        • 1970-01-01
        • 2013-02-09
        • 1970-01-01
        • 2016-11-15
        • 1970-01-01
        • 2017-10-29
        • 2016-10-05
        相关资源
        最近更新 更多