Flutter Blue 从 2 个特征中读取值,使用 notify

问题描述

我是 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]。 但是我的 Dataparser 只会在 uuid ...b72(wt_Ist_stream) 的值改变时启动。然后他从 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 将正确的数据发送到数据解析器,但只发送在流中调用的流:

如何更改代码,让我的解析器从流更改开始并从 snapshot.data 获取正确的值?

数据解析器代码

_DataParser(List<int> dataFromDevice) {
    String InputString = utf8.decode(dataFromDevice);

    if (InputString.length>6) {.....}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)