HC-05 with Flutter Blue-tooth Serial Module

2020-05-23 flutter arduino bluetooth hc-05

I was using HC-05 Bluetooth module to send data like d001;255;255;255;255;255;255;255;255; over BlueTooth and on app side I am using Flutter to receive the data with the help of below function with the help of flutter_bluetooth_serial module:

  void initiateBluetoothConnect() async {
    try {
      BluetoothConnection connection =
          await BluetoothConnection.toAddress("98:D3:31:F9:63:C5");
      print('Connected to the device');

      connection.input.listen((Uint8List data) async {
        print('Data incoming: ${ascii.decode(data)}');
        connection.output.add(utf8.encode("OK" + "\r\n")); // Sending data

        if (ascii.decode(data).contains('!')) {
          connection.finish(); // Closing connection
          print('Disconnecting by local host');
        }
      }).onDone(() {
        print('Disconnected by remote request');
      });
    } catch (exception) {
      print('Cannot connect, exception occured');
    }
  }

Problem is that I am not getting full chunk at a single go, I am getting data like

d0
01;255;25
5;255;255;255;
25
255...u get the idea

Is there any way to define chunk side or I need to add a special character at the end of d001;255;255;255;255;255;255;255;255; so that in-app side I can monitor for end of the transmission, like

     if (ascii.decode(data).contains("*")) {
          incomingData += ascii.decode(data).replaceAll("\n", "");
          print("incoming data : " + incomingData);
          incomingData = "";
        } else {
          incomingData += ascii.decode(data).replaceAll("\n", "");
        }

Answers

Related