[solved]QBluetoothSocket ReadLine problem
I'm working on a mobile Qt application that reads data from a bluetooth device, which sends sensor (accelerometer, etc...) data to the phone. The problem is that when reading from the QBluetoothSocket using the readLine() function, the lines start breaking up (see pictures).
The bluetooth device sends XML data that i parse using a simple XML parser, but even when I tried with a few values only to reduce the overhead the problem persisted. I tried setting the socket's mode to ReadOnly which seemed to help for a while but after a while it came back, and sometimes the problem appears instantly or after a while, rarely not at all, so I don't know if the OpenMode really helped or not.
When reading the data from the rfcomm0 device using cat (cat /dev/rfcomm0), the lines never break up and I always get perfect data, so I figured the problem had to be with the function or the framework.
Here's a screenshot of the terminal, everything is fine:
The first line is broken, but that's normal because the device is continuously sending data even if we're not connected.
Here's a screenshot from qDebug(), reading data from the QByteArray which holds the data saved from readLine(). The data is fine for a while but then starts breaking up:
Here's my readSocket() function:
QByteArray data = socket->readLine().trimmed(); qDebug() << data; Q_EMIT sendSensorData(data); //send to XML parser
I really have no idea why this is happening, but it seems like a Qt framework problem. I tried slowing down the data sending from the device, making it send only a few numbers to see if the size of the string is causing the problem, and even removing things like the trimmed() functions hoping it would help, but none of it did. Setting the socket to ReadMode seemed to help and i thought the problem was gone but it came back. any help is appreciated, thanks!
Turns out I was an idiot!
Had to use the "canReadLine" function to make sure there's a whole line ready to be read from the socket. Thanks anyway.
QByteArray data = socket->readLine().trimmed();
qDebug() << data;
qDebug() << "ERROR";