-
public class ConnectRequest extends TimeoutableRequest
The connect request is used to connect to a Bluetooth LE device. The request will end when the device gets connected, the connection timeouts, or an error occurs.
The done 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 initialize is complete (without or with errors).
-
-
Field Summary
Fields Modifier and Type Field Description private final BluetoothDevicedevice
-
Method Summary
Modifier and Type Method Description BluetoothDevicegetDevice()ConnectRequestsetHandler(@Nullable() Handler handler)Sets the handler that will be used to invoke callbacks. ConnectRequesttimeout(@IntRange(from = 0) long timeout)Sets the operation timeout. ConnectRequestdone(@NonNull() SuccessCallback callback)Use to set a completion callback. ConnectRequestfail(@NonNull() FailCallback callback)Use to set a callback that will be called in case the request has failed. ConnectRequestinvalid(@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. ConnectRequestbefore(@NonNull() BeforeCallback callback)Sets a callback that will be executed before the execution of this operation starts. ConnectRequestthen(@NonNull() AfterCallback callback)Sets a callback that will be executed when the request has been processed, no matterthe request result. ConnectRequestretry(@IntRange(from = 0) int count)Sets an optional retry count. ConnectRequestretry(@IntRange(from = 0) int count, @IntRange(from = 0) int delay)Sets an optional retry count and a delay that the process will wait before each connectionattempt. ConnectRequestuseAutoConnect(boolean autoConnect)This method replaces the shouldAutoConnect method. ConnectRequestuseAutoConnect(boolean autoConnect, boolean createDirectConnectionFirst)Sets whether to connect to the remote device just once (autoConnect == false) or to addthe address to white list of devices that will be automatically connect as soon as theybecome available (autoConnect == true). ConnectRequestusePreferredPhy(int phy)Sets the preferred PHY used for connection. voidcancelPendingConnection()This method cancels the pending connection attempt. voidcancel()This method cancels the pending connection attempt. -
Methods inherited from class no.nordicsemi.android.ble.TimeoutableRequest
await, await, enqueue, enqueue, isCancelled, setHandler, timeout -
Methods inherited from class no.nordicsemi.android.ble.Request
before, createBond, done, fail, invalid, 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, setHandler, then -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
getDevice
@NonNull() BluetoothDevice getDevice()
-
setHandler
@NonNull() ConnectRequest 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.
-
timeout
@NonNull() ConnectRequest timeout(@IntRange(from = 0) long timeout)
Sets the operation timeout.When the timeout occurs, the request will fail with REASON_TIMEOUT.
- Parameters:
timeout- the request timeout in milliseconds, 0 to disable timeout.
-
done
@NonNull() ConnectRequest done(@NonNull() SuccessCallback callback)
Use to set a completion callback. The callback will be invoked when the operation hasfinished successfully unless await or its variant was used, in which case thiscallback will be ignored.
The done callback will also be called when one or more of initialization requests hasfailed due to a reason other than disconnect event. This is because onDeviceReady is called no matterif the requests succeeded, or not. Set failure callbacks to initialization requeststo get information about failures.
- Parameters:
callback- the callback.
-
fail
@NonNull() ConnectRequest 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() ConnectRequest 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() ConnectRequest before(@NonNull() BeforeCallback callback)
Sets a callback that will be executed before the execution of this operation starts.
- Parameters:
callback- the callback.
-
then
@NonNull() ConnectRequest 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.
-
retry
ConnectRequest retry(@IntRange(from = 0) int count)
Sets an optional retry count. The BleManager will do that many attempts to connect to thedevice in case of an error. The library will NOT retry if the device is not reachable,that is when the 30 sec. timeout occurs. In that case the app should scan beforeconnecting, to make sure the target is in range.
- Parameters:
count- how many times should the BleManager retry to connect.
-
retry
ConnectRequest retry(@IntRange(from = 0) int count, @IntRange(from = 0) int delay)
Sets an optional retry count and a delay that the process will wait before each connectionattempt. The library will NOT retry if the device is not reachable, that is when the 30 sec.timeout occurs. In that case the app should scan before connecting, to make sure thetarget is in range.
- Parameters:
count- how many times should the BleManager retry to connect.delay- the delay between each connection attempt, in milliseconds.
-
useAutoConnect
ConnectRequest useAutoConnect(boolean autoConnect)
This method replaces the shouldAutoConnect method.
Sets whether to connect to the remote device just once (false) or to add the address towhite 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 periodicallyfor devices from the white list and, if an advertising packet is received from such, it triesto connect to it. When the connection is lost, the system will keep trying to reconnect toit. If method is called with parameter set to true, and the connection to the device islost, the onLinkLossOccurred callback iscalled instead of onDeviceDisconnected.
This feature works much better on newer Android phone models and may have issues on olderphones.
This method should only be used with bonded devices, as otherwise the device may changeit'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 connectGatt). This isto make it quick as the user most probably waits for a quick response. If autoConnect isused (true), the following connections will be done using connect,which forces the autoConnect parameter to true.
- Parameters:
autoConnect- true to use autoConnect feature on the second and following connections.The first connection is always done with autoConnect parameter equal tofalse, to make it faster and allow to timeout it the device is unreachable.Default value is false.
-
useAutoConnect
ConnectRequest useAutoConnect(boolean autoConnect, boolean createDirectConnectionFirst)
Sets whether to connect to the remote device just once (autoConnect == false) or to addthe address to white list of devices that will be automatically connect as soon as theybecome available (autoConnect == true). In the latter case, if Bluetooth adapter is enabled,Android scans periodically for devices from the white list and, if an advertising packetis received from such, it tries to connect to it.When the connection is lost, the system will keep trying to reconnect toit. If method is called with autoConnect set to true, and the connection to the device islost, the onLinkLossOccurred callback iscalled instead of onDeviceDisconnected.
This feature works much better on newer Android phone models and may have issues on olderphones.
This method should only be used with bonded devices, as otherwise the device may changeit'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.
If createDirectConnectionFirst is set to true, the first connection to a device will always becreated with autoConnect flag to false(see connectGatt). This isto make it quick as the user most probably waits for a quick response. If autoConnect isused (true), the following connections will be done using connect,which forces the autoConnect parameter to true.If autoConnect is used (true) and createDirectConnectionFirst is set to false, the connectionto a device will be created with autoConnect flag to true from the start.
- Parameters:
autoConnect- true to use autoConnect feature.createDirectConnectionFirst- If true, the first connection is always done with autoConnectparameter equal to false, to make it faster and allow to timeoutif the device is unreachable.If false, the connection to a device will be created withautoConnect flag to true from the start.
-
usePreferredPhy
ConnectRequest usePreferredPhy(int phy)
Sets the preferred PHY used for connection. The value should be a bitmask composed of PHY_LE_1M_MASK, PHY_LE_2M_MASK or PHY_LE_CODED_MASK.
Different PHYs are available only on more recent devices with Android 8+.Check isLe2MPhySupported and isLeCodedPhySupported if required PHYs are supported by thisAndroid device. The default PHY is PHY_LE_1M_MASK.
- Parameters:
phy- preferred PHY for connections to remote LE device.
-
cancelPendingConnection
void cancelPendingConnection()
This method cancels the pending connection attempt. All requests enqueued after this onewill be removed form the queue.
This method does nothing if the connection request has finished successfully, orwith an error.
-
cancel
void cancel()
This method cancels the pending connection attempt. All requests enqueued after this onewill be removed form the queue.
This method does nothing if the connection request has finished successfully, orwith an error.
-
-
-
-