no.nordicsemi.android.ble.exception.RequestFailedException Java Examples

The following examples show how to use no.nordicsemi.android.ble.exception.RequestFailedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AwaitingRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@NonNull
@Override
public <E extends T> E await(@NonNull final E response)
		throws RequestFailedException, DeviceDisconnectedException, BluetoothDisabledException,
		InvalidRequestException, InterruptedException {
	assertNotMainThread();

	try {
		// Ensure the trigger request it enqueued after the callback has been set.
		if (trigger != null && trigger.enqueued) {
			throw new IllegalStateException("Trigger request already enqueued");
		}
		super.await(response);
		return response;
	} catch (final RequestFailedException e) {
		if (triggerStatus != BluetoothGatt.GATT_SUCCESS) {
			// Trigger will never have invalid request status. The outer request will.
			/*if (triggerStatus == RequestCallback.REASON_REQUEST_INVALID) {
				throw new InvalidRequestException(trigger);
			}*/
			throw new RequestFailedException(trigger, triggerStatus);
		}
		throw e;
	}
}
 
Example #2
Source File: RequestQueue.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Synchronously waits until all enqueued requests are done. The queue will fail if any of
 * the enqueued requests fails. All following requests will be ignored.
 * <p>
 * Callbacks set using {@link #before(BeforeCallback)}, {@link #done(SuccessCallback)} and
 * {@link #fail(FailCallback)} will be ignored.
 * <p>
 * This method may not be called from the main (UI) thread.
 *
 * @throws RequestFailedException      thrown when the BLE request finished with status other
 *                                     than {@link BluetoothGatt#GATT_SUCCESS}.
 * @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
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @throws InterruptedException        thrown when one of the request has failed with a timeout.
 */
public final void await() throws RequestFailedException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException, InterruptedException {
	assertNotMainThread();

	final BeforeCallback bc = beforeCallback;
	final SuccessCallback sc = successCallback;
	final FailCallback fc = failCallback;
	try {
		syncLock.close();
		final RequestCallback callback = new RequestCallback();
		beforeCallback = null;
		done(callback).fail(callback).invalid(callback).enqueue();

		syncLock.block();
		if (!callback.isSuccess()) {
			if (callback.status == FailCallback.REASON_DEVICE_DISCONNECTED) {
				throw new DeviceDisconnectedException();
			}
			if (callback.status == FailCallback.REASON_BLUETOOTH_DISABLED) {
				throw new BluetoothDisabledException();
			}
			if (callback.status == FailCallback.REASON_TIMEOUT) {
				throw new InterruptedException();
			}
			if (callback.status == RequestCallback.REASON_REQUEST_INVALID) {
				throw new InvalidRequestException(this);
			}
			throw new RequestFailedException(this, callback.status);
		}
	} finally {
		beforeCallback = bc;
		successCallback = sc;
		failCallback = fc;
	}
}
 
Example #3
Source File: TimeoutableRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Synchronously waits until the request is done.
 * <p>
 * Use {@link #timeout(long)} to set the maximum time the manager should wait until the request
 * is ready. When the timeout occurs, the {@link InterruptedException} will be thrown.
 * <p>
 * Callbacks set using {@link #done(SuccessCallback)} and {@link #fail(FailCallback)}
 * will be ignored.
 * <p>
 * This method may not be called from the main (UI) thread.
 *
 * @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
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @see #enqueue()
 */
public final void await() throws RequestFailedException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException, InterruptedException {
	assertNotMainThread();

	final SuccessCallback sc = successCallback;
	final FailCallback fc = failCallback;
	try {
		syncLock.close();
		final RequestCallback callback = new RequestCallback();
		done(callback).fail(callback).invalid(callback).enqueue();

		if (!syncLock.block(timeout)) {
			throw new InterruptedException();
		}
		if (!callback.isSuccess()) {
			if (callback.status == FailCallback.REASON_DEVICE_DISCONNECTED) {
				throw new DeviceDisconnectedException();
			}
			if (callback.status == FailCallback.REASON_BLUETOOTH_DISABLED) {
				throw new BluetoothDisabledException();
			}
			if (callback.status == RequestCallback.REASON_REQUEST_INVALID) {
				throw new InvalidRequestException(this);
			}
			throw new RequestFailedException(this, callback.status);
		}
	} finally {
		successCallback = sc;
		failCallback = fc;
	}
}
 
Example #4
Source File: ConnectionPriorityRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@RequiresApi(value = Build.VERSION_CODES.O)
@NonNull
@Override
public <E extends ConnectionPriorityCallback> E await(@NonNull final E response)
		throws RequestFailedException, DeviceDisconnectedException, BluetoothDisabledException,
		InvalidRequestException {
	// The BluetoothGattCallback#onConnectionUpdated callback was introduced in Android Oreo.
	return super.await(response);
}
 
Example #5
Source File: ConnectionPriorityRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@RequiresApi(value = Build.VERSION_CODES.O)
@NonNull
@Override
public <E extends ConnectionPriorityCallback> E await(@NonNull final Class<E> responseClass)
		throws RequestFailedException, DeviceDisconnectedException, BluetoothDisabledException,
		InvalidRequestException {
	// The BluetoothGattCallback#onConnectionUpdated callback was introduced in Android Oreo.
	return super.await(responseClass);
}
 
Example #6
Source File: SimpleRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Synchronously waits until the request is done.
 * <p>
 * Callbacks set using {@link #before(BeforeCallback)}, {@link #done(SuccessCallback)} and
 * {@link #fail(FailCallback)} will be ignored.
 * <p>
 * This method may not be called from the main (UI) thread.
 *
 * @throws RequestFailedException      thrown when the BLE request finished with status other
 *                                     than {@link BluetoothGatt#GATT_SUCCESS}.
 * @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
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 */
public final void await() throws RequestFailedException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException {
	assertNotMainThread();

	final BeforeCallback bc = beforeCallback;
	final SuccessCallback sc = successCallback;
	final FailCallback fc = failCallback;
	try {
		syncLock.close();
		final RequestCallback callback = new RequestCallback();
		beforeCallback = null;
		done(callback).fail(callback).invalid(callback).enqueue();

		syncLock.block();
		if (!callback.isSuccess()) {
			if (callback.status == FailCallback.REASON_DEVICE_DISCONNECTED) {
				throw new DeviceDisconnectedException();
			}
			if (callback.status == FailCallback.REASON_BLUETOOTH_DISABLED) {
				throw new BluetoothDisabledException();
			}
			if (callback.status == RequestCallback.REASON_REQUEST_INVALID) {
				throw new InvalidRequestException(this);
			}
			throw new RequestFailedException(this, callback.status);
		}
	} finally {
		beforeCallback = bc;
		successCallback = sc;
		failCallback = fc;
	}
}
 
Example #7
Source File: TimeoutableValueRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Synchronously waits until the request is done, for at most given number of milliseconds
 * after which the {@link InterruptedException} will be thrown.
 * <p>
 * Callbacks set using {@link #done(SuccessCallback)}, {@link #fail(FailCallback)} and
 * {@link #with(E)} will be ignored.
 * <p>
 * This method may not be called from the main (UI) thread.
 *
 * @param response the response object.
 * @param timeout  optional timeout in milliseconds.
 * @param <E>      a response class that extends {@link T}.
 * @return The object with a response.
 * @throws RequestFailedException      thrown when the BLE request finished with status other
 *                                     than {@link android.bluetooth.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
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter is disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @deprecated Use {@link #timeout(long)} and {@link #await(E)} instead.
 */
@NonNull
@Deprecated
public <E extends T> E await(@NonNull final E response,
							 @IntRange(from = 0) final long timeout)
		throws RequestFailedException, InterruptedException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException {
	return timeout(timeout).await(response);
}
 
Example #8
Source File: TimeoutableRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Synchronously waits, for as most as the given number of milliseconds, until the request
 * is ready.
 * <p>
 * When the timeout occurs, the {@link InterruptedException} will be thrown.
 * <p>
 * Callbacks set using {@link #done(SuccessCallback)} and {@link #fail(FailCallback)}
 * will be ignored.
 * <p>
 * This method may not be called from the main (UI) thread.
 *
 * @param timeout optional timeout in milliseconds, 0 to disable timeout. This will
 *                override the timeout set using {@link #timeout(long)}.
 * @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
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @deprecated Use {@link #timeout(long)} and {@link #await()} instead.
 */
@Deprecated
public final void await(@IntRange(from = 0) final long timeout) throws RequestFailedException,
		InterruptedException, DeviceDisconnectedException, BluetoothDisabledException,
		InvalidRequestException {
	timeout(timeout).await();
}
 
Example #9
Source File: SimpleValueRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Synchronously waits until the request is done. The given response object will be filled
 * with the request response.
 * <p>
 * Callbacks set using {@link #before(BeforeCallback)}, {@link #done(SuccessCallback)} and
 * {@link #fail(FailCallback)} will be ignored.
 * <p>
 * This method may not be called from the main (UI) thread.
 *
 * @param response the response object.
 * @param <E>      a response class.
 * @return The response with a response.
 * @throws RequestFailedException      thrown when the BLE request finished with status other
 *                                     than {@link android.bluetooth.BluetoothGatt#GATT_SUCCESS}.
 * @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
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter is disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @see #await(Class)
 */
@NonNull
public <E extends T> E await(@NonNull final E response)
		throws RequestFailedException, DeviceDisconnectedException, BluetoothDisabledException,
		InvalidRequestException {
	assertNotMainThread();

	final T vc = valueCallback;
	try {
		with(response).await();
		return response;
	} finally {
		valueCallback = vc;
	}
}
 
Example #10
Source File: ReadRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Same as {@link #await(Class)}, but if the response class extends
 * {@link ProfileReadResponse} and the received response is not valid
 * ({@link ProfileReadResponse#isValid()} returns false), this method will
 * throw an exception.
 *
 * @param responseClass the response class. This class will be instantiate, therefore it
 *                      has to have a default constructor.
 * @return The object with the response.
 * @throws RequestFailedException      thrown when the BLE request finished with status other
    *                                     than {@link BluetoothGatt#GATT_SUCCESS}.
 * @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.
 * @throws DeviceDisconnectedException thrown when the device disconnected before the request
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
    * @throws InvalidDataException        thrown when the received data were not valid (that is when
    *                                     {@link ProfileReadResponse#onDataReceived(BluetoothDevice, Data)}
    *                                     failed to parse the data correctly and called
    *                                     {@link ProfileReadResponse#onInvalidDataReceived(BluetoothDevice, Data)}).
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 */
@NonNull
public <E extends ProfileReadResponse> E awaitValid(@NonNull final Class<E> responseClass)
		throws RequestFailedException, InvalidDataException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException {
	final E response = await(responseClass);
	if (!response.isValid()) {
		throw new InvalidDataException(response);
	}
	return response;
}
 
Example #11
Source File: TimeoutableValueRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Synchronously waits until the request is done, for at most given number of milliseconds
 * after which the {@link InterruptedException} will be thrown.
 * <p>
 * Callbacks set using {@link #done(SuccessCallback)}, {@link #fail(FailCallback)} and
 * {@link #with(E)} will be ignored.
 * <p>
 * This method may not be called from the main (UI) thread.
 *
 * @param responseClass the response class. This class will be instantiate, therefore it has
 *                      to have a default constructor.
 * @param timeout       optional timeout in milliseconds. This value will override one set
 *                      in {@link #timeout(long)}.
 * @param <E>           a response class that extends {@link T}.
 * @return The object with a response.
 * @throws RequestFailedException      thrown when the BLE request finished with status other
 *                                     than {@link android.bluetooth.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.
 * @throws DeviceDisconnectedException thrown when the device disconnected before the request
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter is disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @deprecated Use {@link #timeout(long)} and {@link #await(Class)} instead.
 */
@NonNull
@Deprecated
public <E extends T> E await(@NonNull final Class<E> responseClass,
							 @IntRange(from = 0) final long timeout)
		throws RequestFailedException, InterruptedException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException {
	return timeout(timeout).await(responseClass);
}
 
Example #12
Source File: TimeoutableValueRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Synchronously waits until the request is done.
 * <p>
 * When the timeout, set with {@link #timeout(long)} occurs, the {@link InterruptedException}
 * will be thrown.
 * <p>
 * Callbacks set using {@link #done(SuccessCallback)} and {@link #fail(FailCallback)} and
 * {@link #with(E)} will be ignored.
 * <p>
 * This method may not be called from the main (UI) thread.
 *
 * @param response the response object.
 * @param <E>      a response class.
 * @return The response with a response.
 * @throws RequestFailedException      thrown when the BLE request finished with status other
 *                                     than {@link android.bluetooth.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, or when the trigger was already enqueued.
 * @throws DeviceDisconnectedException thrown when the device disconnected before the request
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter is disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 */
@NonNull
public <E extends T> E await(@NonNull final E response)
		throws RequestFailedException, DeviceDisconnectedException, BluetoothDisabledException,
		InvalidRequestException, InterruptedException {
	assertNotMainThread();

	final T vc = valueCallback;
	try {
		with(response).await();
		return response;
	} finally {
		valueCallback = vc;
	}
}
 
Example #13
Source File: WaitForValueChangedRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Same as {@link #await(Object, long)}, but if received response is not valid,
 * this method will thrown an exception.
 *
 * @param response the result object.
 * @param timeout  optional timeout in milliseconds.
 * @param <E>      a response class that extends {@link ProfileReadResponse}.
 * @return Object with a valid response.
 * @throws IllegalStateException       thrown when you try to call this method from
 *                                     the main (UI) thread.
 * @throws InterruptedException        thrown when the timeout occurred before the
 *                                     characteristic value has changed.
 * @throws RequestFailedException      thrown when the trigger request has failed.
 * @throws DeviceDisconnectedException thrown when the device disconnected before the
 *                                     notification or indication was received.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @throws InvalidDataException        thrown when the received data were not valid (that is when
 *                                     {@link ProfileReadResponse#onDataReceived(BluetoothDevice, Data)}
 *                                     failed to parse the data correctly and called
 *                                     {@link ProfileReadResponse#onInvalidDataReceived(BluetoothDevice, Data)}).
 * @deprecated Use {@link #timeout(long)} and {@link #awaitValid(E)} instead.
 */
@NonNull
@Deprecated
public <E extends ProfileReadResponse> E awaitValid(@NonNull final E response,
													@IntRange(from = 0) final long timeout)
		throws InterruptedException, InvalidDataException, DeviceDisconnectedException,
		RequestFailedException, BluetoothDisabledException, InvalidRequestException {
	return timeout(timeout).awaitValid(response);
}
 
Example #14
Source File: WaitForValueChangedRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Same as {@link #await(Class, long)}, but if received response is not valid, this method will
 * thrown an exception.
 *
 * @param responseClass the result class. This class will be instantiate, therefore it
 *                      has to have a default constructor.
 * @param timeout       optional timeout in milliseconds.
 * @param <E>           a response class that extends {@link ProfileReadResponse}.
 * @return Object with a valid response.
 * @throws IllegalStateException       thrown when you try to call this method from
 *                                     the main (UI) thread.
 * @throws InterruptedException        thrown when the timeout occurred before the
 *                                     characteristic value has changed.
 * @throws IllegalArgumentException    thrown when the response class could not be instantiated.
 * @throws RequestFailedException      thrown when the trigger request has failed.
 * @throws DeviceDisconnectedException thrown when the device disconnected before the
 *                                     notification or indication was received.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @throws InvalidDataException        thrown when the received data were not valid (that is when
 *                                     {@link ProfileReadResponse#onDataReceived(BluetoothDevice, Data)}
 *                                     failed to parse the data correctly and called
 *                                     {@link ProfileReadResponse#onInvalidDataReceived(BluetoothDevice, Data)}).
 * @deprecated Use {@link #timeout(long)} and {@link #awaitValid(Class)} instead.
 */
@NonNull
@Deprecated
public <E extends ProfileReadResponse> E awaitValid(@NonNull final Class<E> responseClass,
													@IntRange(from = 0) final long timeout)
		throws InterruptedException, InvalidDataException, RequestFailedException,
		DeviceDisconnectedException, BluetoothDisabledException, InvalidRequestException {
	return timeout(timeout).awaitValid(responseClass);
}
 
Example #15
Source File: WaitForValueChangedRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Similar to {@link #await(DataReceivedCallback)}, but if the response class extends
 * {@link ProfileReadResponse} and the received response is invalid, an exception is thrown.
 * This allows to keep all error handling in one place.
 *
 * @param responseClass the result class. This class will be instantiate, therefore it
 *                      has to have a default constructor.
 * @param <E>           a response class that extends {@link ProfileReadResponse}.
 * @return Object with a valid response.
 * @throws IllegalStateException       thrown when you try to call this method from
 *                                     the main (UI) thread.
 * @throws InterruptedException        thrown when the timeout occurred before the
 *                                     characteristic value has changed.
 * @throws IllegalArgumentException    thrown when the response class could not be instantiated.
 * @throws RequestFailedException      thrown when the trigger request has failed.
 * @throws DeviceDisconnectedException thrown when the device disconnected before the
 *                                     notification or indication was received.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @throws InvalidDataException        thrown when the received data were not valid (that is when
 *                                     {@link ProfileReadResponse#onDataReceived(BluetoothDevice, Data)}
 *                                     failed to parse the data correctly and called
 *                                     {@link ProfileReadResponse#onInvalidDataReceived(BluetoothDevice, Data)}).
 */
@SuppressWarnings("ConstantConditions")
@NonNull
public <E extends ProfileReadResponse> E awaitValid(@NonNull final Class<E> responseClass)
		throws RequestFailedException, InvalidDataException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException, InterruptedException {
	final E response = await(responseClass);
	if (response != null && !response.isValid()) {
		throw new InvalidDataException(response);
	}
	return response;
}
 
Example #16
Source File: WaitForValueChangedRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Similar to {@link #await(Class)}, but if the response class extends
 * {@link ProfileReadResponse} and the received response is invalid, an exception is thrown.
 * This allows to keep all error handling in one place.
 *
 * @param response the result object.
 * @param <E>      a response class that extends {@link ProfileReadResponse}.
 * @return Object with a valid response.
 * @throws IllegalStateException       thrown when you try to call this method from
 *                                     the main (UI) thread.
 * @throws InterruptedException        thrown when the timeout occurred before the
 *                                     characteristic value has changed.
 * @throws RequestFailedException      thrown when the trigger request has failed.
 * @throws DeviceDisconnectedException thrown when the device disconnected before the
 *                                     notification or indication was received.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 * @throws InvalidDataException        thrown when the received data were not valid (that is when
 *                                     {@link ProfileReadResponse#onDataReceived(BluetoothDevice, Data)}
 *                                     failed to parse the data correctly and called
 *                                     {@link ProfileReadResponse#onInvalidDataReceived(BluetoothDevice, Data)}).
 */
@SuppressWarnings("ConstantConditions")
@NonNull
public <E extends ProfileReadResponse> E awaitValid(@NonNull final E response)
		throws RequestFailedException, InvalidDataException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException, InterruptedException {
	final E result = await(response);
	if (result != null && !result.isValid()) {
		throw new InvalidDataException(result);
	}
	return result;
}
 
Example #17
Source File: ReadRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Same as {@link #await(Object)}, but if the response class extends
 * {@link ProfileReadResponse} and the received response is not valid
 * ({@link ProfileReadResponse#isValid()} returns false), this method will
 * throw an exception.
 *
 * @param response the response object.
 * @return The object with the response.
    * @throws RequestFailedException      thrown when the BLE request finished with status other
    *                                     than {@link BluetoothGatt#GATT_SUCCESS}.
    * @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
 *                                     was completed.
 * @throws BluetoothDisabledException  thrown when the Bluetooth adapter has been disabled.
    * @throws InvalidDataException        thrown when the received data were not valid (that is when
    *                                     {@link ProfileReadResponse#onDataReceived(BluetoothDevice, Data)}
    *                                     failed to parse the data correctly and called
    *                                     {@link ProfileReadResponse#onInvalidDataReceived(BluetoothDevice, Data)}).
 * @throws InvalidRequestException     thrown when the request was called before the device was
 *                                     connected at least once (unknown device).
 */
@NonNull
public <E extends ProfileReadResponse> E awaitValid(@NonNull final E response)
		throws RequestFailedException, InvalidDataException, DeviceDisconnectedException,
		BluetoothDisabledException, InvalidRequestException {
	await(response);
	if (!response.isValid()) {
		throw new InvalidDataException(response);
	}
	return response;
}