[英]How can I write to a BLE GATT characteristic using RxAndroidBle in Kotlin?
[英]How to Read BLE GATT characterstics in chunks form GATT server using RxAndroidBLE
我正在使用RxAndroidBle库来处理BLE连接以及从我的android gatt客户端应用读取/写入GATT服务器。 我遵循了github提供的示例应用程序。
我面临的问题是我的GATT服务器在Intel Edison上运行,并且仅支持80的MTU大小。它以块的形式发送数据,我应该多次读取特征值,直到遇到特殊字符,例如`` /结束' 。 我尝试了“自定义读取”操作示例,该示例应该每250毫秒读取5次。
private static class CustomReadOperation implements RxBleRadioOperationCustom<byte[]> {
private RxBleConnection connection;
private UUID characteristicUuid;
CustomReadOperation(RxBleConnection connection, UUID characteristicUuid) {
this.connection = connection;
this.characteristicUuid = characteristicUuid;
}
/**
* Reads a characteristic 5 times with a 250ms delay between each. This is easily achieve without
* a custom operation. The gain here is that only one operation goes into the RxBleRadio queue
* eliminating the overhead of going on & out of the operation queue.
*/
@NonNull
@Override
public Observable<byte[]> asObservable(BluetoothGatt bluetoothGatt,
RxBleGattCallback rxBleGattCallback,
Scheduler scheduler) throws Throwable {
return connection.getCharacteristic(characteristicUuid)
.flatMap(characteristic -> readAndObserve(characteristic, bluetoothGatt, rxBleGattCallback))
.subscribeOn(scheduler)
.takeFirst(readResponseForMatchingCharacteristic())
.map(byteAssociation -> byteAssociation.second)
.repeatWhen(notificationHandler -> notificationHandler.take(5).delay(250, TimeUnit.MILLISECONDS));
}
@NonNull
private Observable<ByteAssociation<UUID>> readAndObserve(BluetoothGattCharacteristic characteristic,
BluetoothGatt bluetoothGatt,
RxBleGattCallback rxBleGattCallback) {
Observable<ByteAssociation<UUID>> onCharacteristicRead = rxBleGattCallback.getOnCharacteristicRead();
return Observable.create(emitter -> {
Subscription subscription = onCharacteristicRead.subscribe(emitter);
emitter.setCancellation(subscription::unsubscribe);
try {
final boolean success = bluetoothGatt.readCharacteristic(characteristic);
if (!success) {
throw new BleGattCannotStartException(bluetoothGatt, BleGattOperationType.CHARACTERISTIC_READ);
}
} catch (Throwable throwable) {
emitter.onError(throwable);
}
}, Emitter.BackpressureMode.BUFFER);
}
private Func1<ByteAssociation<UUID>, Boolean> readResponseForMatchingCharacteristic() {
return uuidByteAssociation -> uuidByteAssociation.first.equals(characteristicUuid);
}
}
我这样称呼它
public void customRead()
{
if (isConnected()) {
connectionObservable
.flatMap(rxBleConnection -> rxBleConnection.queue(new CustomReadOperation(rxBleConnection, UUID_READ_CHARACTERISTIC)))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bytes -> {
configureMvpView.showList(bytes);
}, this::onRunCustomFailure);
}
}
而且我没有使用此代码从服务器获取任何数据。 但是,如果我尝试这样的简单读取操作
public void readInfo() {
if (isConnected()) {
connectionObservable
.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(UUID_READ_CHARACTERISTIC))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bytes -> {
// parse data
configureMvpView.showWifiList(bytes);
}, this::onReadFailure);
}
}
我得到了第一批数据,但是我需要读取其余数据。
我对RxJava不太了解。 因此,可能有一种简单的方法来执行此操作,但是任何建议或帮助都会很好。
这是我的prepareConnectionObservable
private Observable<RxBleConnection> prepareConnectionObservable() {
return bleDevice
.establishConnection(false)
.takeUntil(disconnectTriggerSubject)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnUnsubscribe(this::clearSubscription)
.compose(this.bindToLifecycle())
.compose(new ConnectionSharingAdapter());
}
我打电话
connectionObservable.subscribe(this::onConnectionReceived, this::onConnectionFailure);
和onConnectionReceived我叫CustomRead。
您没有显示如何创建connectionObservable
,并且我不知道在执行上述代码之前对该连接是否进行了其他操作。
我的猜测是,如果您查看应用程序的日志,您会看到无线电处理队列在连接后的第一个操作中开始执行CustomReadOperation
。 在自定义操作中,您正在调用RxBleConnection.getCharacteristic(UUID)
,该尝试尝试执行.discoverServices()
(在无线电队列上安排RxBleRadioOperationDiscoverServices
)。 问题在于,无线电队列已经在执行CustomReadOperation
并且在完成之前不会发现服务。
还有就是为什么一个原因RxBleConnection
没有传递到RxBleRadioOperationCustom.asObservable()
-大部分的功能将不会在那个时候工作。
您可以做的是在安排CustomReadOperation
之前执行RxBleConnection.discoverServices()
并在构造函数中传递从RxBleDeviceServices
检索到的BluetoothGattCharacteristic
。 所以不要这样:
public void customRead()
{
if (isConnected()) {
connectionObservable
.flatMap(rxBleConnection -> rxBleConnection.queue(new CustomReadOperation(rxBleConnection, UUID_READ_CHARACTERISTIC)))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bytes -> {
configureMvpView.showList(bytes);
}, this::onRunCustomFailure);
}
}
您将有类似以下内容:
public void customRead()
{
if (isConnected()) {
connectionObservable
.flatMap(RxBleConnection::discoverServices, (rxBleConnection, services) ->
services.getCharacteristic(UUID_READ_CHARACTERISTIC)
.flatMap(characteristic -> rxBleConnection.queue(new CustomReadOperation(characteristic)))
)
.flatMap(observable -> observable)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bytes -> {
configureMvpView.showList(bytes);
}, this::onRunCustomFailure);
}
}
编辑(说明):
而且CustomReadOperation
的构造函数应如下所示:
CustomReadOperation(BluetoothGattCharacteristic characteristic) {
this.characteristic = characteristic;
}
因此,您不必在CustomReadOperation
内部使用this.rxBleConnection.getCharacteristic(UUID)
并直接使用bluetoothGatt.readCharacteristic(this.characteristic)
。
编辑2:更改这两行:
return connection.getCharacteristic(characteristicUuid)
.flatMap(characteristic -> readAndObserve(characteristic, bluetoothGatt, rxBleGattCallback))
对此(其余部分相同):
return readAndObserve(this.characteristic, bluetoothGatt, rxBleGattCallback)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.