向特征写入多个命令 [英] Writing multiple commands to characteristic

查看:87
本文介绍了向特征写入多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是发现rxandroidble并且可以在连接后可靠地将单个命令发送到BLE设备

I am just discovering rxandroidble and can reliably send a single command to the BLE device after connection

但是,我一直在努力寻找编写命令链的最佳方法,例如,如果我有一系列需要发送的3条命令

However I am struggling to find the best way to write a chain of commands, ie if I have a series of 3 commands that need to be sent

当然可以通过嵌套发送来完成,但是我确定有更好的方法!

Of course this can be done by nesting the sends, but Im sure there is a better approach!!

单个命令的发送代码为

rxBleMainConection.writeCharacteristic(COMS_WRITE_CHAR_UUID,bytes).toObservable()
.subscribe(
                    characteristicValue -> {
                        // Written characteristic value.
                        Log.d(TAG,"Written command: " + Arrays.toString(characteristicValue));

                    },
                    throwable -> {
                        // Handle an error here.
                        Log.d(TAG,"Error writing command");
                        throwable.printStackTrace();
                    }
            );

发送一系列说5条命令的最佳方法是什么?

What is the best way to send a series of say 5 commands?

推荐答案

您可以像这样将所有要写入的内容连接起来:

You could concatenate all the writes you want to make like this:

Single.concat(Arrays.asList(
        rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytes0),
        rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytes1),
        rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytes2),
        rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytes3),
        // ...
        rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytesn)
))
        .subscribe(
                characteristicValue -> {
                    // Written characteristic value.
                    Log.d(TAG, "Written command: " + Arrays.toString(characteristicValue));
                },
                throwable -> {
                    // Handle an error here.
                    Log.d(TAG, "Error writing command");
                    throwable.printStackTrace();
                },
                () -> {
                    Log.d(TAG, "All writes completed");
                }
        );

我鼓励您看一下其他有关RxAndroidBle的多次写入"的问题在该网站上已经被问到了.有一些帖子可以给您提示/想法.

I would encourage you to take a look on other questions regarding "multiple writes" with RxAndroidBle that were already asked on this site. There are some posts that could give you hints/ideas.

作为旁注:最好创建仅使用单个.subscribe()的代码,因为这样您便拥有最少的需要自己管理的状态.

As a side note: it is best to create code that uses only a single .subscribe() as then you have the least state you need to manage by yourself.

这篇关于向特征写入多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆