/** * Sends the read request to the given descriptor. * If the descriptor is null, the {@link Request#fail(FailCallback) fail(FailCallback)} * callback will be called. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param descriptor the descriptor to read. * @return The request. */ @NonNull protected final ReadRequest readDescriptor(@Nullable final BluetoothGattDescriptor descriptor) { return Request.newReadRequest(descriptor).setManager(this); }
/** * Writes the given data to the descriptor. * <p> * Use {@link WriteRequest#split() split()} or * {@link WriteRequest#split(DataSplitter) split(ValueSplitter)} on the returned * {@link WriteRequest} if data should be automatically split into multiple packets. * If the descriptor is null, the {@link Request#fail(FailCallback) fail(FailCallback)} * callback will be called. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param descriptor the descriptor to write to. * @param data data to be written to the descriptor. * @return The request. */ @NonNull protected final WriteRequest writeDescriptor(@Nullable final BluetoothGattDescriptor descriptor, @Nullable final byte[] data) { return Request.newWriteRequest(descriptor, data).setManager(this); }
/** * Returns a request to create bond with the device. The device must be first set using * {@link #connect(BluetoothDevice)} which will try to connect to the device. * If you need to pair with a device before connecting to it you may do it without * the use of BleManager object and connect after bond is established. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @return The request. */ @NonNull protected final Request createBond() { return Request.createBond().setManager(this); }
/** * Sets an optional request that is suppose to trigger the notification or indication. * This is to ensure that the characteristic value won't change before the callback was set. * * @param trigger the operation that triggers the notification, usually a write characteristic * request that write some OP CODE. * @return The request. */ @NonNull public WaitForValueChangedRequest trigger(@NonNull final Operation trigger) { if (trigger instanceof Request) { this.trigger = (Request) trigger; this.triggerStatus = NOT_STARTED; // The trigger will never receive invalid request event. // If the BluetoothDevice wasn't set, the whole WaitForValueChangedRequest would be invalid. /*this.trigger.invalid(() -> { // never called });*/ this.trigger.internalBefore(device -> triggerStatus = STARTED); this.trigger.internalSuccess(device -> triggerStatus = BluetoothGatt.GATT_SUCCESS); this.trigger.internalFail((device, status) -> { triggerStatus = status; syncLock.open(); notifyFail(device, status); }); } return this; }
@Test public void split_merge() { final WriteRequest request = Request.newWriteRequest(characteristic, text.getBytes(), BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT) .split(); final ReadRequest readRequest = Request.newReadRequest(characteristic) .merge((output, lastPacket, index) -> {
throw new NullPointerException("Bluetooth device not specified"); return Request.connect(device) .useAutoConnect(shouldAutoConnect()) .setManager(this);
/** * Enqueues the request for asynchronous execution. * <p> * Use {@link #timeout(long)} to set the maximum time the manager should wait until the device * is ready. When the timeout occurs, the request will fail with * {@link FailCallback#REASON_TIMEOUT} and the device will get disconnected. */ @Override public final void enqueue() { super.enqueue(); }
/** * Disconnects from the device or cancels the pending connection attempt. * Does nothing if device was not connected. * * @return The disconnect request. The completion callback will be called after the device * has disconnected and the connection was closed. If the device was not connected, * the completion callback will be called immediately with device parameter set to null. */ @NonNull public final DisconnectRequest disconnect() { return Request.disconnect().setManager(this); }
throw new NullPointerException("Bluetooth device not specified"); return Request.connect(device) .usePreferredPhy(phy) .useAutoConnect(shouldAutoConnect())
/** * Writes the given data to the characteristic. The write type is taken from the characteristic. * <p> * Use {@link WriteRequest#split() split()} or * {@link WriteRequest#split(DataSplitter) split(ValueSplitter)} on the returned * {@link WriteRequest} if data should be automatically split into multiple packets. * If the characteristic is null, the {@link Request#fail(FailCallback) fail(FailCallback)} * callback will be called. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param characteristic the characteristic to write to. * @param data data to be written to the characteristic. * @return The request. */ @NonNull protected final WriteRequest writeCharacteristic(@Nullable final BluetoothGattCharacteristic characteristic, @Nullable final byte[] data) { return Request.newWriteRequest(characteristic, data).setManager(this); }
/** * Sends the read request to the given characteristic. * If the characteristic is null, the {@link Request#fail(FailCallback) fail(FailCallback)} * callback will be called. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param characteristic the characteristic to read. * @return The request. */ @NonNull protected final ReadRequest readCharacteristic(@Nullable final BluetoothGattCharacteristic characteristic) { return Request.newReadRequest(characteristic).setManager(this); }
/** * Writes at most length bytes from offset at given data to the characteristic. * The write type is taken from the characteristic. * <p> * Use {@link WriteRequest#split() split()} or * {@link WriteRequest#split(DataSplitter) split(ValueSplitter)} on the returned * {@link WriteRequest} if data should be automatically split into multiple packets. * If the characteristic is null, the {@link Request#fail(FailCallback) fail(FailCallback)} * callback will be called. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param characteristic the characteristic to write to. * @param data data to be written to the characteristic. * @param offset index of the first byte to be sent. * @param length number of bytes to be sent. * @return The request. */ @NonNull protected final WriteRequest writeCharacteristic(@Nullable final BluetoothGattCharacteristic characteristic, @Nullable final byte[] data, final int offset, final int length) { return Request.newWriteRequest(characteristic, data, offset, length).setManager(this); }
/** * Writes at most length bytes from offset at given data to the descriptor. * <p> * Use {@link WriteRequest#split() split()} or * {@link WriteRequest#split(DataSplitter) split(ValueSplitter)} on the returned * {@link WriteRequest} if data should be automatically split into multiple packets. * If the descriptor is null, the {@link Request#fail(FailCallback) fail(FailCallback)} * callback will be called. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param descriptor the descriptor to write to. * @param data data to be written to the descriptor. * @param offset index of the first byte to be sent. * @param length number of bytes to be sent. * @return The request. */ @NonNull protected final WriteRequest writeDescriptor(@Nullable final BluetoothGattDescriptor descriptor, @Nullable final byte[] data, final int offset, final int length) { return Request.newWriteRequest(descriptor, data, offset, length).setManager(this); }
/** * Writes the given data to the characteristic. The write type is taken from the characteristic. * <p> * Use {@link WriteRequest#split() split()} or * {@link WriteRequest#split(DataSplitter) split(DataSplitter)} on the returned * {@link WriteRequest} if data should be automatically split into multiple packets. * If the characteristic is null, the {@link Request#fail(FailCallback) fail(FailCallback)} * callback will be called. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param characteristic the characteristic to write to. * @param data data to be written to the characteristic. * @return The request. */ @NonNull protected final WriteRequest writeCharacteristic(@Nullable final BluetoothGattCharacteristic characteristic, @Nullable final Data data) { return Request.newWriteRequest(characteristic, data != null ? data.getValue() : null) .setManager(this); }
/** * Writes the given data to the descriptor. * <p> * Use {@link WriteRequest#split() split()} or * {@link WriteRequest#split(DataSplitter) split(ValueSplitter)} on the returned * {@link WriteRequest} if data should be automatically split into multiple packets. * If the descriptor is null, the {@link Request#fail(FailCallback) fail(FailCallback)} * callback will be called. * <p> * The returned request must be either enqueued using {@link Request#enqueue()} for * asynchronous use, or awaited using await() in synchronous execution. * * @param descriptor the descriptor to write to. * @param data data to be written to the descriptor. * @return The request. */ @NonNull protected final WriteRequest writeDescriptor(@Nullable final BluetoothGattDescriptor descriptor, @Nullable final Data data) { return Request.newWriteRequest(descriptor, data != null ? data.getValue() : null) .setManager(this); }
@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); }
@Test public void split_callbacks() { final WriteRequest request = Request.newWriteRequest(characteristic, text.getBytes(), BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT) .split((device, data, index) -> {