android - Flutter 与 ESP32 蓝牙串口流通信
问题描述
几天来,我一直在尝试使用 flutter_bluetooth_serial 读取和存储来自 ESP32 的流数据。我必须使用以下方式读取以串行方式写入的那些数据:
void blue(){
//SerialBT.print(",");
SerialBT.print(String(Latitude,6));
SerialBT.print(",");
SerialBT.print(String(Longitude,6));
SerialBT.print(",");
SerialBT.print(s_temperature);
SerialBT.print(",");
SerialBT.print(bar_temperature);
SerialBT.print(",");
SerialBT.print(bar_pressure);
SerialBT.print(",");
SerialBT.print(bar_altitude);
SerialBT.print(",");
SerialBT.print(bar_humidity);
SerialBT.print(",");
SerialBT.print(accX);
SerialBT.print(",");
SerialBT.print(accY);
SerialBT.print(",");
SerialBT.print(accZ);
SerialBT.print(",");
SerialBT.print(gyroX);
SerialBT.print(",");
SerialBT.print(gyroY);
SerialBT.print(",");
SerialBT.print(gyroZ);
SerialBT.print(",");
/*
SerialBT.print(accAngleX);
SerialBT.print(",");
SerialBT.print(accAngleY);
SerialBT.print(",");
SerialBT.print(gyroAngleX);
SerialBT.print(" ");
SerialBT.print(gyroAngleY);
SerialBT.print(" ");
SerialBT.print(gyroAngleZ);
SerialBT.print(" ");
*/
SerialBT.print(int(angleY)); //roll (or pitch) depends by axis direction
SerialBT.print(",");
SerialBT.print(int(angleX)); //pitch (or roll)
SerialBT.print(",");
SerialBT.print(int(angleZ)); //yaw
SerialBT.print("\n");
if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
else{
message = "";
}
Serial.write(incomingChar);
Serial.print(message);
}
//Serial.println(message);
// Check received message and control output accordingly
if (message =="1"){
digitalWrite(green, HIGH);
Serial.println("entra nell if log_on");
}
else if (message =="0"){
digitalWrite(green, LOW);
Serial.println("entra nell if log_off");
}
delay(20);
}
这是用于启动读取流的日志的代码:
void _logOn() async {
connection.output.add(utf8.encode("1"+ "\n"));
await connection.output.allSent;
connection.input.listen((Uint8List data) {
_temp = ascii.decode(data);
});
setState(() {
_deviceState = 1; // device on
_logState = 1;
_prova = _temp;
});
}
我知道在 _prova 上存储数据是一种不好的方法,因为我想存储数据直到 _logstate 等于 1。如何创建一个线程,在后台也连续存储数据,最后将数据保存在 txt 文件中??
解决方案
推荐阅读
- javascript - JS 对象默认单引号行为
- sql - 如何将一行中的所有值连接成一个字符串?
- ios - iOS 项目中的 import <> vs import "" 有什么区别
- node.js - 尝试安装 npm 后出现错误
- javascript - 去抖动和返回
- ecmascript-6 - 如何在 Eclipse swt 应用程序中使用 Windows Edge 浏览器而不是 IE?
- php - 如何在 laravel 中撤销多重身份验证中的令牌?
- spring-boot - keycloak授权服务器如何使用spring rest适配器验证授权规则
- android - 如何返回异步等待承诺
- android - sdk 的 targetSdkVersion 更改为 26