How To Establish Connection And Then Read Characteristics In One Activity?
Currently modifying the sample code and I am encountering an error when try to establish a connection and then read a characteristic in one activity. I am getting BleAlreadyConnect
Solution 1:
The thing is that you are connecting twice. You should rather:
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device);
// How to listen for connection state changes
bleDevice.observeConnectionStateChanges()
.compose(bindUntilEvent(DESTROY))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onConnectionStateChange);
connectionObservable = bleDevice.establishConnection(this, true)
.compose(bindUntilEvent(PAUSE))
.takeUntil(disconnectTriggerSubject)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnUnsubscribe(this::clearSubscription)
.compose(new ConnectionSharingAdapter());
if (isConnected()) {
triggerDisconnect();
} else {
connectionObservable.subscribe(this::onConnectionReceived, this::onConnectionFailure);
}
}
private void onConnectionReceived(RxBleConnection connection) {
Log.d(TAG, "onConnectionReceived");
connection
.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(uuid))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bytes -> {
Log.d(TAG, "onValueReceived:" + bytes);
}, this::onReadFailure);
}
Full example is here.
Post a Comment for "How To Establish Connection And Then Read Characteristics In One Activity?"