@Test public void testOnCharacteristicWriteFail() throws IOException { when(gatt.getService(eq(serviceUUID))).thenReturn(gattService); when(gattService.getCharacteristic(characteristicUUID)).thenReturn(gattCharacteristic); when(gatt.writeCharacteristic(eq(gattCharacteristic))).thenReturn(true); InputSource inputSource = Mockito.mock(InputSource.class); when(inputSource.nextChunk()).thenReturn(new byte[]{21, 22}); writeCommand = new WriteCommand( serviceUUID, characteristicUUID, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT, inputSource, commandObserver); writeCommand.execute(device, operationCommandObserver, gatt); writeCommand.onCharacteristicWrite(gatt, gattCharacteristic, BluetoothGatt.GATT_FAILURE); verifyCommandFail(); }
@Override protected void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status != BluetoothGatt.GATT_SUCCESS) { NeatleLogger.i("Write on " + characteristic.getUuid() + " failed with status " + status); finish(CommandResult.createErrorResult(characteristicUUID, status)); return; } if (asyncMode) { synchronized (bufferReadLock) { bufferReadLock.notify(); } } else { byte[] chunk; try { chunk = buffer.nextChunk(); } catch (IOException ex) { NeatleLogger.e("Failed to get the first chunk", ex); finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE)); try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } return; } if (chunk == null) { try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } finish(CommandResult.createEmptySuccess(characteristicUUID)); return; } nextChunkReady(chunk); } }
WriteCommand extends SingleCharacteristicsCommand { @Override protected void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status != BluetoothGatt.GATT_SUCCESS) { NeatleLogger.i("Write on " + characteristic.getUuid() + " failed with status " + status); finish(CommandResult.createErrorResult(characteristicUUID, status)); return; } if (asyncMode) { synchronized (bufferReadLock) { bufferReadLock.notify(); } } else { byte[] chunk; try { chunk = buffer.nextChunk(); } catch (IOException ex) { NeatleLogger.e("Failed to get the first chunk", ex); finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE)); try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } return; } if (chunk == null) { try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } finish(CommandResult.createEmptySuccess(characteristicUUID)); return; } nextChunkReady(chunk); } } }
WriteCommand extends SingleCharacteristicsCommand { @Override protected void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status != BluetoothGatt.GATT_SUCCESS) { NeatleLogger.i("Write on " + characteristic.getUuid() + " failed with status " + status); finish(CommandResult.createErrorResult(characteristicUUID, status)); return; } if (asyncMode) { synchronized (bufferReadLock) { bufferReadLock.notify(); } } else { byte[] chunk; try { chunk = buffer.nextChunk(); } catch (IOException ex) { NeatleLogger.e("Failed to get the first chunk", ex); finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE)); try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } return; } if (chunk == null) { try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } finish(CommandResult.createEmptySuccess(characteristicUUID)); return; } nextChunkReady(chunk); } } WriteCommand(UUID serviceUUID, UUID characteristicsUUID, int writeType, InputSource buffer, CommandObserver observer); }
WriteCommand extends SingleCharacteristicsCommand { @Override protected void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status != BluetoothGatt.GATT_SUCCESS) { NeatleLogger.i("Write on " + characteristic.getUuid() + " failed with status " + status); finish(CommandResult.createErrorResult(characteristicUUID, status)); return; } if (asyncMode) { synchronized (bufferReadLock) { bufferReadLock.notify(); } } else { byte[] chunk; try { chunk = buffer.nextChunk(); } catch (IOException ex) { NeatleLogger.e("Failed to get the first chunk", ex); finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE)); try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } return; } if (chunk == null) { try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } finish(CommandResult.createEmptySuccess(characteristicUUID)); return; } nextChunkReady(chunk); } } WriteCommand(UUID serviceUUID, UUID characteristicsUUID, int writeType, InputSource buffer, CommandObserver observer); @Override String toString(); }
WriteCommand extends SingleCharacteristicsCommand { @Override protected void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status != BluetoothGatt.GATT_SUCCESS) { NeatleLogger.i("Write on " + characteristic.getUuid() + " failed with status " + status); finish(CommandResult.createErrorResult(characteristicUUID, status)); return; } if (asyncMode) { synchronized (bufferReadLock) { bufferReadLock.notify(); } } else { byte[] chunk; try { chunk = buffer.nextChunk(); } catch (IOException ex) { NeatleLogger.e("Failed to get the first chunk", ex); finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE)); try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } return; } if (chunk == null) { try { buffer.close(); } catch (IOException e) { NeatleLogger.e("Failed to close input source", e); } finish(CommandResult.createEmptySuccess(characteristicUUID)); return; } nextChunkReady(chunk); } } WriteCommand(UUID serviceUUID, UUID characteristicsUUID, int writeType, InputSource buffer, CommandObserver observer); @Override String toString();  }