Skip to content

Commit

Permalink
Add the reset access
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Bourge committed Jun 8, 2018
1 parent 09c96c6 commit 6f5b8fb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public Single<RxBleDeviceServices> discoverServices(long timeout, @NonNull TimeU
return Single.just(rxBleDeviceServices);
}

@Override
public Single<RxBleDeviceServices> discoverServices(long timeout, @NonNull TimeUnit timeUnit, Boolean clearCache) {
return Single.just(rxBleDeviceServices);
}

@Override
public Single<BluetoothGattCharacteristic> getCharacteristic(@NonNull final UUID characteristicUuid) {
return discoverServices()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@ interface WriteOperationAckStrategy extends ObservableTransformer<Boolean, Boole
*/
Single<RxBleDeviceServices> discoverServices(@IntRange(from = 1) long timeout, @NonNull TimeUnit timeUnit);

/**
* Performs GATT service discovery and emits discovered results. After service discovery you can walk through
* {@link android.bluetooth.BluetoothGattService}s and {@link BluetoothGattCharacteristic}s.
* <p>
* Result of the discovery is cached internally so consecutive calls won't trigger BLE operation and can be
* considered relatively lightweight.
*
* Timeouts after specified amount of time.
*
* @param timeout multiplier of TimeUnit after which the discovery will timeout in case of no return values
* @param timeUnit TimeUnit for the timeout
* @return Observable emitting result a GATT service discovery.
* @throws BleGattCannotStartException with {@link BleGattOperationType#SERVICE_DISCOVERY} type, when it wasn't possible to start
* the discovery for internal reasons.
* @throws BleGattException in case of GATT operation error with {@link BleGattOperationType#SERVICE_DISCOVERY} type.
*/
Single<RxBleDeviceServices> discoverServices(@IntRange(from = 1) long timeout, @NonNull TimeUnit timeUnit, Boolean clearCache);

/**
* @see #setupNotification(UUID, NotificationSetupMode) with default setup mode.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public Single<RxBleDeviceServices> discoverServices(long timeout, @NonNull TimeU
return serviceDiscoveryManager.getDiscoverServicesSingle(timeout, timeUnit);
}

@Override
public Single<RxBleDeviceServices> discoverServices(long timeout, @NonNull TimeUnit timeUnit, Boolean clearCache) {
return serviceDiscoveryManager.getDiscoverServicesSingle(timeout, timeUnit, clearCache);
}

@Override
@Deprecated
public Single<BluetoothGattCharacteristic> getCharacteristic(@NonNull final UUID characteristicUuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ public void accept(Disposable disposable) throws Exception {
}
}

Single<RxBleDeviceServices> getDiscoverServicesSingle(final long timeout, final TimeUnit timeoutTimeUnit, Boolean clearCache) {
if (hasCachedResults) {
// optimisation to decrease the number of allocations
return deviceServicesObservable;
} else {
if (clearCache) {
reset();
}
return deviceServicesObservable.doOnSubscribe(
new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
timeoutBehaviorSubject.onNext(new TimeoutConfiguration(timeout, timeoutTimeUnit, Schedulers.computation()));
}
});
}
}

private void reset() {
hasCachedResults = false;
this.deviceServicesObservable = getListOfServicesFromGatt()
Expand Down

0 comments on commit 6f5b8fb

Please sign in to comment.