/** * This method makes sure the data sent will be split to at-most MTU-3 bytes long packets. * This is because Long Write does not work with Reliable Write. */ void forceSplit() { if (dataSplitter == null) split(); }
/** * Sends the given text to RX characteristic. * @param text the text to be sent */ public void send(final String text) { // Are we connected? if (mRXCharacteristic == null) return; if (!TextUtils.isEmpty(text)) { final WriteRequest request = writeCharacteristic(mRXCharacteristic, text.getBytes()) .with((device, data) -> log(LogContract.Log.Level.APPLICATION, "\"" + data.getStringValue(0) + "\" sent")); if (!mUseLongWrite) { // This will automatically split the long data into MTU-3-byte long packets. request.split(); } request.enqueue(); } } }
@Test public void split_highMtu() { final int MTU_HIGH = 276; final WriteRequest request = Request.newWriteRequest(characteristic, text.getBytes(), BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) .split(); chunk = request.getData(MTU_HIGH); // Verify the chunk assertNotNull(chunk); assertEquals(MTU_HIGH - 3, chunk.length); final String expected = text.substring(0, MTU_HIGH - 3); assertArrayEquals(expected.getBytes(), chunk); }
@Test public void split_basic() { final WriteRequest request = Request.newWriteRequest(characteristic, text.getBytes(), BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT) .split(); chunk = request.getData(MTU); // Verify the chunk assertNotNull(chunk); assertEquals(MTU - 3, chunk.length); final String expected = text.substring(0, MTU - 3); assertArrayEquals(expected.getBytes(), chunk); }
/** * This method will write important data to the device. * * @param parameter parameter to be written. */ void performAction(final String parameter) { log(Log.VERBOSE, "Changing device name to \"" + parameter + "\""); // Write some data to the characteristic. writeCharacteristic(mDeviceNameCharacteristic, Data.from(parameter)) // If data are longer than MTU-3, they will be chunked into multiple packets. // Check out other split options, with .split(...). .split() // Callback called when data were sent, or added to outgoing queue in case // Write Without Request type was used. .with((device, data) -> log(Log.DEBUG, data.size() + " bytes were sent")) // Callback called when data were sent, or added to outgoing queue in case // Write Without Request type was used. This is called after .with(...) callback. .done(device -> log(LogContract.Log.Level.APPLICATION, "Device name set to \"" + parameter + "\"")) // Callback called when write has failed. .fail((device, status) -> log(Log.WARN, "Failed to change device name")) .enqueue(); } }
.split((device, data, index) -> {