How to use QLowEnergyService::writeCharacteristic in different thread?
-
How to solve this problem?

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; }; -
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 theWriteCharacteristicThread::writeSizeChanged()signal? If so, you might want to try using theQt::QueuedConnectionconnection type explicitly.Cheers.
-
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 theWriteCharacteristicThread::writeSizeChanged()signal? If so, you might want to try using theQt::QueuedConnectionconnection type explicitly.Cheers.
@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.


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.

-
@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.


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.

-
S swjqq has marked this topic as solved on