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)
- Scan the BLE device.
- Use Gatt connect with the device.
- Get the right characteristic (filter by uuid) and set indicator or notify property.
- Write the command (byte array) to the characteristic
- Get the data in callback function (Use characteristic.getvalue())
onCharacteristicChange
Thank a lot!