1

I am working Bluetooth gatt with

minSdk 21
targetSdk 33

I see the writeDescriptor is deprecated in sdk 33. So I did this to wrap the code in SDK version check

 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)
  }

After I am still getting warning in gatt.writeDescriptor(descriptor) in here

enter image description here

Also Do I need to to use 2 different types of onCharacteristicChanged i.e.

In sdk 33

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

and below sdk 33

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

So what is the best way to solve this problem?

Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127

1 Answers1

1

Yes you need to wrap the call to writeDecriptor() in a SDK version check to call the deprecated version of this method on Android API < 33.

You also have to implement both versions of onCharacteristicChanged() callback to receive characteristic changes on both Android API < 33 and Android API >= 33.

I just tested it and it works properly this way.

cjosepha
  • 193
  • 1
  • 1
  • 12