Flutter 在flatter中将字节列表转换为整数值
我在从ESP32即BLE设备获取的值中删除括号时遇到问题。我想在心率小部件中显示不带括号的值,但我很难解决这个问题。请帮我找到解决办法。来自设备的数据将转换为字符串格式 可编辑代码:Flutter 在flatter中将字节列表转换为整数值,flutter,parsing,dart,bluetooth-lowenergy,esp32,Flutter,Parsing,Dart,Bluetooth Lowenergy,Esp32,我在从ESP32即BLE设备获取的值中删除括号时遇到问题。我想在心率小部件中显示不带括号的值,但我很难解决这个问题。请帮我找到解决办法。来自设备的数据将转换为字符串格式 可编辑代码: class HomeScreen extends StatefulWidget { const HomeScreen({Key key, this.device}) : super(key: key); final BluetoothDevice device; @override _HomeScr
class HomeScreen extends StatefulWidget {
const HomeScreen({Key key, this.device}) : super(key: key);
final BluetoothDevice device;
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
// BLE
final String SERVICE_UUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
final String CHARACTERISTIC_UUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
bool isReady;
Stream<List<int>> stream;
List<int> lastValue;
List<double> traceDust = List();
connectToDevice() async {
// await widget.device.connect();
print("connected");
discoverServices();
}
discoverServices() async {
List<BluetoothService> services = await widget.device.discoverServices();
services.forEach((service) {
if (service.uuid.toString() == SERVICE_UUID) {
service.characteristics.forEach((characteristic) {
if (characteristic.uuid.toString() == CHARACTERISTIC_UUID) {
characteristic.setNotifyValue(!characteristic.isNotifying);
stream = characteristic.value;
print(stream);
lastValue = characteristic.lastValue;
print(lastValue);
setState(() {
isReady = true;
});
}
});
}
});
}
类主屏幕扩展StatefulWidget{
const主屏幕({Key,this.device}):超级(Key:Key);
最终蓝牙设备;
@凌驾
_HomeScreenState createState()=>\u HomeScreenState();
}
类_homescrenstate扩展状态{
//BLE
最终管柱服务_UUID=“4fafc201-1fb5-459e-8fcc-c5c9c331914b”;
最终串特征_UUID=“beb5483e-36e1-4688-b7f5-ea07361b26a8”;
布尔已经准备好了;
溪流;
列出最后一个值;
List traceDust=List();
connectToDevice()异步{
//等待widget.device.connect();
打印(“连接”);
discoverServices();
}
discoverServices()异步{
List services=wait widget.device.discoverServices();
services.forEach((服务){
if(service.uuid.toString()==service\u uuid){
服务.特征.forEach((特征){
if(characteristic.uuid.toString()==characteristic\u uuid){
characteristic.setNotifyValue(!characteristic.isNotifying);
流=特征值;
打印(流);
lastValue=特征值。lastValue;
打印(最后值);
设置状态(){
isReady=真;
});
}
});
}
});
}
心率小部件代码:
Container(
padding: EdgeInsets.symmetric(vertical: 20),
alignment: Alignment.center,
//width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
StreamBuilder<List<int>>(
stream: stream,
initialData: lastValue,
builder: (BuildContext context,
AsyncSnapshot<List<int>> snapshot) {
if (snapshot.hasError)
return Text('Error: ${snapshot.error}',
style: TextStyle(
fontFamily: 'SF Pro Display',
fontSize: 19,
color: const Color(0xffffffff),
fontWeight: FontWeight.w500,
height: 1.4736842105263157,
),
);
if (snapshot.connectionState ==
ConnectionState.active) {
var currentValue = snapshot.data.toString();
//traceDust.add(double.tryParse(currentValue) ?? 0);
return Text('$currentValue',
style: TextStyle(
fontFamily: 'SF Pro Display',
fontSize: 19,
color: const Color(0xffffffff),
fontWeight: FontWeight.w500,
height: 1.4736842105263157,
),
);
} else {
return Text('Check the stream',
style: TextStyle(
fontFamily: 'SF Pro Display',
fontSize: 19,
color: const Color(0xffffffff),
fontWeight: FontWeight.w500,
height: 1.4736842105263157,
),
);
}
},
),
SizedBox(
height: 5,
),
Text(
'Heart Rate',
style: TextStyle(
fontFamily: 'SF Pro Display',
fontSize: 19,
color: Colors.white.withOpacity(0.7),
fontWeight: FontWeight.w500,
height: 1.2777777777777777,
),
textHeightBehavior: TextHeightBehavior(
applyHeightToFirstAscent: false),
textAlign: TextAlign.left,
),
],
),
)
容器(
填充:边缘组。对称(垂直:20),
对齐:对齐.center,
//宽度:double.infinity,
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
StreamBuilder(
小溪:小溪,
initialData:lastValue,
生成器:(BuildContext上下文,
异步快照(快照){
if(snapshot.hasError)
返回文本('错误:${snapshot.Error}',
样式:TextStyle(
fontFamily:“SF Pro显示器”,
尺码:19,
颜色:常量颜色(0xFFFFFF),
fontWeight:fontWeight.w500,
身高:1.4736842105263157,
),
);
如果(snapshot.connectionState==
连接状态(活动){
var currentValue=snapshot.data.toString();
//traceDust.add(double.tryParse(当前值)??0);
返回文本(“$currentValue”,
样式:TextStyle(
fontFamily:“SF Pro显示器”,
尺码:19,
颜色:常量颜色(0xFFFFFF),
fontWeight:fontWeight.w500,
身高:1.4736842105263157,
),
);
}否则{
返回文本('检查流',
样式:TextStyle(
fontFamily:“SF Pro显示器”,
尺码:19,
颜色:常量颜色(0xFFFFFF),
fontWeight:fontWeight.w500,
身高:1.4736842105263157,
),
);
}
},
),
大小盒子(
身高:5,,
),
正文(
“心率”,
样式:TextStyle(
fontFamily:“SF Pro显示器”,
尺码:19,
颜色:颜色。白色。不透明度(0.7),
fontWeight:fontWeight.w500,
身高:1.277777,
),
textHeightBehavior:textHeightBehavior(
ApplyHeightToFirstUpgrand:false),
textAlign:textAlign.left,
),
],
),
)
我得到的值如下(如上图心率所示):
如果这确实是您正在接收的数据格式,只是想以不同的格式对其进行格式化,请尝试一些字符串操作,例如:
final currentValue = '[33]';
print(currentValue.substring(1, currentValue.length-1)); // 33
你没有说你的问题期望值是多少。我假设它是
33
,尽管这对于心率读数来说似乎很低
我希望数据是lis