Flutter如何解析厂商数据获取温度、湿度等
Flutter How to parse manufacturer data to get temperatures, humidity, etc
我正在使用 flutter_blue
插件来发现信标和传感器。就我而言,我使用的是 Teltonika 的眼睛传感器。我正在制作的应用程序发现了传感器,扫描后我得到了以下数据
制造商数据。
{89A: [01, B7, 0A, 01, 2C, 0D, FA, C7, 00, AC, 6B]}.
服务数据
{0000FEAA-0000-1000-8000-00805F9B34FB: [00, 02, 2E, 50, 80, 6A, A3, 82, 55, AA, D9, FA, 2A, 15, 4E, 2D, 00, 55, 00, 00]}
我想从这些数据中提取温度、湿度等,但我不知道该怎么做。我发现了类似的问题 ,但我不知道它如何适合我的情况。
可以找到传感器的文档here。 Protocol Description
部分关于数据的信息很少,但我不确定如何使用它。
我们将不胜感激任何帮助。谢谢
Protocol Description
似乎很好地解释了格式。
以问题Manufacturer data
中的十六进制数为例:
[01, // version
B7, // Flag value the same as documentation so it will be the same fields
0A, 01, // Temperature. Big endian so 2561 which needs dividing by 100 to get 25.61
2C, // Humidity. 2c hex is 44 decimal. So 44%
0D, FA, // Movement. Status is "not moving" and 3578 movement count
C7, // Angle. Device pitch (c7) = -57.
00, AC, // Angle. Device Roll (00AC) = 172
6B] // Battery value 3070mv
使用代码执行此操作应该与您链接到的问题相同,但数据采用 Endian.big
格式,这是默认设置。例如:
import 'dart:typed_data';
var manufacturerData = Uint8List.fromList([0x01, 0xB7, 0x0A, 0x01, 0x2C, 0x0D, 0xFA, 0xC7, 0x00, 0xAC, 0x6B]);
var flags = ByteData.sublistView(manufacturerData, 1).getUint8(0);
/*
0 – Temperature value presence
1 – Humidity value presence
2 – Magnetic sensor presence
3 – Magnetic sensor state (1 magnetic field is detected/0 magnetic field is not detected) Valid value is present only if bit 2 flag is set.
4 – Movement sensor counter
5 – Movement sensor angle
6 – Low Battery indication (if set to 1 low battery voltage detected)
7 – Battery voltage value presence
*/
var flagTemperature = flags >> 0 & 1;
var flagHumidity = flags >> 1 & 1;
var flagMagPresence = flags >> 2 & 1;
var flagMagState = flags >> 3 & 1;
var flagMoveCount = flags >> 4 & 1;
var flagMoveAngle = flags >> 5 & 1;
var flagLowBatt = flags >> 6 & 1;
var flagBattery = flags >> 7 & 1;
var temperature = ByteData.sublistView(manufacturerData, 2, 4);
var humidity = ByteData.sublistView(manufacturerData, 4, 5);
var movement = ByteData.sublistView(manufacturerData, 5, 7);
var movementStatus = movement.getUint16(0) >> 15 & 1;
var movementCount = movement.getUint16(0) & 0x7fff;
var devicePitch = ByteData.sublistView(manufacturerData, 7, 8);
var deviceRoll = ByteData.sublistView(manufacturerData, 8, 10);
var battery = ByteData.sublistView(manufacturerData, 10, 11);
main() {
print("Temperature Value presence: " + flagTemperature.toString());
print("Humidity Value presence: " + flagHumidity.toString());
print("Magnetic Value presence: " + flagMagPresence.toString());
print("Magnetic State: " + flagMagState.toString());
print("Move Sensor: " + flagMoveCount.toString());
print("Move Angle: " + flagMoveAngle.toString());
print("Low Battery: " + flagLowBatt.toString());
print("Battery Value Presence: " + flagBattery.toString());
print("Temperature: " + (temperature.getUint16(0) / 100).toString());
print("Humidity: " + humidity.getUint8(0).toString());
print("Movement Status: " + movementStatus.toString());
print("Movement count: " + movementCount.toString());
print("Device Pitch: " + devicePitch.getInt8(0).toString());
print("Device Roll: " + deviceRoll.getInt16(0).toString());
print("Battery: " + (2000 + (battery.getUint8(0) * 10)).toString());
}
打印到控制台:
Temperature Value presence: 1
Humidity Value presence: 1
Magnetic Value presence: 1
Magnetic State: 0
Move Sensor: 1
Move Angle: 1
Low Battery: 0
Battery Value Presence: 1
Temperature: 25.61
Humidity: 44
Movement Status: 0
Movement count: 3578
Device Pitch: -57
Device Roll: 172
Battery: 3070
我正在使用 flutter_blue
插件来发现信标和传感器。就我而言,我使用的是 Teltonika 的眼睛传感器。我正在制作的应用程序发现了传感器,扫描后我得到了以下数据
制造商数据。
{89A: [01, B7, 0A, 01, 2C, 0D, FA, C7, 00, AC, 6B]}.
服务数据
{0000FEAA-0000-1000-8000-00805F9B34FB: [00, 02, 2E, 50, 80, 6A, A3, 82, 55, AA, D9, FA, 2A, 15, 4E, 2D, 00, 55, 00, 00]}
我想从这些数据中提取温度、湿度等,但我不知道该怎么做。我发现了类似的问题
可以找到传感器的文档here。 Protocol Description
部分关于数据的信息很少,但我不确定如何使用它。
我们将不胜感激任何帮助。谢谢
Protocol Description
似乎很好地解释了格式。
以问题Manufacturer data
中的十六进制数为例:
[01, // version
B7, // Flag value the same as documentation so it will be the same fields
0A, 01, // Temperature. Big endian so 2561 which needs dividing by 100 to get 25.61
2C, // Humidity. 2c hex is 44 decimal. So 44%
0D, FA, // Movement. Status is "not moving" and 3578 movement count
C7, // Angle. Device pitch (c7) = -57.
00, AC, // Angle. Device Roll (00AC) = 172
6B] // Battery value 3070mv
使用代码执行此操作应该与您链接到的问题相同,但数据采用 Endian.big
格式,这是默认设置。例如:
import 'dart:typed_data';
var manufacturerData = Uint8List.fromList([0x01, 0xB7, 0x0A, 0x01, 0x2C, 0x0D, 0xFA, 0xC7, 0x00, 0xAC, 0x6B]);
var flags = ByteData.sublistView(manufacturerData, 1).getUint8(0);
/*
0 – Temperature value presence
1 – Humidity value presence
2 – Magnetic sensor presence
3 – Magnetic sensor state (1 magnetic field is detected/0 magnetic field is not detected) Valid value is present only if bit 2 flag is set.
4 – Movement sensor counter
5 – Movement sensor angle
6 – Low Battery indication (if set to 1 low battery voltage detected)
7 – Battery voltage value presence
*/
var flagTemperature = flags >> 0 & 1;
var flagHumidity = flags >> 1 & 1;
var flagMagPresence = flags >> 2 & 1;
var flagMagState = flags >> 3 & 1;
var flagMoveCount = flags >> 4 & 1;
var flagMoveAngle = flags >> 5 & 1;
var flagLowBatt = flags >> 6 & 1;
var flagBattery = flags >> 7 & 1;
var temperature = ByteData.sublistView(manufacturerData, 2, 4);
var humidity = ByteData.sublistView(manufacturerData, 4, 5);
var movement = ByteData.sublistView(manufacturerData, 5, 7);
var movementStatus = movement.getUint16(0) >> 15 & 1;
var movementCount = movement.getUint16(0) & 0x7fff;
var devicePitch = ByteData.sublistView(manufacturerData, 7, 8);
var deviceRoll = ByteData.sublistView(manufacturerData, 8, 10);
var battery = ByteData.sublistView(manufacturerData, 10, 11);
main() {
print("Temperature Value presence: " + flagTemperature.toString());
print("Humidity Value presence: " + flagHumidity.toString());
print("Magnetic Value presence: " + flagMagPresence.toString());
print("Magnetic State: " + flagMagState.toString());
print("Move Sensor: " + flagMoveCount.toString());
print("Move Angle: " + flagMoveAngle.toString());
print("Low Battery: " + flagLowBatt.toString());
print("Battery Value Presence: " + flagBattery.toString());
print("Temperature: " + (temperature.getUint16(0) / 100).toString());
print("Humidity: " + humidity.getUint8(0).toString());
print("Movement Status: " + movementStatus.toString());
print("Movement count: " + movementCount.toString());
print("Device Pitch: " + devicePitch.getInt8(0).toString());
print("Device Roll: " + deviceRoll.getInt16(0).toString());
print("Battery: " + (2000 + (battery.getUint8(0) * 10)).toString());
}
打印到控制台:
Temperature Value presence: 1
Humidity Value presence: 1
Magnetic Value presence: 1
Magnetic State: 0
Move Sensor: 1
Move Angle: 1
Low Battery: 0
Battery Value Presence: 1
Temperature: 25.61
Humidity: 44
Movement Status: 0
Movement count: 3578
Device Pitch: -57
Device Roll: 172
Battery: 3070