no.nordicsemi.android.ble.callback.FailCallback Java Examples

The following examples show how to use no.nordicsemi.android.ble.callback.FailCallback. 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: ProximityManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Writes the HIGH ALERT or NO ALERT command to the target device.
 *
 * @param on true to enable the alarm on proximity tag, false to disable it.
 */
public void writeImmediateAlert(final boolean on) {
	if (!isConnected())
		return;

	writeCharacteristic(alertLevelCharacteristic, on ? AlertLevelData.highAlert() : AlertLevelData.noAlert())
			.before(device -> log(Log.VERBOSE,
					on ? "Setting alarm to HIGH..." : "Disabling alarm..."))
			.with((device, data) -> log(LogContract.Log.Level.APPLICATION,
					"\"" + AlertLevelParser.parse(data) + "\" sent"))
			.done(device -> {
				alertOn = on;
				mCallbacks.onRemoteAlarmSwitched(device, on);
			})
			.fail((device, status) -> log(Log.WARN,
					status == FailCallback.REASON_NULL_ATTRIBUTE ?
							"Alert Level characteristic not found" :
							GattError.parse(status)))
			.enqueue();
}
 
Example #2
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 #3
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 #4
Source File: TimeoutableRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
void notifyStarted(@NonNull final BluetoothDevice device) {
	if (timeout > 0L) {
		timeoutCallback = () -> {
			timeoutCallback = null;
			if (!finished) {
				notifyFail(device, FailCallback.REASON_TIMEOUT);
				requestHandler.onRequestTimeout(this);
			}
		};
		handler.postDelayed(timeoutCallback, timeout);
	}
	super.notifyStarted(device);
}
 
Example #5
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 #6
Source File: ReliableWriteRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public ReliableWriteRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #7
Source File: WriteRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public WriteRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #8
Source File: ReadRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public ReadRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #9
Source File: RequestQueue.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public RequestQueue fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #10
Source File: WaitForReadRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public WaitForReadRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #11
Source File: PhyRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public PhyRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #12
Source File: ConnectRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NonNull
@Override
public ConnectRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #13
Source File: ConditionalWaitRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public ConditionalWaitRequest<T> fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #14
Source File: ConnectionPriorityRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public ConnectionPriorityRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #15
Source File: ReadRssiRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public ReadRssiRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #16
Source File: DisconnectRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NonNull
@Override
public DisconnectRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #17
Source File: SetValueRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public SetValueRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #18
Source File: WaitForValueChangedRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NonNull
@Override
public WaitForValueChangedRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #19
Source File: MtuRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@NonNull
public MtuRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #20
Source File: SleepRequest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NonNull
@Override
public SleepRequest fail(@NonNull final FailCallback callback) {
	super.fail(callback);
	return this;
}
 
Example #21
Source File: Request.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Use to set a callback that will be called in case the request has failed.
 * If the target device wasn't set before executing this request
 * ({@link BleManager#connect(BluetoothDevice)} was never called), the
 * {@link #invalid(InvalidRequestCallback)} will be used instead, as the
 * {@link BluetoothDevice} is not known.
 * <p>
 * This callback will be ignored if request was executed synchronously, in which case
 * the error will be returned as an exception.
 *
 * @param callback the callback.
 * @return The request.
 */
@NonNull
public Request fail(@NonNull final FailCallback callback) {
	this.failCallback = callback;
	return this;
}
 
Example #22
Source File: Request.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Used to set internal fail callback. The callback will be notified in case the request
 * has failed.
 *
 * @param callback the callback.
 */
void internalFail(@NonNull final FailCallback callback) {
	this.internalFailCallback = callback;
}