writeDescriptor(BluetoothGattDescriptor!):布尔值'已弃用。在 Java 中弃用

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

我正在与 Bluetooth gatt 一起工作

minSdk 21
targetSdk 33

我看到

writeDescriptor
在 sdk 33 中被弃用了。所以我这样做是为了将代码包装在 SDK 版本检查中

 gatt.setCharacteristicNotification(characteristic, true)
 val descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"))
  if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.TIRAMISU) {
        descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
        gatt.writeDescriptor(descriptor)
  } else {
        gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)
  }

在我还在

gatt.writeDescriptor(descriptor)
在这里收到警告之后

我还需要使用 2 种不同类型的

onCharacteristicChanged

在 SDK 33 中

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

sdk 33以下

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

那么解决这个问题的最佳方法是什么?

android kotlin bluetooth-lowenergy android-bluetooth android-ble
1个回答
0
投票

是的,您需要在 SDK 版本检查中包装对

writeDecriptor()
的调用,以在 Android API 上调用此方法的弃用版本 < 33.

您还必须实现两个版本的

onCharacteristicChanged()
回调以接收两个 Android API < 33 and Android API >= 33 上的特性更改。

我刚刚测试了它,它以这种方式正常工作。

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