Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use QLowEnergyService::writeCharacteristic in different thread?

How to use QLowEnergyService::writeCharacteristic in different thread?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 384 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swjqq
    wrote on last edited by swjqq
    #1

    How to solve this problem?
    3d0cce93-53d5-4b91-96cf-182ad9d732e2-image.png

    Here is my code:

    class WriteCharacteristicThread : public QThread
    {
        Q_OBJECT
    
    public:
        WriteCharacteristicThread(QByteArray *buffer = nullptr, int packetSize = 244, int delayMs = 100, QLowEnergyService *service = nullptr, QString uuidStr = "")
        {
            m_service = service;
            if (m_service != nullptr)
            {
                m_characteristic = m_service->characteristic(QBluetoothUuid(uuidStr));
            }
    
            m_buffer = buffer;
            m_packetSize = packetSize;
            m_delayMs = delayMs;
        }
    
    signals:
        void writeSizeChanged(int size);
    
    protected:
        void run() override
        {
            if (!m_service || !m_characteristic.isValid() || !m_buffer || !m_buffer->size())
            {
                return;
            }
    
            QList<QByteArray> blocks;
            for (int i = 0; i < m_buffer->size(); i += m_packetSize)
            {
                blocks.append(m_buffer->mid(i, m_packetSize));
            }
    
            for (int i = 0; i < blocks.size(); i++)
            {
                m_service->writeCharacteristic(m_characteristic, blocks[i]);
                m_writeSize += blocks[i].size();
                emit writeSizeChanged(m_writeSize);
    
                msleep(m_delayMs);
            }
        }
    
    private:
        QByteArray *m_buffer = nullptr;
        int m_packetSize = 244;
        QLowEnergyService *m_service = nullptr;
        QLowEnergyCharacteristic m_characteristic;
        int m_delayMs = 0;
        int m_writeSize = 0;
    };
    
    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      Hi @swjqq,

      I see nothing in your thread that's creating any QObject's with non-null parents, nor setting any child/parent relationships (they could be there, but I don't see it).

      Have you tried stepping through the code with a debugger to see which line is causing the error?

      Also, do you have any code connect'd to the WriteCharacteristicThread::writeSizeChanged() signal? If so, you might want to try using the Qt::QueuedConnection connection type explicitly.

      Cheers.

      S 1 Reply Last reply
      0
      • Paul ColbyP Paul Colby

        Hi @swjqq,

        I see nothing in your thread that's creating any QObject's with non-null parents, nor setting any child/parent relationships (they could be there, but I don't see it).

        Have you tried stepping through the code with a debugger to see which line is causing the error?

        Also, do you have any code connect'd to the WriteCharacteristicThread::writeSizeChanged() signal? If so, you might want to try using the Qt::QueuedConnection connection type explicitly.

        Cheers.

        S Offline
        S Offline
        swjqq
        wrote on last edited by swjqq
        #3

        @Paul-Colby Thanks for your reply.

        Have you tried stepping through the code with a debugger to see which line is causing the error?

        This error occurs when the WriteCharacteristicThread::run() function executes.

        7f026d77-72e9-473f-807f-8aac8425ac47-image.png

        e9caedf5-ec3d-41a7-b89b-11683eb7e7f0-image.png

        Also, do you have any code connect'd to the WriteCharacteristicThread::writeSizeChanged() signal? If so, you might want to try using the Qt::QueuedConnection connection type explicitly.

        b7ff0abb-c783-44f5-aaae-b6e25095622b-image.png

        jsulmJ 1 Reply Last reply
        0
        • S swjqq

          @Paul-Colby Thanks for your reply.

          Have you tried stepping through the code with a debugger to see which line is causing the error?

          This error occurs when the WriteCharacteristicThread::run() function executes.

          7f026d77-72e9-473f-807f-8aac8425ac47-image.png

          e9caedf5-ec3d-41a7-b89b-11683eb7e7f0-image.png

          Also, do you have any code connect'd to the WriteCharacteristicThread::writeSizeChanged() signal? If so, you might want to try using the Qt::QueuedConnection connection type explicitly.

          b7ff0abb-c783-44f5-aaae-b6e25095622b-image.png

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @swjqq Please post code as text, not pictures. m_service and m_characteristic are not allocated in your thread.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S swjqq has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved