From ea243e6b5c9b1a37db49aba71317499de4b84c81 Mon Sep 17 00:00:00 2001 From: Dariusz Seweryn Date: Wed, 26 Apr 2017 11:19:31 +0200 Subject: [PATCH] Fixed test and behaviour of `RxBleRadioOperationDisconnect` when no `BluetoothGatt` is available. https://github.com/Polidea/RxAndroidBle/issues/178 Reviewers: pawel.urban Reviewed By: pawel.urban Differential Revision: https://phabricator.polidea.com/D2325 --- .../internal/RxBleRadioOperation.java | 2 ++ .../RxBleSingleGattRadioOperation.java | 7 ----- .../connection/RxBleConnectionImpl.java | 7 +---- ...RadioOperationCharacteristicLongWrite.java | 1 - .../RxBleRadioOperationConnect.java | 26 ++++--------------- .../RxBleRadioOperationDisconnect.java | 6 ----- .../RxBleRadioOperationDisconnectTest.groovy | 10 +++++-- 7 files changed, 16 insertions(+), 43 deletions(-) diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/RxBleRadioOperation.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/RxBleRadioOperation.java index 65086c1e0..dda170c81 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/RxBleRadioOperation.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/RxBleRadioOperation.java @@ -94,8 +94,10 @@ protected final void onNext(T next) { /** * A convenience method for calling the Subscriber's onCompleted() + * Calling this method automatically releases the radio -> calls releaseRadio(). */ protected final void onCompleted() { + releaseRadio(); replaySubject.onCompleted(); } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/RxBleSingleGattRadioOperation.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/RxBleSingleGattRadioOperation.java index 4d3ac9337..89fbb3d2e 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/RxBleSingleGattRadioOperation.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/RxBleSingleGattRadioOperation.java @@ -15,7 +15,6 @@ import rx.Observable; import rx.Scheduler; import rx.Subscription; -import rx.functions.Action0; public abstract class RxBleSingleGattRadioOperation extends RxBleRadioOperation { @@ -44,12 +43,6 @@ final protected void protectedRun() throws Throwable { timeoutFallbackProcedure(bluetoothGatt, rxBleGattCallback, timeoutConfiguration.timeoutScheduler), timeoutConfiguration.timeoutScheduler ) - .doOnCompleted(new Action0() { - @Override - public void call() { - RxBleSingleGattRadioOperation.this.releaseRadio(); - } - }) .subscribe(getSubscriber()); boolean success = startOperation(bluetoothGatt); diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/connection/RxBleConnectionImpl.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/connection/RxBleConnectionImpl.java index a7f12c245..d68e40137 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/connection/RxBleConnectionImpl.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/connection/RxBleConnectionImpl.java @@ -482,12 +482,7 @@ protected void protectedRun() throws Throwable { throw new IllegalArgumentException("The custom operation asObservable method must return a non-null observable"); } - operationObservable.doOnCompleted(new Action0() { - @Override - public void call() { - releaseRadio(); - } - }).subscribe(getSubscriber()); + operationObservable.subscribe(getSubscriber()); } @Override diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationCharacteristicLongWrite.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationCharacteristicLongWrite.java index 6541e4ffa..65fc9ecea 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationCharacteristicLongWrite.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationCharacteristicLongWrite.java @@ -93,7 +93,6 @@ protected void protectedRun() throws Throwable { public void call() { onNext(bytesToWrite); onCompleted(); - releaseRadio(); } }, new Action1() { diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationConnect.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationConnect.java index 02c507e45..4a7ad2367 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationConnect.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationConnect.java @@ -72,17 +72,6 @@ public RxBleRadioOperationConnect build() { private final BluetoothGattProvider bluetoothGattProvider; private final TimeoutConfiguration connectTimeout; private final boolean autoConnect; - private final Runnable releaseRadioRunnable = new Runnable() { - @Override - public void run() { - releaseRadio(); - } - }; - private final Runnable emptyRunnable = new Runnable() { - @Override - public void run() { - } - }; private final BehaviorSubject isSubscribed = BehaviorSubject.create(); @@ -124,9 +113,6 @@ public Observable asObservable() { @Override protected void protectedRun() { - final Runnable onConnectionEstablishedRunnable = autoConnect ? emptyRunnable : releaseRadioRunnable; - final Runnable onConnectCalledRunnable = autoConnect ? releaseRadioRunnable : emptyRunnable; - getConnectedBluetoothGatt() .compose(wrapWithTimeoutWhenNotAutoconnecting()) // when there are no subscribers there is no point of continuing work -> next will be disconnect operation @@ -136,12 +122,6 @@ public void call(Boolean noSubscribers) { RxBleLog.d("No subscribers, finishing operation"); } })) - .doOnCompleted(new Action0() { - @Override - public void call() { - onConnectionEstablishedRunnable.run(); - } - }) .doOnNext(new Action1() { @Override public void call(BluetoothGatt ignored) { @@ -149,7 +129,11 @@ public void call(BluetoothGatt ignored) { } }) .subscribe(getSubscriber()); - onConnectCalledRunnable.run(); + + if (autoConnect) { + // with autoConnect the connection may be established after a really long time + releaseRadio(); + } } private Observable.Transformer wrapWithTimeoutWhenNotAutoconnecting() { diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationDisconnect.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationDisconnect.java index 03982adca..8e45c5752 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationDisconnect.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationDisconnect.java @@ -62,12 +62,6 @@ protected void protectedRun() { onCompleted(); } else { (isDisconnected(bluetoothGatt) ? just(bluetoothGatt) : disconnect(bluetoothGatt)) - .doOnTerminate(new Action0() { - @Override - public void call() { - releaseRadio(); - } - }) .observeOn(mainThreadScheduler) .subscribe( new Action1() { diff --git a/rxandroidble/src/test/groovy/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationDisconnectTest.groovy b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationDisconnectTest.groovy index 718c0b95e..00c5cf2bd 100644 --- a/rxandroidble/src/test/groovy/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationDisconnectTest.groovy +++ b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/internal/operations/RxBleRadioOperationDisconnectTest.groovy @@ -55,15 +55,19 @@ public class RxBleRadioOperationDisconnectTest extends Specification { RxAndroidPlugins.getInstance().reset() } - def setup() { + private def testWithGattProviderReturning(BluetoothGatt providedBluetoothGatt) { mockBluetoothGattProvider = Mock(BluetoothGattProvider) - mockBluetoothGattProvider.getBluetoothGatt() >>mockBluetoothGatt + mockBluetoothGattProvider.getBluetoothGatt() >> providedBluetoothGatt mockGattCallback.getOnConnectionStateChange() >> connectionStatePublishSubject mockBluetoothGatt.getDevice() >> mockDevice prepareObjectUnderTest() } def "should complete if AtomicReference contains null and then release the radio"() { + + given: + testWithGattProviderReturning(null) + when: objectUnderTest.run() @@ -78,6 +82,7 @@ public class RxBleRadioOperationDisconnectTest extends Specification { def "should call BluetoothGatt.close() if BluetoothGatt is disconnected at the time of running and then release the radio"() { given: + testWithGattProviderReturning(mockBluetoothGatt) mockBluetoothManager.getConnectionState(mockDevice, GATT) >> STATE_DISCONNECTED when: @@ -94,6 +99,7 @@ public class RxBleRadioOperationDisconnectTest extends Specification { def "should call BluetoothGatt.disconnect() if BluetoothGatt is not disconnected at the time of running and then BluetoothGatt.close() when RxBleGattCallback.getOnConnectionStateChange() will emit RxBleConnection.RxBleConnectionState.DISCONNECTED and then release the radio"() { given: + testWithGattProviderReturning(mockBluetoothGatt) mockBluetoothManager.getConnectionState(mockDevice, GATT) >> initialState when: