IamsagarTu
IamsagarTu

Reputation: 88

(Android) Question with getting data from custom characteristic from BLE device

Similar to this problem Read/Write custom characteristic from BLE device

I wonder to know why I need to write a command code to the BLE device before getting data from it?

I have done it even don't know why. Here is part of my code (almost the same from the problem above) I have check the document from the supplier! Make sure the commands are right

byte [] arrayOfByte = new byte[8];
arrayOfByte [0] = (byte) 0x51;
arrayOfByte [1] = (byte) 0x26;
arrayOfByte [2] = (byte) 0x00;
arrayOfByte [3] = (byte) 0x00;
arrayOfByte [4] = (byte) 0x00;
arrayOfByte [5] = (byte) 0x01;
arrayOfByte [6] = (byte) 0xA3;
arrayOfByte [7] = ((byte)(arrayOfByte[0] + arrayOfByte[1] + arrayOfByte[2] + arrayOfByte[3] + arrayOfByte[4] + arrayOfByte[5] + arrayOfByte[6]& 0xFF ));
Char.setValue(arrayOfByte);
boolean result = gatt.writeCharacteristic(Char);

After that, it will trigger callback

onCharacteristicWrite

and

onCharacteristicChanged

In both of which, I have if/else statement to make sure the status is

BluetoothGatt.GATT_SUCCESS

when I trying to get the data from

onCharacteristicChanged

I get the value which I write to it.


What I have done is...

Get the data according to SIG document. However, I need to get the data from the custom characteristic. In order to get other data like previous data rather than the last one.


What I trying to do is (step by step)

  1. Scan the BLE device.
  2. Use Gatt connect with the device.
  3. Get the right characteristic (filter by uuid) and set indicator or notify property.
  4. Write the command (byte array) to the characteristic
  5. Get the data in callback function (Use characteristic.getvalue())
onCharacteristicChange

Thank a lot!

Upvotes: 0

Views: 201

Answers (1)

Sorenp
Sorenp

Reputation: 320

Have you tried to debug your project? Sorry to sound like a douche .. but seriously, try to follow it step by step and see what goes where.

Upvotes: 0

Related Questions