我的 BluetoothGattCallback() onCharacteristicChanged 在 Android 13 以下不起作用

问题描述 投票:0回答:2

我正在尝试使用

BLE
设备接收数据,但它不起作用。我以这种方式配置我的
onCharacteristicChanged

    @SuppressLint("MissingPermission")
    override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {

        bluetoothGatt.requestMtu(517)
    }

    override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) {
        val characteristic = findCharacteristics()

        enableNotification(characteristic)
    }

    fun ByteArray.toHexString(): String =
        joinToString(separator = "", prefix = "") { String.format("%02X", it) }

    override fun onCharacteristicChanged(
        gatt: BluetoothGatt,
        characteristic: BluetoothGattCharacteristic,
        value: ByteArray
    ) {
        Log.d("CHAR", value.toHexString())
    }

仅适用于 Android 13,当我在 Android 11 中运行该应用程序时,

onCharacteristicChanged
不会触发。我检查的权限:

private fun checkPermissions() {
    when {
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            requestPermissions.launch(
                arrayOf(
                    Manifest.permission.BLUETOOTH_SCAN,
                    Manifest.permission.BLUETOOTH_CONNECT
                )
            )
        }
        Build.VERSION.SDK_INT < Build.VERSION_CODES.S -> {
            requestPermissions.launch(
                arrayOf(
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                )
            )
        }
    }
}

private val requestPermissions =
    registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
        permissions.forEach { (key, value) ->

            when (key) {
                Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION -> {
                    if (!value) {
                        ActivityCompat.requestPermissions(
                            requireActivity(),
                            arrayOf(
                                Manifest.permission.ACCESS_COARSE_LOCATION,
                                Manifest.permission.ACCESS_FINE_LOCATION
                            ),
                            1
                        )
                    }
                }
                Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT -> {
                    if (!value) {
                        ActivityCompat.requestPermissions(
                            requireActivity(),
                            arrayOf(
                                Manifest.permission.BLUETOOTH_SCAN,
                                Manifest.permission.BLUETOOTH_CONNECT
                            ),
                            1
                        )
                    }
                }
            }
        }
    }

当它在 Android 11 上运行时,日志表明一切正常:

D/BluetoothGatt: onConnectionUpdated() - Device=7C:9E:BD:07:3D:82 interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=7C:9E:BD:07:3D:82 Status=0
D/BluetoothGatt: configureMTU() - device: 7C:9E:BD:07:3D:82 mtu: 517
D/BluetoothGatt: onConfigureMTU() - Device=7C:9E:BD:07:3D:82 mtu=517 status=0
D/BluetoothGatt: setCharacteristicNotification() - uuid: 00001103-0000-1000-8000-00805f9b34fb enable: true
android kotlin bluetooth bluetooth-lowenergy
2个回答
0
投票

我认为您必须改用此回调:https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback#onCharacteristicChanged(android.bluetooth.BluetoothGatt,%20android.bluetooth.BluetoothGattCharacteristic)。您尝试在值作为参数传递的地方使用的重载已添加到 Android 13 中,因此不适用于旧设备。


0
投票

已解决

问题:

override fun onCharacteristicChanged(
    gatt: BluetoothGatt,
    characteristic: BluetoothGattCharacteristic,
    value: ByteArray
)

该版本的构造函数仅在设备版本为android 12以上时调用

解决方案:

override fun onCharacteristicChanged(
    gatt: BluetoothGatt,
    characteristic: BluetoothGattCharacteristic
)

添加它的旧版本代码工作正常

© www.soinside.com 2019 - 2024. All rights reserved.