Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to ensure complete string is received over bluetooth?
Forum Update on Monday, May 27th 2025

How to ensure complete string is received over bluetooth?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
9 Posts 5 Posters 759 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ahsan737A Offline
    ahsan737A Offline
    ahsan737
    wrote on last edited by ahsan737
    #1

    Greetings,
    I have built an android application that can communicate over Bluetooth. But if disconnects for some reason and tries to re-connect then it causes the application to crash because the string (comma separated values) was received in middle and not from the start. How to deal with this problem?
    Here is the code:

     while (socket->canReadLine())
        {
            QByteArray line = socket->readLine().trimmed();
            emit messageReceived(socket->peerName(),QString ::fromUtf8(line.constData(), line.length()));
            
            QString list= QString::fromUtf8(line.constData(), line.length());
            
            QStringList lista;
            lista= list.split(",");
    
            AnalogA=lista.at(0).toInt();   //left Analog A0
            AnalogB=lista.at(1).toInt();   //right Analog A1
            AnalogC=lista.at(2).toInt();   //status
    }
    

    your suggestions will be much appreciated.

    Best regards,
    Ahsan

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by
      #2

      Hi,

      You can maybe use CRC to be sure that the QString is corresponding to the data you are expecting,

      ahsan737A 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        The most simple: don't assume you got all the data. Check that lista has the required size before extracting data like you are currently doing.

        On a side note, are you sure you are receiving UTF8 data from your bluetooth device ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        ahsan737A 1 Reply Last reply
        2
        • semlanikS Offline
          semlanikS Offline
          semlanik
          wrote on last edited by
          #4

          https://en.wikipedia.org/wiki/Type-length-value

          1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            The most simple: don't assume you got all the data. Check that lista has the required size before extracting data like you are currently doing.

            On a side note, are you sure you are receiving UTF8 data from your bluetooth device ?

            ahsan737A Offline
            ahsan737A Offline
            ahsan737
            wrote on last edited by
            #5

            @SGaist correct me if I am wrongly using UTF8. Currently, I am receiving data from Arduino being sent via Bluetooth with serial mode (8-n-1).

            SGaistS 1 Reply Last reply
            0
            • M mostefa

              Hi,

              You can maybe use CRC to be sure that the QString is corresponding to the data you are expecting,

              ahsan737A Offline
              ahsan737A Offline
              ahsan737
              wrote on last edited by
              #6

              @mostefa can you please explain what CRC is because I am completely ignorant to it.

              1 Reply Last reply
              0
              • ahsan737A ahsan737

                @SGaist correct me if I am wrongly using UTF8. Currently, I am receiving data from Arduino being sent via Bluetooth with serial mode (8-n-1).

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @ahsan737 said in How to ensure complete string is received over bluetooth?:

                @SGaist correct me if I am wrongly using UTF8. Currently, I am receiving data from Arduino being sent via Bluetooth with serial mode (8-n-1).

                Well, I am guessing you are also responsible for the code running on the Arduino but by first guess is that you are likely receiving ASCII rather than UTF8. You should check.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                ahsan737A 1 Reply Last reply
                1
                • SGaistS SGaist

                  @ahsan737 said in How to ensure complete string is received over bluetooth?:

                  @SGaist correct me if I am wrongly using UTF8. Currently, I am receiving data from Arduino being sent via Bluetooth with serial mode (8-n-1).

                  Well, I am guessing you are also responsible for the code running on the Arduino but by first guess is that you are likely receiving ASCII rather than UTF8. You should check.

                  ahsan737A Offline
                  ahsan737A Offline
                  ahsan737
                  wrote on last edited by
                  #8

                  @SGaist you, Arduino is sending ASCII-encoded decimal but according to different articles, ASCII is now a subset of UTF-8. So what do you suggest that I keep using QString::fromUtf8 or I interpret it as an ASCII? If we opt for ASCII format then what will be the replacement of QString::fromUtf8?

                  aha_1980A 1 Reply Last reply
                  0
                  • ahsan737A ahsan737

                    @SGaist you, Arduino is sending ASCII-encoded decimal but according to different articles, ASCII is now a subset of UTF-8. So what do you suggest that I keep using QString::fromUtf8 or I interpret it as an ASCII? If we opt for ASCII format then what will be the replacement of QString::fromUtf8?

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi @ahsan737,

                    If you strictly receive ASCII, then it does not matter if you use QString::fromUtf8() or QString::fromLatin1(); both will give the same result.

                    Regards

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved