Skip to content

Commit

Permalink
Merge pull request #596 from NordicSemiconductor/improvement/cancel-w…
Browse files Browse the repository at this point in the history
…rite

Making WriteRequest and ReadRequest timeoutable amd cancellable
  • Loading branch information
philips77 authored Oct 10, 2024
2 parents cfecc7d + dafd669 commit ec3332b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions ble/src/main/java/no/nordicsemi/android/ble/ReadRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import no.nordicsemi.android.ble.exception.RequestFailedException;

@SuppressWarnings({"unused", "WeakerAccess"})
public final class ReadRequest extends SimpleValueRequest<DataReceivedCallback> implements Operation {
public final class ReadRequest extends TimeoutableValueRequest<DataReceivedCallback> implements Operation {
private ReadProgressCallback progressCallback;
private DataMerger dataMerger;
private DataStream buffer;
Expand Down Expand Up @@ -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.
Expand All @@ -214,7 +216,7 @@ public ReadRequest merge(@NonNull final DataMerger merger,
@NonNull
public <E extends ProfileReadResponse> E awaitValid(@NonNull final Class<E> responseClass)
throws RequestFailedException, InvalidDataException, DeviceDisconnectedException,
BluetoothDisabledException, InvalidRequestException {
BluetoothDisabledException, InterruptedException, InvalidRequestException {
final E response = await(responseClass);
if (!response.isValid()) {
throw new InvalidDataException(response);
Expand All @@ -232,6 +234,8 @@ public <E extends ProfileReadResponse> E awaitValid(@NonNull final Class<E> 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
Expand All @@ -247,7 +251,7 @@ public <E extends ProfileReadResponse> E awaitValid(@NonNull final Class<E> resp
@NonNull
public <E extends ProfileReadResponse> E awaitValid(@NonNull final E response)
throws RequestFailedException, InvalidDataException, DeviceDisconnectedException,
BluetoothDisabledException, InvalidRequestException {
BluetoothDisabledException, InterruptedException, InvalidRequestException {
await(response);
if (!response.isValid()) {
throw new InvalidDataException(response);
Expand Down Expand Up @@ -315,6 +319,6 @@ void notifyValueChanged(@NonNull final BluetoothDevice device, @Nullable final b

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean hasMore() {
return !complete;
return !complete && !cancelled && !finished;
}
}
4 changes: 2 additions & 2 deletions ble/src/main/java/no/nordicsemi/android/ble/WriteRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import no.nordicsemi.android.ble.data.DefaultMtuSplitter;

@SuppressWarnings({"unused", "WeakerAccess"})
public final class WriteRequest extends SimpleValueRequest<DataSentCallback> implements Operation {
public final class WriteRequest extends TimeoutableValueRequest<DataSentCallback> implements Operation {
private final static DataSplitter MTU_SPLITTER = new DefaultMtuSplitter();

private WriteProgressCallback progressCallback;
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit ec3332b

Please sign in to comment.