Qt bluetooth chat example - Read data.



  • Hello I need to process data which comes from bluetooth interface but I cant see anything in the buffer.

    line is append to QString variable. I got this code from bluetooth chat example.

    void MainWindow::readSocket()
    {
        QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
        if (!socket)
            return;
    
        QString data="";
    
        while (socket->canReadLine()) {
           QByteArray line = socket->readLine().trimmed();
    
            emit messageReceived(socket->peerName(),QString::fromUtf8(line.constData(), line.length()));
            data.append(line+"-");
        }
        qDebug()<<data<<endl;
    }
    

    Whay I see on console is ""

    What Im doing wrong here?
    Thanks in advance.



  • Hi @Julian,

    Which kind of data are you waiting for ?
    By this way you can received only "ASCII" data. So if you receive binary , and it starts with character which cannot be converted as ASCII, typically control characters like NUL(0), STX, ETX, and all the others, your string will be empty. So if canReadLine() returns true, and your string is empty, I guess this is probably the case here.

    You can try to display the data in hexadecimal to see what is really in your buffer.
    You could also remove the call to QByteArray::trimmed() to be sure you don't truncate your data, at least during debugging phase.

    QByteArray line = socket->readLine();
    emit messageReceived(socket->peerName(), QString(line.toHex()));
    


  • @Gojir4 said in Qt bluetooth chat example - Read data.:

    emit messageReceived(socket->peerName(), QString(line.toHex()));

    Hello and thanks for the reply. If I used this code line and it works:

    QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
        if (!socket)
            return;
    
        QString data=socket->readAll();
        qDebug()<<data<<endl;
    

    I can read the data that I sents through bluetooth. Jus to check I was sending from mobile a 100 value and I read "d", which its fine!

    But I cant read anything with:

      QString chain="";
        while (socket->canReadLine()) {
           //QByteArray line = socket->readLine().trimmed();
            QByteArray line = socket->readLine();
    
            emit messageReceived(socket->peerName(),QString::fromUtf8(line.constData(), line.length()));
    
     //       emit messageReceived(socket->peerName(), QString(line.toHex()));
    
            qDebug()<<"canReadLine: "+line<<endl;
            QString temp(line);
           chain.append(temp+"-");
        }
        qDebug()<<"canReadLine: "+chain<<endl;
    

    So, canReadLine() is not working

    This is the complete code:

    void MainWindow::readSocket()
    {
        QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
        if (!socket)
            return;
    
        QString data=socket->readAll();
        qDebug()<<"Read All: "+data<<endl;
    
        QString chain="";
        while (socket->canReadLine()) {
           //QByteArray line = socket->readLine().trimmed();
            QByteArray line = socket->readLine();
    
            emit messageReceived(socket->peerName(),QString::fromUtf8(line.constData(), line.length()));
    
     //       emit messageReceived(socket->peerName(), QString(line.toHex()));
    
            qDebug()<<"canReadLine: "+line<<endl;
            QString temp(line);
           chain.append(temp+"-");
        }
        qDebug()<<"canReadLine: "+chain<<endl;
    
    }
    

    So, I will use readAll() in the meanwhile.

    Thanks again.



  • @Julian
    I don't know how behaves canReadLine() with binary data.

    But if you send "a 100 value" from a mobile, this probably means that you are sending the data in BINARY but by using readLine(), you are trying to read data in ASCII.

    So you have to use the same format for sending and reading the data.
    Can you try to send a string "100\n" from your mobile? This should work
    In binary, this will make the following frame: [0x31, 0x30, 0x30, 0x13] in hex or [49, 48, 48, 19] in integer.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.