@Test
public void writeDescriptor() {

    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);
    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_INDICATE,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002903-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);
    when(gatt.getServices()).thenReturn(Arrays.asList(service));
    byte[] originalByteArray = new byte[]{0x01};
    peripheral.writeDescriptor(descriptor, originalByteArray);
    verify(gatt).writeDescriptor(descriptor);
    callback.onDescriptorWrite(gatt, descriptor, 0);
    ArgumentCaptor<BluetoothPeripheral> captorPeripheral = ArgumentCaptor.forClass(BluetoothPeripheral.class);
    ArgumentCaptor<byte[]> captorValue = ArgumentCaptor.forClass(byte[].class);
    ArgumentCaptor<BluetoothGattDescriptor> captorDescriptor = ArgumentCaptor.forClass(BluetoothGattDescriptor.class);
    ArgumentCaptor<Integer> captorStatus = ArgumentCaptor.forClass(Integer.class);
    verify(peripheralCallback).onDescriptorWrite(captorPeripheral.capture(), captorValue.capture(), captorDescriptor.capture(), captorStatus.capture());
    byte[] value = captorValue.getValue();
    assertEquals(0x01, value[0]);
    assertNotEquals(value, originalByteArray);   // Check if the byte array has been copied
    assertEquals(peripheral, captorPeripheral.getValue());
    assertEquals(descriptor, captorDescriptor.getValue());
    assertEquals(GATT_SUCCESS, (int) captorStatus.getValue() );
}