In Android onDescriptorWrite method is returning status code as 4, What does it mean?

263 Views Asked by At

I am developing an Android application that can connect and communicate with BLE device, Connection and data sending is working fine.. But when i am writing descriptor value to gatt object I am getting status code as 4 in onDescriptorWrite Callback. As per my understanding status code zero is GATT_SUCCESS. I dont know what does status code 4 means?

Here is my code

val descriptor = characteristicDeviceToApp?.getDescriptor(UUID.fromString(DESC_UUID))
descriptor?.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
val descriptorResult = gatt.writeDescriptor(descriptor)

I am getting below callback with status as 4

override fun onDescriptorWrite(
            gatt: BluetoothGatt?,
            descriptor: BluetoothGattDescriptor?,
            status: Int
        ) {
        }

Note: I am getting descriptorResult as true.

I have tried referring official documents..Nothing is helpful..Additionally the same code is returning status as 0 (GATT_SUCCESS) for some other BLE device

1

There are 1 best solutions below

5
Kozmotronik On

It is because the PDU (Protocol Data Unit) size sent from Android. The error code 4 is defined as INVALID_PDU in the API source code (see Google's API source code). Different devices may have different PDU sizes depending on their specific BLE version.
So if the PDU sent from your Android device (client) is wrong length, then the peripheral (server) device will respond with this error code according to the BLE Core Specification v4.0, section 3.3 ATTRIBUTE PDU.

If the server receives an invalid request–for example, the PDU is the wrong length–then the server shall respond with the Error Response with the Error Code «Invalid PDU», with the Attribute Handle In Error set to 0x0000.

However your code doesn't seem to have a problem. This error may have occured because of some incompabilities like unmatching PDU size in low level APIs of both devices.

Well what to do to handle this situation?
The answer is it depends.

  • Try resizing your data that you write
  • Retry with the same configuration
  • Try restarting the Android's bluetooth hardware
  • Try restarting Android
  • Try your code using another Android device