Skip to content

Commit

Permalink
Fixed a null RxBleConnection being returned when connecting. (#275) (#…
Browse files Browse the repository at this point in the history
…278)

`RxBleConnection` can be instantiated only after `BluetoothGatt` is available. Changed the Connector code from `Observable.just(connectionComponent.rxBleConnection()` to `Observable.fromCallable(connectionComponent::rxBleConnection)` equivalent.
  • Loading branch information
dariuszseweryn committed Sep 12, 2017
1 parent ed1fdf5 commit aa4cf34
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.polidea.rxandroidble.internal.operations.RxBleRadioOperationConnect;
import com.polidea.rxandroidble.internal.operations.RxBleRadioOperationDisconnect;

import java.util.concurrent.Callable;
import javax.inject.Inject;

import rx.Observable;
Expand Down Expand Up @@ -35,16 +36,23 @@ public Observable<RxBleConnection> prepareConnection(final boolean autoConnect)
public Observable<RxBleConnection> call() {

final ConnectionComponent connectionComponent = connectionComponentBuilder.build();
RxBleRadioOperationConnect operationConnect = connectionComponent.connectOperationBuilder()
final RxBleRadioOperationConnect operationConnect = connectionComponent.connectOperationBuilder()
.setAutoConnect(autoConnect)
.build();

final RxBleConnection connection = connectionComponent.rxBleConnection();
final Observable<RxBleConnection> newConnectionObservable = Observable.fromCallable(new Callable<RxBleConnection>() {
@Override
public RxBleConnection call() throws Exception {
// BluetoothGatt is needed for RxBleConnection
// BluetoothGatt is produced by RxBleRadioOperationConnect
return connectionComponent.rxBleConnection();
}
});
final Observable<BluetoothGatt> connectedObservable = rxBleRadio.queue(operationConnect);
final Observable<RxBleConnection> disconnectedErrorObservable = connectionComponent.gattCallback().observeDisconnect();
final Action0 disconnect = queueIgnoringResult(connectionComponent.disconnectOperation());

return Observable.just(connection)
return newConnectionObservable
.delaySubscription(connectedObservable)
.mergeWith(disconnectedErrorObservable)
.doOnUnsubscribe(disconnect);
Expand Down

0 comments on commit aa4cf34

Please sign in to comment.