目前正在修改示例代码,我在尝试建立连接然后在一个活动中读取特征时遇到错误。
我BleAlreadyConnectedException
在尝试从设备读取时得到。我首先连接 onCreate。
@Override
protected 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);
if (isConnected()) {
triggerDisconnect();
} else {
connectionSubscription = bleDevice.establishConnection(this, true)
.compose(bindUntilEvent(PAUSE))
.observeOn(AndroidSchedulers.mainThread())
.doOnUnsubscribe(this::clearSubscription)
.subscribe(this::onConnectionReceived, this::onConnectionFailure);
}
}
然后尝试读取 onConnectionReceived 中的特征...
private void onConnectionReceived(RxBleConnection connection) {
Log.d(TAG, "onConnectionReceived");
connectionObservable = bleDevice
.establishConnection(this, false)
.takeUntil(disconnectTriggerSubject)
.compose(bindUntilEvent(PAUSE))
.doOnUnsubscribe(this::clearSubscription)
.compose(new ConnectionSharingAdapter());
if (isConnected()) {
connectionObservable
.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(uuid))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bytes -> {
Log.d(TAG, "onConnectionReceived:" + bytes);
}, this::onReadFailure);
}
}
您如何使用 ConnectionSharingAdapter 解决双重 .establishConnection(this, false)?