简体   繁体   English

如何从字节数组中获取正确的温度值

[英]How to get the correct temperature value from byte array

I'm trying to create a BLE app where i want to catch the value of the temperature from a device.我正在尝试创建一个 BLE 应用程序,我想在其中捕获设备的温度值。 I'm using Temp Sitter device.我正在使用 Temp Sitter 设备。

I have this UUID=0000ffe1-0000-1000-8000-00805f9b34fb.我有这个 UUID=0000ffe1-0000-1000-8000-00805f9b34fb。 From here I get an array of bytes.从这里我得到一个字节数组。

// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();

if (data != null && data.length > 0) {
    final StringBuilder stringBuilder = new 
                                       StringBuilder(data.length);
    for(byte byteChar : data)
        stringBuilder.append(String.format("%02X ", byteChar));
    intent.putExtra(EXTRA_DATA, new String(data) + "\n" + 
    stringBuilder.toString());
}

Here are some hex results:以下是一些十六进制结果:

AA 06 11 00 3E 0D 00 62 --- 
AA 06 11 00 43 0D 00 67 --- 
AA 06 11 00 49 0D 00 6D 

Can any one help me how to read the exact value of this array?任何人都可以帮助我如何读取此数组的确切值吗?

After reverse-engineering the IRULU / Guangdong Biolight Meditech Temp Sitter app, it looks as if the message has this format:在对 IRULU / 广东百光医疗Temp Sitter应用程序进行逆向工程后,该消息看起来好像具有以下格式:

            0      1      2      3      4      5      6      7
        +------+------+------+------+------+------+------+------+
        |Marker|Length|Type  |Subtyp|Low   |High  |Unused|Chksum|
        +------+------+------+------+------+------+------+------+

Example    AA     06     11     00     3E     0D     00     62 

The fields are:这些字段是:

  • Marker : Always 0xAA.标记:始终为 0xAA。 Marker used to delinate message.用于描述消息的标记。 The message format seems to be designed to send a stream of messages.消息格式似乎旨在发送消息流。 0xAA would indicate the start of a new message. 0xAA 表示新消息的开始。
  • Length : The payload length, in bytes. Length :有效载荷长度,以字节为单位。 It's the length without marker and without checksum.这是没有标记和校验和的长度。
  • Type : The type of the message.类型:消息的类型。 0x11 seem to be temperature messages. 0x11 似乎是温度消息。 There's also a message tpe 0x12 that seems interesting (even though I don't understand its purpose).还有一条消息 tpe 0x12 看起来很有趣(即使我不明白它的目的)。 Other message types are ignored by the app.应用程序会忽略其他消息类型。
  • Subtype : The message subtype.子类型:消息子类型。 Subtype 0 are temperature measurements.子类型 0 是温度测量。 Subtype 1 and 2 seems to high and low warnings/indicators. 1型和2似乎是警告/指标。
  • Low : The low byte of the temperature value. Low : 温度值的低字节。
  • High : The high byte of the temperature value. High : 温度值的高字节。
  • Unused : This byte seems to be unsued and set to 0.未使用:此字节似乎未使用并设置为 0。
  • Checksum : The checksum of the payload.校验和:有效载荷的校验和。 It is simply computed by adding all bytes of the payload (starting with length and ending with the unused field).它是通过添加有效载荷的所有字节(以长度开始并以未使用的字段结束)来计算的。

The temperature value is stored in 0.01 degree (probably degree Celsius).温度值以 0.01 度(可能是摄氏度)存储。 So to extract it, you compute:所以要提取它,你计算:

double temperature = ((message[5] & 0xff) * 256 + (message[4] & 0xff)) * 0.01;

In the above example, the result would be 33.90 °C.在上面的示例中,结果将为 33.90 °C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM