我是 Flutter 的初学者,想用蓝牙 LE 编写一个应用程序。我使用flutter_blue。如果我只想读取一个特征的值,我的应用程序就可以工作。现在我需要阅读第二个特征,这就是问题所在。我正在使用来自 esp32 灰尘传感器的示例。主要类似于flutter blue的例子。到下一个站点的步骤是使用以下代码:
StreamBuilder<List<ScanResult>>(
stream: FlutterBlue.instance.scanResults,
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data
.map(
(r) => ScanResultTile(
result: r,
onTap: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) {
r.device.connect();
return SensorPage(device: r.device);
})),
),
这行得通。在 Sensor-Page 中,有以以下开头的页面代码:
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
title: Text(' Connect'),
),
body: Container(
// color: Colors.purple,
decoration: BoxDecoration(
image: DecorationImage(
// image: AssetImage("lib/Images/knaus.jpg"),
image: AssetImage("lib/Images/innen.jpg"),
fit: BoxFit.cover,
),
),
child: !isReady
? Center(
child: Text(
"Warte auf Verbindung...",
style: TextStyle(fontSize: 24, color: Colors.red),
),
)
: Container(
child: StreamBuilder<List<int>>(
stream: wt_Ist_stream,
builder: (BuildContext context,
AsyncSnapshot<List<int>> snapshot) {
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
if (snapshot.connectionState ==
ConnectionState.active) {
_DataParser(snapshot.data);
我产生的特征:
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_WT_RcvWater) {
characteristic.setNotifyValue(!characteristic.isNotifying);
characteristic1=characteristic;
// characteristic1.setNotifyValue(!characteristic.isNotifying);
wt_Ist_stream = characteristic1.value;
// wt_Soll_stream = service.characteristic.value;
setState(() {
isReady = true;
});
}
if (characteristic.uuid.toString() == CHARACTERISTIC_UUID_WT_Soll) {
characteristic.setNotifyValue(!characteristic.isNotifying);
characteristic2=characteristic;
// characteristic2.setNotifyValue(!characteristic2.isNotifying);
wt_Soll_stream = characteristic2.value;
// characteristic2.value.listen((InputString2)
// wt_Soll_stream = service.characteristic.value;
}
//Updating characteristic to perform write operation.
if (characteristic.uuid.toString() == CHARACTERISTIC_UUID_WT_TxWater) {
characteristic_Write=characteristic;
}
});
}
});
if (!isReady) {
_Pop();
}
}
两个阅读特征都向应用程序发送数据,我收到通知,新数据可用 [onCharacteristicChanged] uuid:456e869c-d393-4cec-9f43-cef5382eab72]。但是只有当 uuid ...b72(wt_Ist_stream) 的值发生变化时,我的 Dataparser 才会启动。然后他从 snapshot.data 中得到正确的字符串。
如果我更改了 Streambuilder-Stream
child: StreamBuilder<List<int>>(
stream: wt_Ist_stream,
至
child: StreamBuilder<List<int>>(
stream: wt_Soll_stream,
我的数据解析器从特征 2 中获取值。但是,如果一个流(wt_Ist_Stream 或 wt_Soll_stream)发生更改然后获取正确的值,我该如何更改我的解析器自动启动的应用程序。
Streambuilder 将正确的数据发送到 dataparser,但只发送流,它在流中被调用:
如何更改代码,让我的解析器从流更改开始并从 snapshot.data 获取正确的值?
数据解析器代码:
_DataParser(List<int> dataFromDevice) {
String InputString = utf8.decode(dataFromDevice);
if (InputString.length>6) {.....}