为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 java 中解析 BluetoothGattCharacteristic 值

如何在 java 中解析 BluetoothGattCharacteristic 值

FFIVE 2022-11-30 16:45:48
我目前在尝试弄清楚如何将数据从我的 ESP32 微控制器传输到 Android 手机时有点迷茫。我已经设法发送和读取特征值,但不知道如何解析它。现在,我发送简单的整数值 = 15。我发现数据是使用字节数组发送的,因此我将其转换为十六进制字符串,结果没有意义31-35-2E-30-30。检查了nRF connect应用程序,它也显示相同的十六进制字符串结果,但另外它已将值解析为15.00.这是 Arduino 代码:...char txString[8];int someNumber = 15;dtostrf(someNumber, 1, 2, txString);_pCharacteristicNotify -> setValue(txString);_pCharacteristicNotify -> notify();...安卓工作室public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {    _handler.handleMessage(Message.obtain(null, ..., characteristic));}private Handler _handler = new Handler() {    public void handleMessage(Message msg) {        // ------- The problem is here ----------------        BluetoothGattCharacteristic characteristic;        characteristic = (BluetoothGattCharacteristic) msg.obj;        String value = Utils.parseBLECharacteristicValue(characteristic);        // the value is "31-35-2E-30-30"        // HOW TO GET THE NUMBER 15 ??    }};我正在使用的解析方法来自这里示例 1有人可以解释一下如何解析给定值吗?如果提供的值是字符串"abcd123"而不是整数,逻辑将如何改变?
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

31-35-2E-30-30是字符串的 ASCII 表示15.00

根据dtostrf() 文档

转换以格式“[-]d.ddd”完成。

这解释了字符串 15.00 由于调用的第三个参数是两个十进制值2

尝试使用 Arduino 的String 构造函数进行整数到字符串的转换。之后,您可以使用 Android 中的 Integer 类将字符串转换回整数,如本答案中所述。


查看完整回答
反对 回复 2022-11-30
  • 1 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信