-
- All Implemented Interfaces:
-
com.polidea.rxandroidble2.RxBleConnection
public class RxBleConnectionImpl implements RxBleConnection
-
-
Constructor Summary
Constructors Constructor Description RxBleConnectionImpl(ConnectionOperationQueue operationQueue, RxBleGattCallback gattCallback, BluetoothGatt bluetoothGatt, ServiceDiscoveryManager serviceDiscoveryManager, NotificationAndIndicationManager notificationIndicationManager, MtuProvider mtuProvider, DescriptorWriter descriptorWriter, OperationsProvider operationProvider, Provider<RxBleConnection.LongWriteOperationBuilder> longWriteOperationBuilderProvider, Scheduler callbackScheduler, IllegalOperationChecker illegalOperationChecker)
-
Method Summary
Modifier and Type Method Description RxBleConnection.LongWriteOperationBuildercreateNewLongWriteBuilder()Returns a LongWriteOperationBuilder used for creating atomic write operations divided into multiple writes. CompletablerequestConnectionPriority(int connectionPriority, long delay, @NonNull() TimeUnit timeUnit)Performs a GATT request connection priority operation, which requests a connection parameterupdate on the remote device. Single<Integer>requestMtu(int mtu)Performs GATT MTU (Maximum Transfer Unit) request.Timeouts after 10 seconds. intgetMtu()Get currently negotiated MTU value. Single<RxBleDeviceServices>discoverServices()Performs GATT service discovery and emits discovered results. Single<RxBleDeviceServices>discoverServices(long timeout, @NonNull() TimeUnit timeUnit)Performs GATT service discovery and emits discovered results. Single<BluetoothGattCharacteristic>getCharacteristic(@NonNull() UUID characteristicUuid)Convenience method for characteristic retrieval. Observable<Observable<Array<byte>>>setupNotification(@NonNull() UUID characteristicUuid)Observable<Observable<Array<byte>>>setupNotification(@NonNull() BluetoothGattCharacteristic characteristic)Observable<Observable<Array<byte>>>setupNotification(@NonNull() UUID characteristicUuid, @NonNull() NotificationSetupMode setupMode)Setup characteristic notification in order to receive callbacks when given characteristic has been changed. Observable<Observable<Array<byte>>>setupNotification(@NonNull() BluetoothGattCharacteristic characteristic, @NonNull() NotificationSetupMode setupMode)Setup characteristic notification in order to receive callbacks when given characteristic has been changed. Observable<Observable<Array<byte>>>setupIndication(@NonNull() UUID characteristicUuid)Observable<Observable<Array<byte>>>setupIndication(@NonNull() BluetoothGattCharacteristic characteristic)Observable<Observable<Array<byte>>>setupIndication(@NonNull() UUID characteristicUuid, @NonNull() NotificationSetupMode setupMode)Setup characteristic indication in order to receive callbacks when given characteristic has been changed. Observable<Observable<Array<byte>>>setupIndication(@NonNull() BluetoothGattCharacteristic characteristic, @NonNull() NotificationSetupMode setupMode)Setup characteristic indication in order to receive callbacks when given characteristic has been changed. Single<Array<byte>>readCharacteristic(@NonNull() UUID characteristicUuid)Performs GATT read operation on a characteristic with given UUID. Single<Array<byte>>readCharacteristic(@NonNull() BluetoothGattCharacteristic characteristic)Performs GATT read operation on a given characteristic. Single<Array<byte>>writeCharacteristic(@NonNull() UUID characteristicUuid, @NonNull() Array<byte> data)Performs GATT write operation on a characteristic with given UUID. Single<Array<byte>>writeCharacteristic(@NonNull() BluetoothGattCharacteristic characteristic, @NonNull() Array<byte> data)Performs GATT write operation on a given characteristic. Single<Array<byte>>readDescriptor(@NonNull() UUID serviceUuid, @NonNull() UUID characteristicUuid, @NonNull() UUID descriptorUuid)Performs GATT read operation on a descriptor from a characteristic with a given UUID from a service with a given UUID. Single<Array<byte>>readDescriptor(@NonNull() BluetoothGattDescriptor descriptor)Performs GATT read operation on a descriptor from a characteristic with a given UUID from a service with a given UUID. CompletablewriteDescriptor(@NonNull() UUID serviceUuid, @NonNull() UUID characteristicUuid, @NonNull() UUID descriptorUuid, @NonNull() Array<byte> data)Performs GATT write operation on a descriptor from a characteristic with a given UUID from a service with a given UUID. CompletablewriteDescriptor(@NonNull() BluetoothGattDescriptor bluetoothGattDescriptor, @NonNull() Array<byte> data)Performs GATT write operation on a given descriptor. Single<Integer>readRssi()Performs GATT read rssi operation. Observable<ConnectionParameters>observeConnectionParametersUpdates()Allows observing of connection parameters updates. <T> Observable<T>queue(@NonNull() RxBleCustomOperation<T> operation)This method requires deep knowledge of RxAndroidBLE internals. <T> Observable<T>queue(@NonNull() RxBleCustomOperation<T> operation, @NonNull() Priority priority)This method requires deep knowledge of RxAndroidBLE internals. -
Methods inherited from class com.polidea.rxandroidble2.RxBleConnection
discoverServices, getCharacteristic, queue, queue, readDescriptor, requestConnectionPriority, requestMtu, setupIndication, setupIndication, setupNotification, setupNotification, writeCharacteristic, writeCharacteristic, writeDescriptor, writeDescriptor -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
RxBleConnectionImpl
RxBleConnectionImpl(ConnectionOperationQueue operationQueue, RxBleGattCallback gattCallback, BluetoothGatt bluetoothGatt, ServiceDiscoveryManager serviceDiscoveryManager, NotificationAndIndicationManager notificationIndicationManager, MtuProvider mtuProvider, DescriptorWriter descriptorWriter, OperationsProvider operationProvider, Provider<RxBleConnection.LongWriteOperationBuilder> longWriteOperationBuilderProvider, Scheduler callbackScheduler, IllegalOperationChecker illegalOperationChecker)
-
-
Method Detail
-
createNewLongWriteBuilder
RxBleConnection.LongWriteOperationBuilder createNewLongWriteBuilder()
Returns a LongWriteOperationBuilder used for creating atomic write operations divided into multiple writes.This is useful when the BLE peripheral does NOT handle long writes on the firmware level (in which situationa regular writeCharacteristic should be sufficient.
-
requestConnectionPriority
Completable requestConnectionPriority(int connectionPriority, long delay, @NonNull() TimeUnit timeUnit)
Performs a GATT request connection priority operation, which requests a connection parameterupdate on the remote device. NOTE: peripheral may silently decline request.
Tells Android to request an update of connection interval and slave latency parameters.Using CONNECTION_PRIORITY_HIGH will increase transmission speed andbattery drainage, if accepted by the device, compared to CONNECTION_PRIORITY_BALANCED,while using CONNECTION_PRIORITY_LOW_POWER will cause higher latenciesand save battery, if accepted by the device, compared to CONNECTION_PRIORITY_BALANCED.
By default connection is balanced.
NOTE: Till API 26 (8.0) there was no method like `BluetoothGattCallback.onConnectionPriorityChanged()`. It was not possible to knowif the request was successful (accepted by the peripheral). This also causes the need of specifying when the request is consideredfinished (parameter delay and timeUnit). Since API 26 the mentioned callback is hidden, yet possible to use. It is not used toautomatically complete this request due to Android OS changing connection parameters on its own. It is not possible to determinewhich callback is actually finishing the request nor if the Android OS will not change the parameters right after the request.If access to the callback is a must for your implementation you may achieve it by setHiddenNativeCallback via queue and createa custom request connection priority operation.
As of Lollipop the connection parameters are:* CONNECTION_PRIORITY_BALANCED: min interval 30 ms, max interval 50 ms, slave latency 0* CONNECTION_PRIORITY_HIGH: min interval 7.5 ms, max interval 10ms, slave latency 0* CONNECTION_PRIORITY_LOW_POWER: min interval 100ms, max interval 125 ms, slave latency 2
Returned completable completes after the specified delay if and only if requestConnectionPriority has returned true.
- Parameters:
connectionPriority- requested connection prioritydelay- delay after which operation is assumed to be successful (must be shorter than 30 seconds)timeUnit- time unit of the delay
-
requestMtu
Single<Integer> requestMtu(int mtu)
Performs GATT MTU (Maximum Transfer Unit) request.Timeouts after 10 seconds.
-
getMtu
int getMtu()
Get currently negotiated MTU value. On pre-lollipop Android versions it will always return 23.
-
discoverServices
Single<RxBleDeviceServices> discoverServices()
Performs GATT service discovery and emits discovered results. After service discovery you can walk through android.bluetooth.BluetoothGattServices and BluetoothGattCharacteristics.
Result of the discovery is cached internally so consecutive calls won't trigger BLE operation and can beconsidered relatively lightweight.Uses default timeout of 20 seconds
-
discoverServices
Single<RxBleDeviceServices> discoverServices(long timeout, @NonNull() TimeUnit timeUnit)
Performs GATT service discovery and emits discovered results. After service discovery you can walk through android.bluetooth.BluetoothGattServices and BluetoothGattCharacteristics.
Result of the discovery is cached internally so consecutive calls won't trigger BLE operation and can beconsidered relatively lightweight.Timeouts after specified amount of time.
- Parameters:
timeout- multiplier of TimeUnit after which the discovery will timeout in case of no return valuestimeUnit- TimeUnit for the timeout
-
getCharacteristic
@Deprecated() Single<BluetoothGattCharacteristic> getCharacteristic(@NonNull() UUID characteristicUuid)
Convenience method for characteristic retrieval. First step is service discovery which is followed by service/characteristictraversal. This is an alias to:
- Parameters:
characteristicUuid- Requested characteristic UUID.
-
setupNotification
Observable<Observable<Array<byte>>> setupNotification(@NonNull() UUID characteristicUuid)
-
setupNotification
Observable<Observable<Array<byte>>> setupNotification(@NonNull() BluetoothGattCharacteristic characteristic)
-
setupNotification
Observable<Observable<Array<byte>>> setupNotification(@NonNull() UUID characteristicUuid, @NonNull() NotificationSetupMode setupMode)
Setup characteristic notification in order to receive callbacks when given characteristic has been changed. Returned observable willemit Observableonce the notification setup has been completed. It is possible to setup more observables for the samecharacteristic and the lifecycle of the notification will be shared among them.
Notification is automatically unregistered once this observable is unsubscribed.NOTE: due to stateful nature of characteristics if one will setupIndication() before setupNotification()the notification will not be set up and will emit an BleCharacteristicNotificationOfOtherTypeAlreadySetException
- Parameters:
characteristicUuid- Characteristic UUID for notification setup.setupMode- Configures how the notification is set up.
-
setupNotification
Observable<Observable<Array<byte>>> setupNotification(@NonNull() BluetoothGattCharacteristic characteristic, @NonNull() NotificationSetupMode setupMode)
Setup characteristic notification in order to receive callbacks when given characteristic has been changed. Returned observable willemit Observableonce the notification setup has been completed. It is possible to setup more observables for the samecharacteristic and the lifecycle of the notification will be shared among them.
Notification is automatically unregistered once this observable is unsubscribed.
NOTE: due to stateful nature of characteristics if one will setupIndication() before setupNotification()the notification will not be set up and will emit an BleCharacteristicNotificationOfOtherTypeAlreadySetException
The characteristic can be retrieved from com.polidea.rxandroidble2.RxBleDeviceServices emitted from discoverServices
- Parameters:
characteristic- Characteristic for notification setup.setupMode- Configures how the notification is set up.
-
setupIndication
Observable<Observable<Array<byte>>> setupIndication(@NonNull() UUID characteristicUuid)
-
setupIndication
Observable<Observable<Array<byte>>> setupIndication(@NonNull() BluetoothGattCharacteristic characteristic)
-
setupIndication
Observable<Observable<Array<byte>>> setupIndication(@NonNull() UUID characteristicUuid, @NonNull() NotificationSetupMode setupMode)
Setup characteristic indication in order to receive callbacks when given characteristic has been changed. Returned observable willemit Observableonce the indication setup has been completed. It is possible to setup more observables for the samecharacteristic and the lifecycle of the indication will be shared among them.
Indication is automatically unregistered once this observable is unsubscribed.
NOTE: due to stateful nature of characteristics if one will setupNotification() before setupIndication()the indication will not be set up and will emit an BleCharacteristicNotificationOfOtherTypeAlreadySetException
- Parameters:
characteristicUuid- Characteristic UUID for indication setup.setupMode- Configures how the notification is set up.
-
setupIndication
Observable<Observable<Array<byte>>> setupIndication(@NonNull() BluetoothGattCharacteristic characteristic, @NonNull() NotificationSetupMode setupMode)
Setup characteristic indication in order to receive callbacks when given characteristic has been changed. Returned observable willemit Observableonce the indication setup has been completed. It is possible to setup more observables for the samecharacteristic and the lifecycle of the indication will be shared among them.
Indication is automatically unregistered once this observable is unsubscribed.
NOTE: due to stateful nature of characteristics if one will setupNotification() before setupIndication()the indication will not be set up and will emit an BleCharacteristicNotificationOfOtherTypeAlreadySetException
The characteristic can be retrieved from com.polidea.rxandroidble2.RxBleDeviceServices emitted from discoverServices
- Parameters:
characteristic- Characteristic for indication setup.setupMode- Configures how the notification is set up.
-
readCharacteristic
Single<Array<byte>> readCharacteristic(@NonNull() UUID characteristicUuid)
Performs GATT read operation on a characteristic with given UUID.
- Parameters:
characteristicUuid- Requested characteristic UUID.
-
readCharacteristic
Single<Array<byte>> readCharacteristic(@NonNull() BluetoothGattCharacteristic characteristic)
Performs GATT read operation on a given characteristic.
- Parameters:
characteristic- Requested characteristic.
-
writeCharacteristic
Single<Array<byte>> writeCharacteristic(@NonNull() UUID characteristicUuid, @NonNull() Array<byte> data)
Performs GATT write operation on a characteristic with given UUID.
- Parameters:
characteristicUuid- Requested characteristic UUID.
-
writeCharacteristic
Single<Array<byte>> writeCharacteristic(@NonNull() BluetoothGattCharacteristic characteristic, @NonNull() Array<byte> data)
Performs GATT write operation on a given characteristic.
- Parameters:
data- the byte array to write
-
readDescriptor
Single<Array<byte>> readDescriptor(@NonNull() UUID serviceUuid, @NonNull() UUID characteristicUuid, @NonNull() UUID descriptorUuid)
Performs GATT read operation on a descriptor from a characteristic with a given UUID from a service with a given UUID.
- Parameters:
serviceUuid- Requested android.bluetooth.BluetoothGattService UUIDcharacteristicUuid- Requested android.bluetooth.BluetoothGattCharacteristic UUIDdescriptorUuid- Requested android.bluetooth.BluetoothGattDescriptor UUID
-
readDescriptor
Single<Array<byte>> readDescriptor(@NonNull() BluetoothGattDescriptor descriptor)
Performs GATT read operation on a descriptor from a characteristic with a given UUID from a service with a given UUID.
- Parameters:
descriptor- Requested android.bluetooth.BluetoothGattDescriptor
-
writeDescriptor
Completable writeDescriptor(@NonNull() UUID serviceUuid, @NonNull() UUID characteristicUuid, @NonNull() UUID descriptorUuid, @NonNull() Array<byte> data)
Performs GATT write operation on a descriptor from a characteristic with a given UUID from a service with a given UUID.
- Parameters:
serviceUuid- Requested android.bluetooth.BluetoothGattDescriptor UUIDcharacteristicUuid- Requested android.bluetooth.BluetoothGattCharacteristic UUIDdescriptorUuid- Requested android.bluetooth.BluetoothGattDescriptor UUID
-
writeDescriptor
Completable writeDescriptor(@NonNull() BluetoothGattDescriptor bluetoothGattDescriptor, @NonNull() Array<byte> data)
Performs GATT write operation on a given descriptor.
-
observeConnectionParametersUpdates
Observable<ConnectionParameters> observeConnectionParametersUpdates()
Allows observing of connection parameters updates. This is part of Android's hidden API and therefore is not guaranteed to work.It was added in API 26 (8.0, Oreo) in android.bluetooth.BluetoothGattCallback and will not work on lower API levels at all.The system does change the parameters on its own at the beginning of connection (i.e. to speed up service discovery process).The parameters may be further changed by using requestConnectionPriority.
-
queue
<T> Observable<T> queue(@NonNull() RxBleCustomOperation<T> operation)
This method requires deep knowledge of RxAndroidBLE internals. Use it only as a last resort if you knowwhat your are doing.
Queue an operation for future execution. The method accepts a RxBleCustomOperation concrete implementationand will queue it inside connection operation queue. When ready to execute, the Observable returnedby the asObservable will besubscribed to.
Every event emitted by the Observable returned by asObservable will be forwardedto the Observable returned by this method.
You must ensure the custom operation's Observable does terminate either via
{@code onCompleted}or{@code onError(Throwable)}. Otherwise, the internal queue orchestrator will wait forever foryour Observable to complete. Normal queue processing will be resumed after the Observable returned by asObservable completes.The operation will be added to the queue using a NORMAL priority.
- Parameters:
operation- The custom operation to queue.
-
queue
<T> Observable<T> queue(@NonNull() RxBleCustomOperation<T> operation, @NonNull() Priority priority)
This method requires deep knowledge of RxAndroidBLE internals. Use it only as a last resort if you knowwhat your are doing.
Queue an operation for future execution. The method accepts a RxBleCustomOperation concrete implementationand will queue it inside connection operation queue. When ready to execute, the Observable returnedby the asObservable will besubscribed to.
Every event emitted by the Observable returned by asObservable will be forwardedto the Observable returned by this method.
You must ensure the custom operation's Observable does terminate either via
{@code onCompleted}or{@code onError(Throwable)}. Otherwise, the internal queue orchestrator will wait forever foryour Observable to complete. Normal queue processing will be resumed after the Observable returned by asObservable completes.- Parameters:
operation- The custom operation to queue.priority- Priority affected to this operation
-
-
-
-