Unsolved How to ensure complete string is received over bluetooth?
-
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 -
Hi,
You can maybe use CRC to be sure that the QString is corresponding to the data you are expecting,
-
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 ?
-
-
@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).
-
@mostefa can you please explain what CRC is because I am completely ignorant to it.
-
@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.
-
@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 ofQString::fromUtf8
? -
Hi @ahsan737,
If you strictly receive ASCII, then it does not matter if you use
QString::fromUtf8()
orQString::fromLatin1()
; both will give the same result.Regards