4

I'm a beginner in flutter and want to program a app with Bluetooth LE. I use flutter_blue. My App works if i only want to read the value of one characteristic. Now i need to read a second characteristic and that is the problem. I'm using a example from the esp32 dust sensor. The main is similar to the flutter blue example. The step to the next site is work with the following code:

             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);
                  })),
                ),

That works ok. At the Sensor-Page there is the code for the Page which starts with:

@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);

The characteristics i generate with:

  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();
    }
  }

Both reading-Characteristics are send Data to the app and i get the notification, that new data available [onCharacteristicChanged] uuid: 456e869c-d393-4cec-9f43-cef5382eab72]. but my Dataparser will only start if the value for uuid ...b72(wt_Ist_stream) changed. Then he gets the right string from snapshot.data.

If i changed the Streambuilder-Stream

  child: StreamBuilder<List<int>>(
                      stream: wt_Ist_stream,

to

  child: StreamBuilder<List<int>>(
                      stream: wt_Soll_stream,

my dataparser gets the value from characteristic 2. But how can i change my app that my parser starts automatic if one stream (wt_Ist_Stream or wt_Soll_stream) changed and then gets the right value.

The Streambuilder sends the right data to the dataparser but only the stream, which is called in stream:

How can i changed the code, that my parser starts on a stream-change and gets the right values from snapshot.data?

The dataparser-code:

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

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.