Home > Blockchain >  flutter bluetooth serial communication with hc-06
flutter bluetooth serial communication with hc-06

Time:06-02

I have asked about this issue multiple times but i dont seem to get the correct answer or maybe I was not providing clear explination of what is going on with me, so I will try to be as clear as I can this time. I am building a flutter app to communicate with microcontroller via bluetooth. I want to send numbers from the microcontroller to my flutter app. I am using flutter-bluetooth-serial package to set the bluetooth communication. So far, what I have already done is creating the connection successfully with the HC-06 bluetooth module which is connected to the microcontroller, and started listening to incoming data.

_getBTConnection(){

    BluetoothConnection.toAddress(widget.server.address).then((_connection){
      connection = _connection;
      isConnecting = false;
      isDisconnecting = false;
      setState(() {});

      connection.input?.listen(_onDataReceived).onDone(() {
        if(isDisconnecting){
          print("Disconnecting locally");


        }else{
          print("Disconnecting remotely");

        }
        if(mounted){
          setState(() {});
        }
        Navigator.of(context).pop();

      });

    }).catchError((error){
      Navigator.of(context).pop();

    });
  }

Once the app receive any data I print it on the compiler:


 void _onDataReceived( data  ){

    if(data != null ){
          print(data);

However, everytime I print the received data it comes with 2 other elements as shown bellow:

I/flutter ( 2705): [64]

I/flutter ( 2705): [175, 6]

The number I sent here is just 6 but this 64, 175 is always printed and here is another example where i sent 12:

I/flutter ( 2705): [64]

I/flutter ( 2705): [175, 12]

The second problem that I am facing is that I want to send float numbers instead of integers Here is what will happen:

In the following example, I sent 2.3 and here is what I receive:

I/flutter ( 2705): [64]

I/flutter ( 2705): [171, 2]

The different here is that when i sent integers the number before the value i sent was 175 but with float its 171 and I am not getting the full number which is 2.3 but I am getting only 2

What Can I change on my code to make it able to receive float numbers and what does this 64 and 171/175 means?

Please can someone help me, I am really struggling with these issues and non of the suggested solutions earlier worked for me

CodePudding user response:

You must be receiving it as ASCII codes. Try this String.fromCharCode(i) -replace i with your value.

  • Related