edufolly / flutter_bluetooth_serial

A basic Flutter Bluetooth Serial
MIT License
479 stars 465 forks source link

how to increment a counter value only one of getting last value of a bluetooth serial data #186

Closed rasheed-ct closed 1 year ago

rasheed-ct commented 1 year ago

Problem summary

i'm working on a bluetooth serial app and device send data continously i want to increment a counter value by 1 of getting last data of received how can i do this

`void bluetoothDataListener( ) async {

try {

  BluetoothConnection connection = await BluetoothConnection.toAddress(widget.deviceAddress);
  print('Connected to the device form listenToBluetooth method');

  connection.input!.listen((Uint8List data) {
    conn = connection;
    setState(() {
      connection.output.add(data);
    });

    if(ascii.decode(data).contains("out of vein") ){
   setState((){

     modal.controller.checkVein = ascii.decode(data).toString();
     modal.controller.dotsShowHide();
     modal.controller.attempt++;
     // if(modal.controller.attempt == 5){
     //     scoreBasedOnCondition(0);
     // }

     print(ascii.decode(data));
     modal.controller.update();
     // connection.close();
     });
    }
    else if(ascii.decode(data).contains("in vein")){
      setState((){

        modal.controller.checkVein = ascii.decode(data).toString();
        modal.controller.dotsShowHide();
        modal.controller.attempt++;

        Future.delayed(const Duration(seconds: 1), () {
          scoreBasedOnCondition(
              modal.controller.attempt==1?modal.controller.ivPoints = 5:
              modal.controller.attempt==2?modal.controller.ivPoints = 4:
              modal.controller.attempt==3?modal.controller.ivPoints = 3:
              modal.controller.attempt==4?modal.controller.ivPoints = 2:
              modal.controller.attempt==5?modal.controller.ivPoints = 1:
              0
          );

        });
        modal.controller.update();
        connection.close();

      });

    }

  }).onDone(() {
    print('Disconnected by remote request from listenToBluetooth');

  });
}
catch (exception) {

  print("exeeppp   $exception");
}

}`

here is the receiving data:- I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein I/flutter (21893): out of vein

Screenshot_2022-12-19-12-51-41-46_6dc96ee480fc8319ec8cff3e5400c235