Android BLE : Weight data from the BLE Scale – Need to convert byte array to decimal weight value in Kotlin

huangapple go评论67阅读模式
英文:

Android BLE : Weight data from the BLE Scale - Need to convert byte array to decimal weight value in Kotlin

问题

我已将我的应用程序与BLE体重秤设备连接,并能够从设备获取字节数组。但我无法将字节数组转换为我的体重值。体重值位于字节数组的第6个位置。我有一些测试案例如下。

[-6, 5, 0, 3, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] =========32克

[-6, 5, 0, 3, 0, 1, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ========364克

[-6, 5, 0, 3, 0, 0, -17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ========239克

[-6, 5, 0, 3, 0, 1, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] =======364克

[-6, 5, 0, 3, 0, 1, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ========325克

[-6, 5, 0, 3, 0, 1, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]==========333克

[-6, 5, 0, 3, 0, 2, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] =========603克

[-6, 5, 0, 3, 0, 4, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] =========1040克

看起来第5和第6个值是相关的。请帮助我将其转换为体重值。

英文:

I connected my app with a BLE weight scale device and I'm able to get the byte array from the device.but I couldn't able to convert the byte array to my weight.
the weight value is coming in the 6th position of the byte array. I have some test cases as follows.

[-6, 5, 0, 3, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] =========32gm

[-6, 5, 0, 3, 0, 1, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ========364gm

[-6, 5, 0, 3, 0, 0, -17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ========239gm

[-6, 5, 0, 3, 0, 1, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] =======364gm

[-6, 5, 0, 3, 0, 1, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ========325gm

[-6, 5, 0, 3, 0, 1, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]==========333gm

[-6, 5, 0, 3, 0, 2, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] =========603gm

[-6, 5, 0, 3, 0, 4, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] =========1040gm

It seems like 5th and 6th values are dependent.Please help me on converting this to weight value.

答案1

得分: 1

你想要接收的数据是存储在位置5和6的两个字节中的Short值。在Kotlin JVM中将字节解码为原始类型的最简单方法是将数据的这部分包装到ByteBuffer中,然后解码它。

从你的一行代码中

[-6, 5, 0, 3, 0, 1, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ========364gm
val bleByteArray = byteArrayOf(-6, 5, 0, 3, 0, 1, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
val buffer = ByteBuffer.wrap(bleByteArray, 5, Short.SIZE_BYTES)
assert(buffer.getShort() == 364)

在这里,buffer 只包含字节5和6。因此,在调用 getShort 时,缓冲区会读取前两个字节并返回相应的short值。

如果您想读取BLE信息的其他部分,您可以使用ByteBuffer的其他方法,将整个ByteArray包装起来,然后读取其中的多个部分。

英文:

The data you want to receive is a Short value stored in two bytes at position 5 and 6. The easiest way to decode bytes to primitive in kotlin jvm is to wrap this part of the data into a ByteBuffer and decode it.

From one of your line

> [-6, 5, 0, 3, 0, 1, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ========364gm

val bleByteArray = byteArrayOf(-6, 5, 0, 3, 0, 1, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
val buffer = ByteBuffer.wrap(bleByteArray, 5, Short.SIZE_BYTES)
assert(buffer.getShort() == 364)

Here, buffer only contains bytes 5 and 6. So when getShort is called, the buffer read the first two bytes and return the corresponding short value.

If you want to read other parts of the ble information, you may want to wrap() the whole ByteArray and then read multiple parts of it with other methods of ByteBuffer.

答案2

得分: 1

假设传入的数据被定义为byte[32]类型,并保存在一个名为ble的数组中:

int weight = (ble[5] << 8) | ble[6];

请记住,Java数组从索引0开始,所以第一个元素是ble[0],而不是ble1。这就是为什么上面的代码使用了数组元素5和6 - 它们实际上是数组的第6和第7个元素。

英文:

Assuming the incoming data is typed as byte[32] and saved into an array named ble:

int weight = (ble[5] << 8) | ble[6];

Remember that Java arrays start with index 0, so the first element is ble[0], not ble1. That's why the above code uses array elements 5 and 6 - they are actually the 6th and 7th elements of the array.

huangapple
  • 本文由 发表于 2023年7月20日 15:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定