I've got problem with writing value to BLE characteristic. I send correctly array of bytes , however the major is not being changed. I've got few characteristics and there's no problem to read them. However there's a problem with writing to every of them new value. This is my "reading" part :
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
List<BluetoothGattService> services = gatt.getServices();
for (BluetoothGattService service : services) {
registerService(service);
characteristics = new ArrayList<BluetoothGattCharacteristic>();
characteristics.add(services.get(4).getCharacteristics().get(0));
characteristics.add(services.get(4).getCharacteristics().get(1));
characteristics.add(services.get(4).getCharacteristics().get(2));
characteristics.add(services.get(4).getCharacteristics().get(3));
characteristics.add(services.get(4).getCharacteristics().get(4));
characteristics.add(services.get(4).getCharacteristics().get(5));
characteristics.add(services.get(4).getCharacteristics().get(6));
characteristics.add(services.get(4).getCharacteristics().get(7));
characteristics.add(services.get(4).getCharacteristics().get(8));
characteristics.add(services.get(4).getCharacteristics().get(9));
characteristics.add(services.get(4).getCharacteristics().get(10));
characteristics.add(services.get(4).getCharacteristics().get(11));
characteristics.add(services.get(5).getCharacteristics().get(0));
characteristics.add(services.get(6).getCharacteristics().get(0));
if (Characteristics.PARAMETERS_SERVICE_UUID.equals(service.getUuid()))
registerParametersCharacteristics(characteristics);
Log.e(TAG, "onServicesDiscovered: " + service.getUuid() );
requesReadCharacteristics(gatt);
}
callback.connectedStateChanged(true);
} else
disconnect();
}
public void requesReadCharacteristics(BluetoothGatt gatt) {
if (characteristics.get(characteristics.size() - 1).getUuid().equals(Characteristics.TEMPERATURE_CHARACTERISTIC_UUID)) {
Log.e(TAG, "requesReadCharacteristics: TRUE");
}
if (characteristics.get(characteristics.size() - 1).getUuid().equals(Characteristics.ACCELEROMETER_CHARACTERISTIC_UUID)) {
Log.e(TAG, "requesReadCharacteristics: TRUE");
}
gatt.readCharacteristic(characteristics.get(characteristics.size() - 1));
}
To write new value to characteristic I've got method :
public void setMajor(int major) {
byte[] bytes = new byte[]{(byte) (major & 0xFF), (byte) ((major >> 8) & 0xFF)};
if (majorCharacteristic != null) {
BluetoothCommunicationManager.getInstance().add(new WriteCharacteristicCommand(majorCharacteristic, bluetoothGatt, bytes));
Log.e(TAG, "setMajor:" + Arrays.toString(bytes));
}
else
Log.e(TAG, "setMajor: NU" + Arrays.toString(bytes));
}
and a class to handle it :
public class WriteCharacteristicCommand implements BTLECommand {
private final BluetoothGatt gatt;
private final BluetoothGattCharacteristic characteristic;
private final byte[] value;
public WriteCharacteristicCommand(BluetoothGattCharacteristic characteristic, BluetoothGatt gatt, byte[] value) {
this.gatt = gatt;
this.characteristic = characteristic;
this.value = value;
}
@Override
public void process() {
characteristic.setValue(value);
gatt.writeCharacteristic(characteristic);
}
}
I found on logs, that when I set new value every characteristics are reading again ... twice, and than again fo the third time , and that's when old value is being set up again. Strange but that what's happen'. Any idea what I'm doing wrong ? Thanks in advance!