-
public abstract class RequestOn Android, when multiple BLE operations needs to be done, it is required to wait for a proper BluetoothGattCallback callback before calling another operation. In order to make BLE operations easier the BleManager allows to enqueue a request containing all data necessary for a given operation. Requests are performed one after another until the queue is empty.
-
-
Field Summary
Fields Modifier and Type Field Description CallbackHandlerhandler
-
Method Summary
Modifier and Type Method Description RequestsetHandler(@Nullable() Handler handler)Sets the handler that will be used to invoke callbacks. static SimpleRequestcreateBond()Creates a new request that will start pairing with the device. static SimpleRequestremoveBond()Creates a new request that will remove the bonding information from the Android device.This is done using reflections and may not work on all devices. static ReadRequestnewReadRequest(@Nullable() BluetoothGattCharacteristic characteristic)Creates new Read Characteristic request. static WriteRequestnewWriteRequest(@Nullable() BluetoothGattCharacteristic characteristic, @Nullable() Array<byte> value)Creates new Write Characteristic request. static WriteRequestnewWriteRequest(@Nullable() BluetoothGattCharacteristic characteristic, @Nullable() Array<byte> value, int writeType)Creates new Write Characteristic request. static WriteRequestnewWriteRequest(@Nullable() BluetoothGattCharacteristic characteristic, @Nullable() Array<byte> value, @IntRange(from = 0) int offset, @IntRange(from = 0) int length)Creates new Write Characteristic request. static WriteRequestnewWriteRequest(@Nullable() BluetoothGattCharacteristic characteristic, @Nullable() Array<byte> value, @IntRange(from = 0) int offset, @IntRange(from = 0) int length, int writeType)Creates new Write Characteristic request. static ReadRequestnewReadRequest(@Nullable() BluetoothGattDescriptor descriptor)Creates new Read Descriptor request. static WriteRequestnewWriteRequest(@Nullable() BluetoothGattDescriptor descriptor, @Nullable() Array<byte> value)Creates new Write Descriptor request. static WriteRequestnewWriteRequest(@Nullable() BluetoothGattDescriptor descriptor, @Nullable() Array<byte> value, @IntRange(from = 0) int offset, @IntRange(from = 0) int length)Creates new Write Descriptor request. static WriteRequestnewEnableNotificationsRequest(@Nullable() BluetoothGattCharacteristic characteristic)Creates new Enable Notification request. static WriteRequestnewDisableNotificationsRequest(@Nullable() BluetoothGattCharacteristic characteristic)Creates new Disable Notification request. static WriteRequestnewEnableIndicationsRequest(@Nullable() BluetoothGattCharacteristic characteristic)Creates new Enable Indications request. static WriteRequestnewDisableIndicationsRequest(@Nullable() BluetoothGattCharacteristic characteristic)Creates new Disable Indications request. static WaitForValueChangedRequestnewWaitForNotificationRequest(@Nullable() BluetoothGattCharacteristic characteristic)Creates new Wait For Notification request. static WaitForValueChangedRequestnewWaitForIndicationRequest(@Nullable() BluetoothGattCharacteristic characteristic)Creates new Wait For Indication request. static ReadRequestnewReadBatteryLevelRequest()Creates new Read Battery Level request. static WriteRequestnewEnableBatteryLevelNotificationsRequest()Creates new Enable Notifications on the first found Battery Level characteristic from thefirst found Battery Service. static WriteRequestnewDisableBatteryLevelNotificationsRequest()Creates new Disable Notifications on the first found Battery Level characteristic from thefirst found Battery Service. static MtuRequestnewMtuRequest(@IntRange(from = 23, to = 517) int mtu)Requests new MTU (Maximum Transfer Unit). static ConnectionPriorityRequestnewConnectionPriorityRequest(int priority)Requests the new connection priority. static PhyRequestnewSetPreferredPhyRequest(int txPhy, int rxPhy, int phyOptions)Requests the change of preferred PHY for this connections. static PhyRequestnewReadPhyRequest()Reads the current PHY for this connections. static ReadRssiRequestnewReadRssiRequest()Reads the current RSSI (Received Signal Strength Indication). static SimpleRequestnewRefreshCacheRequest()Refreshes the device cache. static SleepRequestnewSleepRequest(@IntRange(from = 0) long delay)Creates new Sleep request that will postpone next request for given number of milliseconds. Requestdone(@NonNull() SuccessCallback callback)Use to set a completion callback. Requestfail(@NonNull() FailCallback callback)Use to set a callback that will be called in case the request has failed. Requestinvalid(@NonNull() InvalidRequestCallback callback)Use to set a callback that will be called in case the request was invalid, for examplecalled before the device was connected.This callback will be ignored if request was executed synchronously, in which casethe error will be returned as an exception. Requestbefore(@NonNull() BeforeCallback callback)Sets a callback that will be executed before the execution of this operation starts. Requestthen(@NonNull() AfterCallback callback)Sets a callback that will be executed when the request has been processed, no matterthe request result. voidenqueue()Enqueues the request for asynchronous execution. -
-
Method Detail
-
setHandler
@NonNull() Request setHandler(@Nullable() Handler handler)
Sets the handler that will be used to invoke callbacks. By default, the handler set in BleManager will be used.
If set to null, the callbacks will be invoked immediately on the BLE looper.
- Parameters:
handler- The handler to invoke callbacks for this request.
-
createBond
@Deprecated()@NonNull() static SimpleRequest createBond()
Creates a new request that will start pairing with the device.
-
removeBond
@Deprecated()@NonNull() static SimpleRequest removeBond()
Creates a new request that will remove the bonding information from the Android device.This is done using reflections and may not work on all devices.
The device will disconnect after calling this method. The success callback will be calledafter the device got disconnected, when the getBondState changesto BOND_NONE.
-
newReadRequest
@Deprecated()@NonNull() static ReadRequest newReadRequest(@Nullable() BluetoothGattCharacteristic characteristic)
Creates new Read Characteristic request. The request will not be executed if givencharacteristic is null or does not have READ property.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to be read.
-
newWriteRequest
@Deprecated()@NonNull() static WriteRequest newWriteRequest(@Nullable() BluetoothGattCharacteristic characteristic, @Nullable() Array<byte> value)
Creates new Write Characteristic request. The request will not be executed if givencharacteristic is null or does not have WRITE property.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to be written.value- value to be written.
-
newWriteRequest
@Deprecated()@NonNull() static WriteRequest newWriteRequest(@Nullable() BluetoothGattCharacteristic characteristic, @Nullable() Array<byte> value, int writeType)
Creates new Write Characteristic request. The request will not be executed if givencharacteristic is null or does not have WRITE property.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to be written.value- value to be written.writeType- write type to be used, one ofWRITE_TYPE_DEFAULT,WRITE_TYPE_NO_RESPONSE.
-
newWriteRequest
@Deprecated()@NonNull() static WriteRequest newWriteRequest(@Nullable() BluetoothGattCharacteristic characteristic, @Nullable() Array<byte> value, @IntRange(from = 0) int offset, @IntRange(from = 0) int length)
Creates new Write Characteristic request. The request will not be executed if givencharacteristic is null or does not have WRITE property.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to be written.value- value to be written.offset- the offset from which value has to be copied.length- number of bytes to be copied from the value buffer.
-
newWriteRequest
@Deprecated()@NonNull() static WriteRequest newWriteRequest(@Nullable() BluetoothGattCharacteristic characteristic, @Nullable() Array<byte> value, @IntRange(from = 0) int offset, @IntRange(from = 0) int length, int writeType)
Creates new Write Characteristic request. The request will not be executed if givencharacteristic is null or does not have WRITE property.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to be written.value- value to be written.offset- the offset from which value has to be copied.length- number of bytes to be copied from the value buffer.writeType- write type to be used, one ofWRITE_TYPE_DEFAULT,WRITE_TYPE_NO_RESPONSE orWRITE_TYPE_SIGNED.
-
newReadRequest
@Deprecated()@NonNull() static ReadRequest newReadRequest(@Nullable() BluetoothGattDescriptor descriptor)
Creates new Read Descriptor request. The request will not be executed if given descriptoris null. After the operation is complete a proper callback will be invoked.
- Parameters:
descriptor- descriptor to be read.
-
newWriteRequest
@Deprecated()@NonNull() static WriteRequest newWriteRequest(@Nullable() BluetoothGattDescriptor descriptor, @Nullable() Array<byte> value)
Creates new Write Descriptor request. The request will not be executed if given descriptoris null. After the operation is complete a proper callback will be invoked.
- Parameters:
descriptor- descriptor to be written.value- value to be written.
-
newWriteRequest
@Deprecated()@NonNull() static WriteRequest newWriteRequest(@Nullable() BluetoothGattDescriptor descriptor, @Nullable() Array<byte> value, @IntRange(from = 0) int offset, @IntRange(from = 0) int length)
Creates new Write Descriptor request. The request will not be executed if given descriptoris null. After the operation is complete a proper callback will be invoked.
- Parameters:
descriptor- descriptor to be written.value- value to be written.offset- the offset from which value has to be copied.length- number of bytes to be copied from the value buffer.
-
newEnableNotificationsRequest
@Deprecated()@NonNull() static WriteRequest newEnableNotificationsRequest(@Nullable() BluetoothGattCharacteristic characteristic)
Creates new Enable Notification request. The request will not be executed if givencharacteristic is null, does not have NOTIFY property or the CCCD.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to have notifications enabled.
-
newDisableNotificationsRequest
@Deprecated()@NonNull() static WriteRequest newDisableNotificationsRequest(@Nullable() BluetoothGattCharacteristic characteristic)
Creates new Disable Notification request. The request will not be executed if givencharacteristic is null, does not have NOTIFY property or the CCCD.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to have notifications disabled.
-
newEnableIndicationsRequest
@Deprecated()@NonNull() static WriteRequest newEnableIndicationsRequest(@Nullable() BluetoothGattCharacteristic characteristic)
Creates new Enable Indications request. The request will not be executed if givencharacteristic is null, does not have INDICATE property or the CCCD.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to have indications enabled.
-
newDisableIndicationsRequest
@Deprecated()@NonNull() static WriteRequest newDisableIndicationsRequest(@Nullable() BluetoothGattCharacteristic characteristic)
Creates new Disable Indications request. The request will not be executed if givencharacteristic is null, does not have INDICATE property or the CCCD.After the operation is complete a proper callback will be invoked.
- Parameters:
characteristic- characteristic to have indications disabled.
-
newWaitForNotificationRequest
@Deprecated()@NonNull() static WaitForValueChangedRequest newWaitForNotificationRequest(@Nullable() BluetoothGattCharacteristic characteristic)
Creates new Wait For Notification request. The request will not be executed if givencharacteristic is null, does not have NOTIFY property or the CCCD.After the operation is complete a proper callback will be invoked.
If the notification should be triggered by another operation (for example writing anop code), set it with trigger.
- Parameters:
characteristic- characteristic from which a notification should be received.
-
newWaitForIndicationRequest
@Deprecated()@NonNull() static WaitForValueChangedRequest newWaitForIndicationRequest(@Nullable() BluetoothGattCharacteristic characteristic)
Creates new Wait For Indication request. The request will not be executed if givencharacteristic is null, does not have INDICATE property or the CCCD.After the operation is complete a proper callback will be invoked.
If the indication should be triggered by another operation (for example writing anop code), set it with trigger.
- Parameters:
characteristic- characteristic from which a notification should be received.
-
newReadBatteryLevelRequest
@NonNull()@Deprecated() static ReadRequest newReadBatteryLevelRequest()
Creates new Read Battery Level request. The first found Battery Level characteristic valuefrom the first found Battery Service. If any of them is not found, or the characteristicdoes not have the READ property this operation will not execute.
-
newEnableBatteryLevelNotificationsRequest
@NonNull()@Deprecated() static WriteRequest newEnableBatteryLevelNotificationsRequest()
Creates new Enable Notifications on the first found Battery Level characteristic from thefirst found Battery Service. If any of them is not found, or the characteristic does nothave the NOTIFY property this operation will not execute.
-
newDisableBatteryLevelNotificationsRequest
@NonNull()@Deprecated() static WriteRequest newDisableBatteryLevelNotificationsRequest()
Creates new Disable Notifications on the first found Battery Level characteristic from thefirst found Battery Service. If any of them is not found, or the characteristic does nothave the NOTIFY property this operation will not execute.
-
newMtuRequest
@Deprecated()@NonNull() static MtuRequest newMtuRequest(@IntRange(from = 23, to = 517) int mtu)
Requests new MTU (Maximum Transfer Unit). This is only supported on Android Lollipop or newer.On older platforms the request will enqueue, but will fail to execute and fail callback will be called.The target device may reject requested value and set a smaller MTU.
- Parameters:
mtu- the new MTU.
-
newConnectionPriorityRequest
@Deprecated()@NonNull() static ConnectionPriorityRequest newConnectionPriorityRequest(int priority)
Requests the new connection priority. Acceptable values are:
- CONNECTION_PRIORITY_HIGH - Interval: 11.25 -15 ms (Android 6+) and 7.5 - 10 ms (older), latency: 0,supervision timeout: 20 sec,
- CONNECTION_PRIORITY_BALANCED - Interval: 30 - 50 ms, latency: 0, supervision timeout: 20 sec,
- CONNECTION_PRIORITY_LOW_POWER - Interval: 100 - 125 ms, latency: 2, supervision timeout: 20 sec.
- Parameters:
priority- one of: CONNECTION_PRIORITY_HIGH,CONNECTION_PRIORITY_BALANCED,CONNECTION_PRIORITY_LOW_POWER.
-
newSetPreferredPhyRequest
@Deprecated()@NonNull() static PhyRequest newSetPreferredPhyRequest(int txPhy, int rxPhy, int phyOptions)
Requests the change of preferred PHY for this connections.
PHY LE 2M and PHY LE Coded are supported only on Android Oreo or newer.You may safely request other PHYs on older platforms, but the request will not be executedand you will get PHY LE 1M as TX and RX PHY in the callback.
- Parameters:
txPhy- preferred transmitter PHY.rxPhy- preferred receiver PHY.phyOptions- preferred coding to use when transmitting on the LE Coded PHY.
-
newReadPhyRequest
@Deprecated()@NonNull() static PhyRequest newReadPhyRequest()
Reads the current PHY for this connections.
PHY LE 2M and PHY LE Coded are supported only on Android Oreo or newer.You may safely read PHY on older platforms, but the request will not be executedand you will get PHY LE 1M as TX and RX PHY in the callback.
-
newReadRssiRequest
@Deprecated()@NonNull() static ReadRssiRequest newReadRssiRequest()
Reads the current RSSI (Received Signal Strength Indication).
-
newRefreshCacheRequest
@Deprecated()@NonNull() static SimpleRequest newRefreshCacheRequest()
Refreshes the device cache. As the refresh method is not in thepublic API (it's hidden, and on Android P it is on a light gray list) it is calledusing reflections and may be removed in some future Android release or on some devices.
There is no callback indicating when the cache has been cleared. This library assumessome time and waits. After the delay, it will start service discovery and clear thetask queue. When the service discovery finishes, the isRequiredServiceSupported and isOptionalServiceSupported willbe called and the initialization will be performed as if the device has just connected.
-
newSleepRequest
@Deprecated()@NonNull() static SleepRequest newSleepRequest(@IntRange(from = 0) long delay)
Creates new Sleep request that will postpone next request for given number of milliseconds.
- Parameters:
delay- the delay in milliseconds.
-
done
@NonNull() Request done(@NonNull() SuccessCallback callback)
Use to set a completion callback. The callback will be invoked when the operation hasfinished successfully unless the request was executed synchronously, in which case thiscallback will be ignored.
- Parameters:
callback- the callback.
-
fail
@NonNull() Request fail(@NonNull() FailCallback callback)
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(connect was never called), the invalid will be used instead, as the BluetoothDevice is not known.
This callback will be ignored if request was executed synchronously, in which casethe error will be returned as an exception.
- Parameters:
callback- the callback.
-
invalid
@NonNull() Request invalid(@NonNull() InvalidRequestCallback callback)
Use to set a callback that will be called in case the request was invalid, for examplecalled before the device was connected.This callback will be ignored if request was executed synchronously, in which casethe error will be returned as an exception.
- Parameters:
callback- the callback.
-
before
@NonNull() Request before(@NonNull() BeforeCallback callback)
Sets a callback that will be executed before the execution of this operation starts.
- Parameters:
callback- the callback.
-
then
@NonNull() Request then(@NonNull() AfterCallback callback)
Sets a callback that will be executed when the request has been processed, no matterthe request result.
Set done or fail if you need a callbackcalled in a specific situation.
This request will not be called if the BluetoothDevice was not set. Instead, InvalidRequestCallback will be called.
- Parameters:
callback- the callback.
-
enqueue
void enqueue()
Enqueues the request for asynchronous execution.
-
-
-
-