diff options
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_p.h | 33 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp | 63 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycontroller_win.cpp | 2 |
3 files changed, 72 insertions, 26 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h index ee3531cb..489ed807 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h @@ -80,7 +80,28 @@ QT_END_NAMESPACE #endif #ifdef QT_WIN_BLUETOOTH -#include <QtConcurrent> +QT_BEGIN_NAMESPACE +class QThread; + +class ThreadWorkerLE : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE void discover(); +signals: + void discoveryCompleted(const QVariant res); +}; + +class ThreadWorkerClassic : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE void discover(QVariant hSearch); +signals: + void discoveryCompleted(const QVariant res); +}; +QT_END_NAMESPACE + #elif defined(QT_WINRT_BLUETOOTH) #include <QtCore/QPointer> #include <QtCore/QTimer> @@ -177,9 +198,9 @@ private: void finishDiscovery(QBluetoothDeviceDiscoveryAgent::Error errorCode, const QString &errorText); void startLeDevicesDiscovery(); - void completeLeDevicesDiscovery(); + void completeLeDevicesDiscovery(const QVariant res); void startClassicDevicesDiscovery(Qt::HANDLE hSearch = nullptr); - void completeClassicDevicesDiscovery(); + void completeClassicDevicesDiscovery(const QVariant res); void processDiscoveredDevice(const QBluetoothDeviceInfo &foundDevice); @@ -188,8 +209,10 @@ private: bool pendingStart; bool active; - QFutureWatcher<QVariant> *classicScanWatcher; - QFutureWatcher<QVariant> *lowenergyScanWatcher; + QThread *threadLE = nullptr; + QThread *threadClassic = nullptr; + ThreadWorkerLE *threadWorkerLE = nullptr; + ThreadWorkerClassic *threadWorkerClassic = nullptr; #endif #ifdef QT_WINRT_BLUETOOTH diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp index a7755696..3d1b0c4b 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp @@ -44,7 +44,8 @@ #include "qbluetoothlocaldevice_p.h" #include <QtCore/qmutex.h> -#include <QtConcurrent/QtConcurrent> +#include <QtCore/QThread> +#include <QtCore/QLoggingCategory> #include <qt_windows.h> #include <setupapi.h> @@ -332,17 +333,34 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( , pendingCancel(false) , pendingStart(false) , active(false) - , classicScanWatcher(nullptr) - , lowenergyScanWatcher(nullptr) , lowEnergySearchTimeout(-1) // remains -1 -> timeout not supported , q_ptr(parent) { + threadLE = new QThread; + threadWorkerLE = new ThreadWorkerLE; + threadWorkerLE->moveToThread(threadLE); + connect(threadWorkerLE, &ThreadWorkerLE::discoveryCompleted, this, &QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery); + connect(threadLE, &QThread::finished, threadWorkerLE, &ThreadWorkerLE::deleteLater); + connect(threadLE, &QThread::finished, threadLE, &QThread::deleteLater); + threadLE->start(); + + threadClassic = new QThread; + threadWorkerClassic = new ThreadWorkerClassic; + threadWorkerClassic->moveToThread(threadClassic); + connect(threadWorkerClassic, &ThreadWorkerClassic::discoveryCompleted, this, &QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery); + connect(threadClassic, &QThread::finished, threadWorkerClassic, &ThreadWorkerClassic::deleteLater); + connect(threadClassic, &QThread::finished, threadClassic, &QThread::deleteLater); + threadClassic->start(); } QBluetoothDeviceDiscoveryAgentPrivate::~QBluetoothDeviceDiscoveryAgentPrivate() { if (active) stop(); + if (threadLE) + threadLE->quit(); + if (threadClassic) + threadClassic->quit(); } bool QBluetoothDeviceDiscoveryAgentPrivate::isActive() const @@ -447,23 +465,17 @@ void QBluetoothDeviceDiscoveryAgentPrivate::finishDiscovery(QBluetoothDeviceDisc void QBluetoothDeviceDiscoveryAgentPrivate::startLeDevicesDiscovery() { - if (!lowenergyScanWatcher) { - lowenergyScanWatcher = new QFutureWatcher<QVariant>(this); - connect(lowenergyScanWatcher, &QFutureWatcher<QVariant>::finished, - this, &QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery); - } - const QFuture<QVariant> future = QtConcurrent::run(discoverLeDevicesStatic); - lowenergyScanWatcher->setFuture(future); + QMetaObject::invokeMethod(threadWorkerLE, "discover", Qt::QueuedConnection); } -void QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery() +void QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery(const QVariant res) { if (pendingCancel && !pendingStart) { cancelDiscovery(); } else if (pendingStart) { restartDiscovery(); } else { - const DiscoveryResult result = lowenergyScanWatcher->result().value<DiscoveryResult>(); + const DiscoveryResult result = res.value<DiscoveryResult>(); if (result.systemErrorCode == NO_ERROR || result.systemErrorCode == ERROR_NO_MORE_ITEMS) { for (const QBluetoothDeviceInfo &device : result.devices) processDiscoveredDevice(device); @@ -484,18 +496,13 @@ void QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery() void QBluetoothDeviceDiscoveryAgentPrivate::startClassicDevicesDiscovery(Qt::HANDLE hSearch) { - if (!classicScanWatcher) { - classicScanWatcher = new QFutureWatcher<QVariant>(this); - connect(classicScanWatcher, &QFutureWatcher<QVariant>::finished, - this, &QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery); - } - const QFuture<QVariant> future = QtConcurrent::run(discoverClassicDevicesStatic, hSearch); - classicScanWatcher->setFuture(future); + QMetaObject::invokeMethod(threadWorkerClassic, "discover", Qt::QueuedConnection, + Q_ARG(QVariant, QVariant::fromValue(hSearch))); } -void QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery() +void QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery(const QVariant res) { - const DiscoveryResult result = classicScanWatcher->result().value<DiscoveryResult>(); + const DiscoveryResult result = res.value<DiscoveryResult>(); if (pendingCancel && !pendingStart) { closeClassicSearch(result.hSearch); cancelDiscovery(); @@ -560,6 +567,20 @@ void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevice( } } + +void ThreadWorkerLE::discover() +{ + const QVariant res = discoverLeDevicesStatic(); + emit discoveryCompleted(res); +} + +void ThreadWorkerClassic::discover(QVariant search) +{ + Qt::HANDLE hSearch = search.value<Qt::HANDLE>(); + const QVariant res = discoverClassicDevicesStatic(hSearch); + emit discoveryCompleted(res); +} + QT_END_NAMESPACE Q_DECLARE_METATYPE(DiscoveryResult) diff --git a/src/bluetooth/qlowenergycontroller_win.cpp b/src/bluetooth/qlowenergycontroller_win.cpp index 9b2ab6a2..c6ea5c78 100644 --- a/src/bluetooth/qlowenergycontroller_win.cpp +++ b/src/bluetooth/qlowenergycontroller_win.cpp @@ -46,6 +46,8 @@ #include <QtCore/QEvent> #include <QtCore/QMutex> #include <QtCore/QThread> +#include <QtCore/QDataStream> +#include <QtCore/QCoreApplication> #include <algorithm> // for std::max |