From dafd6690a90a43207b475769b5bf03ef01221956 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 10 Oct 2024 23:40:43 +0200 Subject: [PATCH] Making WriteRequest and ReadRequest timeoutable amd cancellable --- .../java/no/nordicsemi/android/ble/ReadRequest.java | 12 ++++++++---- .../java/no/nordicsemi/android/ble/WriteRequest.java | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ble/src/main/java/no/nordicsemi/android/ble/ReadRequest.java b/ble/src/main/java/no/nordicsemi/android/ble/ReadRequest.java index 1fd7d290..406cf6ce 100644 --- a/ble/src/main/java/no/nordicsemi/android/ble/ReadRequest.java +++ b/ble/src/main/java/no/nordicsemi/android/ble/ReadRequest.java @@ -51,7 +51,7 @@ import no.nordicsemi.android.ble.exception.RequestFailedException; @SuppressWarnings({"unused", "WeakerAccess"}) -public final class ReadRequest extends SimpleValueRequest implements Operation { +public final class ReadRequest extends TimeoutableValueRequest implements Operation { private ReadProgressCallback progressCallback; private DataMerger dataMerger; private DataStream buffer; @@ -198,6 +198,8 @@ public ReadRequest merge(@NonNull final DataMerger merger, * @return The object with the response. * @throws RequestFailedException thrown when the BLE request finished with status other * than {@link BluetoothGatt#GATT_SUCCESS}. + * @throws InterruptedException thrown if the timeout occurred before the request has + * finished. * @throws IllegalStateException thrown when you try to call this method from the main * (UI) thread. * @throws IllegalArgumentException thrown when the response class could not be instantiated. @@ -214,7 +216,7 @@ public ReadRequest merge(@NonNull final DataMerger merger, @NonNull public E awaitValid(@NonNull final Class responseClass) throws RequestFailedException, InvalidDataException, DeviceDisconnectedException, - BluetoothDisabledException, InvalidRequestException { + BluetoothDisabledException, InterruptedException, InvalidRequestException { final E response = await(responseClass); if (!response.isValid()) { throw new InvalidDataException(response); @@ -232,6 +234,8 @@ public E awaitValid(@NonNull final Class resp * @return The object with the response. * @throws RequestFailedException thrown when the BLE request finished with status other * than {@link BluetoothGatt#GATT_SUCCESS}. + * @throws InterruptedException thrown if the timeout occurred before the request has + * finished. * @throws IllegalStateException thrown when you try to call this method from the main * (UI) thread. * @throws DeviceDisconnectedException thrown when the device disconnected before the request @@ -247,7 +251,7 @@ public E awaitValid(@NonNull final Class resp @NonNull public E awaitValid(@NonNull final E response) throws RequestFailedException, InvalidDataException, DeviceDisconnectedException, - BluetoothDisabledException, InvalidRequestException { + BluetoothDisabledException, InterruptedException, InvalidRequestException { await(response); if (!response.isValid()) { throw new InvalidDataException(response); @@ -315,6 +319,6 @@ void notifyValueChanged(@NonNull final BluetoothDevice device, @Nullable final b @SuppressWarnings("BooleanMethodIsAlwaysInverted") boolean hasMore() { - return !complete; + return !complete && !cancelled && !finished; } } diff --git a/ble/src/main/java/no/nordicsemi/android/ble/WriteRequest.java b/ble/src/main/java/no/nordicsemi/android/ble/WriteRequest.java index d4f853d8..21a1d00d 100644 --- a/ble/src/main/java/no/nordicsemi/android/ble/WriteRequest.java +++ b/ble/src/main/java/no/nordicsemi/android/ble/WriteRequest.java @@ -47,7 +47,7 @@ import no.nordicsemi.android.ble.data.DefaultMtuSplitter; @SuppressWarnings({"unused", "WeakerAccess"}) -public final class WriteRequest extends SimpleValueRequest implements Operation { +public final class WriteRequest extends TimeoutableValueRequest implements Operation { private final static DataSplitter MTU_SPLITTER = new DefaultMtuSplitter(); private WriteProgressCallback progressCallback; @@ -310,7 +310,7 @@ boolean notifyPacketSent(@NonNull final BluetoothDevice device, @Nullable final * @return True if not all data were sent, false if the request is complete. */ boolean hasMore() { - return !complete; + return !complete && !cancelled && !finished; } /**