Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Bluetooth low Energy: Sensor Data is split up using QByteArray



  • Hello guys,

    I am creating a Desktop Application for sending data from a sensor to the pc using Qt Bluetooth Low Energy. Using Arduino I am sending 12 bytes of data in every one second. The data I am sending every second consist of traxial acceleration and gyroscope data.
    Every time, when the data changes, the application gets notified via SLOT:

    void MainWindow::updateData(const QLowEnergyCharacteristic &c,const QByteArray &value){
       ui->dataList->addItem(value);
       ui->dataList->scrollToBottom();
    }
    

    The problem is, that the data is split up in several parts (up to 3 lines that are displayed every second in my listwidget but i expect only one line with the whole data).

    For example I expect to receive 12345678912345678912 in one line, but the data is split up to
    123456789123
    456789
    12

    What could be the problem? Maybe my data container the QByteArray has some limitations? Because on another app from the manufacturer of the development board I am using to send the data, the data are sent like expected (one line every second).



  • Okay, I have solved the problem with a workaround: In the code for my development board I added a new line character after every value.
    Then, in my Qt code I check every time the SLOT is called if the value does have a new line char.
    Either the value is saved in another variable until it receives a value with a new line character or it is output immediately (if it has a new line character).

    static QString newValue= "";
    
    void MainWindow::updateData(const QLowEnergyCharacteristic &c,const QByteArray &value)
    {
        newValue = newValue + (QString)value;
        if (newValue.contains("\n") || newValue.contains("\r")){
                ui->dataList->addItem(newValue.simplified());
                ui->dataList->scrollToBottom();
            newValue = "";
        }
    }
    


  • @TUStudi said in Bluetooth low Energy: Sensor Data is split up using QByteArray:

    For example I expect to receive 12345678912345678912 in one line, but the data is split up to
    123456789123
    456789
    12

    Are these three lines one item or is the data split into three items?
    What happens, if you resize your window? Maybe it's just a word wrap issue.

    Does your data contain newline characters (e.g. \n = 10 = 0x0A)?



  • Are these three lines one item or is the data split into three items?
    What happens, if you resize your window? Maybe it's just a word wrap issue.

    Does your data contain newline characters (e.g. \n = 10 = 0x0A)?

    @Pl45m4 Thanks for your response.
    No, these three lines should be one item. because on my development board, where the data comes from, I made a test by sending the "123456789123" every second so I expect my GUI Application in Qt to print 123456789123 in one line as one item but it is sending it in 2 or 3 parts as I mentioned above.
    I tried to resize the window and the list Widget but it seems that this is not the problem.
    Before adding the items to the List I tried to figure out the size of the value that I receives from the QByteArray and it seems that the data is split up the in ByteArray because the output of value.size() of 12345678912345678912 should be 20 but the output is

    // output of  value.size()
    12
    6
    2
    

    whch is in total 20. So I think maybe the problem is the QByteArray? I don't think that it is the code of my development board because as I mentioned before on another app from the manufacturer the data are not split up..
    An no I do not have any new line characters.



  • @TUStudi said in Bluetooth low Energy: Sensor Data is split up using QByteArray:

    the output is
    // output of value.size()
    12
    6
    2

    This contradicts

    @TUStudi said in Bluetooth low Energy: Sensor Data is split up using QByteArray:

    No, these three lines should be one item

    with this.

    You said the update-slot is called every second. So you add a new item (containing value) to your QListWidget. If you debug value.size() in this slot and it returns 12, 6, 2 it can not be one item (because a ByteArray has just one size)?!
    So your update slot got called multiple times


  • Qt Champions 2019

    @TUStudi said in Bluetooth low Energy: Sensor Data is split up using QByteArray:

    Every time, when the data changes, the application gets notified via SLOT:

    Do oyu call this slot? From where is it called?



  • @Christian-Ehrlicher

    void MainWindow::on_callCharacteristic_clicked()
    {
        readChar = uartService->characteristic(QBluetoothUuid(QUuid("6e400003-b5a3-f393-e0a9-e50e24dcca9e")));
        QLowEnergyDescriptor  descript = readChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
        uartService->writeDescriptor(descript, QByteArray::fromHex("0100"));
    
        connect(uartService, SIGNAL(characteristicChanged(QLowEnergyCharacteristic,QByteArray)),
                this, SLOT(updateData(QLowEnergyCharacteristic,QByteArray)));
    }
    

    When a button is clicked in the GUI this method is called. And as @Pl45m4 outlined the update slot is calling multiple times (sorry for the confusing description):

    void MainWindow::updateData(const QLowEnergyCharacteristic &c,const QByteArray &value){
       ui->dataList->addItem(value);
       ui->dataList->scrollToBottom();
    }
    

    So every time the characteristic c changes (means whenever there is a new sensor value) this SLOT is called.
    Here is the output from my QT GUI App:

    5dd78fbe-4b72-4556-9746-5658dd6fc8cc-image.png

    and I expect this (the whole time):

    c730c708-3ed6-4bff-bca0-7f041818463b-image.png



  • Okay, I have solved the problem with a workaround: In the code for my development board I added a new line character after every value.
    Then, in my Qt code I check every time the SLOT is called if the value does have a new line char.
    Either the value is saved in another variable until it receives a value with a new line character or it is output immediately (if it has a new line character).

    static QString newValue= "";
    
    void MainWindow::updateData(const QLowEnergyCharacteristic &c,const QByteArray &value)
    {
        newValue = newValue + (QString)value;
        if (newValue.contains("\n") || newValue.contains("\r")){
                ui->dataList->addItem(newValue.simplified());
                ui->dataList->scrollToBottom();
            newValue = "";
        }
    }