public class ConnectRequest extends TimeoutableRequest
The done(SuccessCallback) callback will be called after the device is ready, that is
when it is connected, the services were discovered, the required services were found and the
initialization queue set in BleManagerHandler.initialize() is complete
(without or with errors).
timeouthandler, requestHandler| Modifier and Type | Method and Description |
|---|---|
ConnectRequest |
before(BeforeCallback callback)
Sets a callback that will be executed before the execution of this operation starts.
|
ConnectRequest |
done(SuccessCallback callback)
Use to set a completion callback.
|
ConnectRequest |
fail(FailCallback callback)
Use to set a callback that will be called in case the request has failed.
|
android.bluetooth.BluetoothDevice |
getDevice() |
ConnectRequest |
invalid(InvalidRequestCallback callback)
Use to set a callback that will be called in case the request was invalid, for example
called before the device was connected.
|
ConnectRequest |
retry(int count)
Sets an optional retry count.
|
ConnectRequest |
retry(int count,
int delay)
Sets an optional retry count and a delay that the process will wait before each connection
attempt.
|
ConnectRequest |
setHandler(android.os.Handler handler)
Sets the handler that will be used to invoke callbacks.
|
ConnectRequest |
timeout(long timeout)
Sets the operation timeout.
|
ConnectRequest |
useAutoConnect(boolean autoConnect)
This method replaces the
BleManager.shouldAutoConnect() method. |
ConnectRequest |
usePreferredPhy(int phy)
Sets the preferred PHY used for connection.
|
await, await, enqueue, enqueuecreateBond, newConnectionPriorityRequest, newDisableBatteryLevelNotificationsRequest, newDisableIndicationsRequest, newDisableNotificationsRequest, newEnableBatteryLevelNotificationsRequest, newEnableIndicationsRequest, newEnableNotificationsRequest, newMtuRequest, newReadBatteryLevelRequest, newReadPhyRequest, newReadRequest, newReadRequest, newReadRssiRequest, newRefreshCacheRequest, newSetPreferredPhyRequest, newSleepRequest, newWaitForIndicationRequest, newWaitForNotificationRequest, newWriteRequest, newWriteRequest, newWriteRequest, newWriteRequest, newWriteRequest, newWriteRequest, removeBond@NonNull public ConnectRequest setHandler(@NonNull android.os.Handler handler)
RequestBleManager will be used.setHandler in class TimeoutableRequesthandler - The handler to invoke callbacks for this request.@NonNull public ConnectRequest timeout(long timeout)
TimeoutableRequestFailCallback.REASON_TIMEOUT.timeout in class TimeoutableRequesttimeout - the request timeout in milliseconds, 0 to disable timeout.@NonNull public ConnectRequest done(@NonNull SuccessCallback callback)
TimeoutableRequest.await() or its variant was used, in which case this
callback will be ignored.
The done callback will also be called when one or more of initialization requests has
failed due to a reason other than disconnect event. This is because
BleManagerCallbacks.onDeviceReady(BluetoothDevice) is called no matter
if the requests succeeded, or not. Set failure callbacks to initialization requests
to get information about failures.
@NonNull public ConnectRequest fail(@NonNull FailCallback callback)
RequestBleManager.connect(BluetoothDevice) was never called), the
Request.invalid(InvalidRequestCallback) will be used instead, as the
BluetoothDevice is not known.
This callback will be ignored if request was executed synchronously, in which case the error will be returned as an exception.
@NonNull public ConnectRequest invalid(@NonNull InvalidRequestCallback callback)
Request@NonNull public ConnectRequest before(@NonNull BeforeCallback callback)
Requestpublic ConnectRequest retry(int count)
count - how many times should the BleManager retry to connect.retry(int, int)public ConnectRequest retry(int count, int delay)
count - how many times should the BleManager retry to connect.delay - the delay between each connection attempt, in milliseconds.
The real delay will be 200 ms longer than specified, as
Object.clone() is estimated to last
200 ms.retry(int)public ConnectRequest useAutoConnect(boolean autoConnect)
BleManager.shouldAutoConnect() method.
Sets whether to connect to the remote device just once (false) or to add the address to
white list of devices that will be automatically connect as soon as they become available
(true). In the latter case, if Bluetooth adapter is enabled, Android scans periodically
for devices from the white list and, if an advertising packet is received from such, it tries
to connect to it. When the connection is lost, the system will keep trying to reconnect to
it. If method is called with parameter set to true, and the connection to the device is
lost, the BleManagerCallbacks.onLinkLossOccurred(BluetoothDevice) callback is
called instead of BleManagerCallbacks.onDeviceDisconnected(BluetoothDevice).
This feature works much better on newer Android phone models and may have issues on older phones.
This method should only be used with bonded devices, as otherwise the device may change it's address. It will however work also with non-bonded devices with private static address. A connection attempt to a non-bonded device with private resolvable address will fail.
The first connection to a device will always be created with autoConnect flag to false
(see BluetoothDevice.connectGatt(Context, boolean, BluetoothGattCallback)). This is
to make it quick as the user most probably waits for a quick response. If autoConnect is
used (true), the following connections will be done using BluetoothGatt.connect(),
which forces the autoConnect parameter to true.
autoConnect - true to use autoConnect feature on the second and following connections.
The first connection is always done with autoConnect parameter equal to
false, to make it faster and allow to timeout it the device is unreachable.
Default value is false.public ConnectRequest usePreferredPhy(int phy)
PhyRequest.PHY_LE_1M_MASK, PhyRequest.PHY_LE_2M_MASK or
PhyRequest.PHY_LE_CODED_MASK.
Different PHYs are available only on more recent devices with Android 8+.
Check BluetoothAdapter.isLe2MPhySupported() and
BluetoothAdapter.isLeCodedPhySupported() if required PHYs are supported by this
Android device. The default PHY is PhyRequest.PHY_LE_1M_MASK.
phy - preferred PHY for connections to remote LE device. Bitwise OR of any of
PhyRequest.PHY_LE_1M_MASK, PhyRequest.PHY_LE_2M_MASK,
and PhyRequest.PHY_LE_CODED_MASK. This option does not take effect
if autoConnect is set to true.@NonNull public android.bluetooth.BluetoothDevice getDevice()