Android BLE characteristics getValue returns null
Asked Answered
E

3

6

I am trying to write text data to my BLE device. So , i am following Android Bluetooth GATT classes to do the task. But i found writing the text to the Characteristics is fine but while trying to retrieve the Characteristics value , it returns null.

MyCode :

public void writeCharacteristic(BluetoothGattCharacteristic characteristic,
                                String text) {

    String TAGS ="MyBeacon";

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAGS, "BluetoothAdapter not initialized");
        return;
    } else {
        Log.w(TAGS, "Writting ... ");
    }
    byte[] data = hexStringToByteArray(text);


    Log.w(TAGS, "Writting text = " + data);


    try {
        characteristic.setValue(URLEncoder.encode(text, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    boolean writeValue = mBluetoothGatt.writeCharacteristic(characteristic);

    Log.w(TAGS, "Writting Status = " + writeValue);

}

// Successfully onCharacteristicWrite also gets called //

   @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);

        String TAGS ="MyBeacon";

        String text = null;
        try {
            text = new String(characteristic.getValue(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Log.w(TAGS, "onCharacteristicWrite = " + text+" :: "+status);

    }

but while trying to read the Characteristics it returns null.

  for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {

                final byte[] data = gattCharacteristic.getValue(); // returns null

                  if (data != null && data.length > 0) {

                     Log.d("MyBeacon", " Read Data ")

                  } else {

                     Log.d("MyBeacon", " Data is null")
                  }

      }

MyBeacon

Also check the issue in other thread too.

Please help me out , suggest me some solution to write and read data successfully to my Beacon.

Erbil answered 9/3, 2016 at 21:49 Comment(2)
To read the characteristic, did you try mBluetoothGatt.readCharacteristic(characteristic) and then catch the onCharacteristicRead() callback?Macmullin
yes i did , but onCharacteristicRead not called.Erbil
E
6

Syntax should be as follows,

mBluetoothGatt.readCharacteristic(characteristic);

Reading characteristics: You can read the characteristic using mBluetoothGatt.readCharacteristic(characteristic);

You can have to read the characteristic's descriptor as follows,

mBluetoothGatt.readDescriptor(ccc);

Once you read it, it should return data by calling the onDescriptorRead callback. Here you can set up (subscribe) to the charactersitic through either notification or indication by calling:

mBluetoothGatt.setCharacteristicNotification(characteristic, true)

once it returns true you will need to write to the descriptor again (the value of notification or indication)

BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

//clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(clientConfig);

Once this is done you will get notifications through onCharacteristicChanged callback every time the characteristic changes.

Do update me , if you have any problems while implementing,

Excreta answered 10/3, 2016 at 7:39 Comment(8)
Thank for the reply , i just want to know , the writeDescriptor will be written in onServiceDiscovered callback.Erbil
can you also please tell me , what is characteristic.getDescriptor(CCC); and what is "CCC".Erbil
CCC - Client Characteristic Configuration descriptorExcreta
it defines how the characteristic may be configured by a specific clientExcreta
characteristic.getDescriptor(CCC) returns null , it's seem CCC not supporting to my BLE device. So how do i do it?Erbil
try any other mobile that supports CCC.Excreta
have you ever tried getting the descriptors that are supported by your mobile? If not tried doing so and change your implementation according to the descriptors that you've.Excreta
can you please check this issue #35938029Erbil
G
2

I faced a similar issue where the characteristic.getValue returns Null. I was following exactly what is mentioned in the BLE Gatt documentation, and other blogs, but still the issue persisted until finally I understood what I was doing wrong.

At client device end, we setValue into the characteristic that we are interested in using

gatt.WriteCharacteristic(characteristic.setValue("Hello"));

At Server end, the request is received onto the onCharacteristicWriteRequest(....) callback. Generally we expect the value that we set at client end to be carried by the characteristic parameter but we observe the characteristic.getValue() is null.

Where in the same callback we also have another parameter by name "Value" which actually carries the characteristic value we set at Client end. Please refer this parameter and this should solve the problem.

Groningen answered 1/8, 2018 at 20:42 Comment(0)
B
0

Did you read it too early? It should be read after onCharacteristicWrite() has been called.

Bodice answered 11/3, 2016 at 6:37 Comment(4)
no , after onCharacteristicWrite only , i am calling onCharacteristicReadErbil
sorry, I made a mistake. gattCharacteristic.getValue() will not read the value form your device, it just return "the stored value for this characteristic". So it should not be null. BTW, you have called getValue() in onCharacteristicWrite(), so what is the message about Log.w(TAGS, "onCharacteristicWrite = " + text+" :: "+status);?Bodice
the Log is to read the stored values of the Characteristics. "text" is the variable which contains the Text data of the byte valueErbil
I think "final byte[] data = gattCharacteristic.getValue(); // returns null" will get the same results as "text = new String(characteristic.getValue(), "UTF-8");"... Or, what is the definition of gattCharacteristic.getValue()?Bodice

© 2022 - 2024 — McMap. All rights reserved.