问题描述
当前正在修改示例代码,然后我尝试建立连接,然后在一个活动中读取特征时遇到错误.
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.
尝试从设备读取时,我得到了BleAlreadyConnectedException
.我首先连接onCreate.
I am getting BleAlreadyConnectedException
when attempting to read from the device. I am first connecting on 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中的特征...
and then trying to read a characteristic in 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解决double.EstablishmentConnection(this,false)?
How do you work around the double .establishConnection(this, false) with ConnectionSharingAdapter?
推荐答案
问题是您连接了两次.您应该宁愿:
The thing is that you are connecting twice. You should rather:
@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);
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);
}
完整示例为此处.
这篇关于如何建立联系,然后在一项活动中阅读特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!