Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Apr 2012

    Question read() readAll() method of QBluetoothSocket may lose incoming data. Use readData()

    I have reported this issue on https://bugreports.qt-project.org/. The key of the issue is QTSDK-1263.

    I have written a program which uses QBluetoothUuid::SerialPort to transfer data with another bluetooth device. When I use the method read() or readAll() of QBluetoothSocket to read incoming data, I find some data is lost. And it happens obviously. The code is like this:

    socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
    connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
    void BluetoothConnectService::readSocket()
    if (!socket)
    QByteArray data;
    while (socket->bytesAvailable())

    { data += socket->readAll(); }
    emit messageReceived(socket->peerName(),
    QString::fromAscii(data.constData(), data.length()));

    And I also have tried QBluetoothSocket::read(), QBluetoothSocket::atEnd(), but some of the data can still be lost.
    Then I wrote a new class which inherits QBluetoothSocket. It implements a public method readData() which actually uses the protected method readData() of QBluetoothSocket or QIODevice. Only this time it can read all the data and I find no data lost. The code is like this:

    class MyBluetoothSocket : public QBluetoothSocket
    { Q_OBJECT public: explicit MyBluetoothSocket(QObject *parent = 0); MyBluetoothSocket(SocketType socketType, QObject * parent = 0); qint64 readData(char * data, qint64 maxSize = 1024); signals: public slots: };
    qint64 MyBluetoothSocket::readData(char * data, qint64 maxSize)

    { return QBluetoothSocket::readData(data, maxSize); }
    void BluetoothConnectService::readSocket()

    { if (!socket) return; char data[512]; quint64 size; size = socket->readData(data); emit messageReceived(socket->peerName(), QString::fromAscii((data), size)); }

    This problem brings me some troubles when I debug a program using bluetooth serialport, and I have spent half a day to solve this problem. So I think it is a bug.

  2. #2
    Registered User
    Join Date
    Nov 2011

    Re: read() readAll() method of QBluetoothSocket may lose incoming data. Use readData(

    Thanks for sharing!

Similar Threads

  1. Replies: 3
    Last Post: 2011-01-27, 18:14
  2. problem with BTSPP, I lose data
    By felip3 in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2007-06-05, 15:31
  3. Replies: 1
    Last Post: 2006-02-23, 09:57
  4. Replies: 0
    Last Post: 2002-07-03, 09:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts