@Test
public void writeCharacteristicTest() throws Exception {

    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"), PROPERTY_WRITE,0);
    when(gatt.writeCharacteristic(characteristic)).thenReturn(true);
    peripheral.writeCharacteristic(characteristic, new byte[]{0}, WRITE_TYPE_DEFAULT);
    verify(gatt).writeCharacteristic(any(BluetoothGattCharacteristic.class));
    byte[] originalByteArray = new byte[]{0x01};
    characteristic.setValue(originalByteArray);
    callback.onCharacteristicWrite(gatt, characteristic, 0);
    ArgumentCaptor<BluetoothPeripheral> captorPeripheral = ArgumentCaptor.forClass(BluetoothPeripheral.class);
    ArgumentCaptor<byte[]> captorValue = ArgumentCaptor.forClass(byte[].class);
    ArgumentCaptor<BluetoothGattCharacteristic> captorCharacteristic = ArgumentCaptor.forClass(BluetoothGattCharacteristic.class);
    ArgumentCaptor<Integer> captorStatus = ArgumentCaptor.forClass(Integer.class);
    verify(peripheralCallback).onCharacteristicWrite(captorPeripheral.capture(), captorValue.capture(), captorCharacteristic.capture(), captorStatus.capture());
    byte[] value = captorValue.getValue();
    assertEquals(0, value[0]);  // Check if original value is returned and not the one in the characteristic
    assertEquals(peripheral, captorPeripheral.getValue());
    assertEquals(characteristic, captorCharacteristic.getValue());
    assertEquals(GATT_SUCCESS, (int) captorStatus.getValue() );
}