diff options
4 files changed, 44 insertions, 80 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h index d60e89fa..cb12352f 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h @@ -83,23 +83,13 @@ QT_END_NAMESPACE QT_BEGIN_NAMESPACE class QThread; -class ThreadWorkerLE : public QObject +class ThreadWorkerDeviceDiscovery : 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(const QVariant &hSearch); -signals: - void discoveryCompleted(const QVariant res); -}; QT_END_NAMESPACE #elif defined(QT_WINRT_BLUETOOTH) @@ -211,8 +201,8 @@ private: QThread *threadLE = nullptr; QThread *threadClassic = nullptr; - ThreadWorkerLE *threadWorkerLE = nullptr; - ThreadWorkerClassic *threadWorkerClassic = nullptr; + ThreadWorkerDeviceDiscovery *threadWorkerLE = nullptr; + ThreadWorkerDeviceDiscovery *threadWorkerClassic = nullptr; #endif #ifdef QT_WINRT_BLUETOOTH diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp index e1c6bea4..7509c5f6 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp @@ -282,28 +282,6 @@ struct DiscoveryResult { HBLUETOOTH_DEVICE_FIND hSearch; // Used only for classic devices }; -static QVariant discoverLeDevicesStatic() -{ - DiscoveryResult result; // Do not use hSearch here! - result.systemErrorCode = NO_ERROR; - result.devices = enumerateLeDevices(&result.systemErrorCode); - return QVariant::fromValue(result); -} - -static QVariant discoverClassicDevicesStatic(HBLUETOOTH_DEVICE_FIND hSearch) -{ - DiscoveryResult result; - result.hSearch = hSearch; - result.systemErrorCode = NO_ERROR; - - const QBluetoothDeviceInfo device = hSearch - ? findNextClassicDevice(&result.systemErrorCode, result.hSearch) - : findFirstClassicDevice(&result.systemErrorCode, &result.hSearch); - - result.devices.append(device); - return QVariant::fromValue(result); -} - QString QBluetoothDeviceDiscoveryAgentPrivate::discoveredLeDeviceSystemPath( const QBluetoothAddress &deviceAddress) { @@ -332,18 +310,18 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( , q_ptr(parent) { threadLE = new QThread; - threadWorkerLE = new ThreadWorkerLE; + threadWorkerLE = new ThreadWorkerDeviceDiscovery; threadWorkerLE->moveToThread(threadLE); - connect(threadWorkerLE, &ThreadWorkerLE::discoveryCompleted, this, &QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery); - connect(threadLE, &QThread::finished, threadWorkerLE, &ThreadWorkerLE::deleteLater); + connect(threadWorkerLE, &ThreadWorkerDeviceDiscovery::discoveryCompleted, this, &QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery); + connect(threadLE, &QThread::finished, threadWorkerLE, &ThreadWorkerDeviceDiscovery::deleteLater); connect(threadLE, &QThread::finished, threadLE, &QThread::deleteLater); threadLE->start(); threadClassic = new QThread; - threadWorkerClassic = new ThreadWorkerClassic; + threadWorkerClassic = new ThreadWorkerDeviceDiscovery; threadWorkerClassic->moveToThread(threadClassic); - connect(threadWorkerClassic, &ThreadWorkerClassic::discoveryCompleted, this, &QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery); - connect(threadClassic, &QThread::finished, threadWorkerClassic, &ThreadWorkerClassic::deleteLater); + connect(threadWorkerClassic, &ThreadWorkerDeviceDiscovery::discoveryCompleted, this, &QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery); + connect(threadClassic, &QThread::finished, threadWorkerClassic, &ThreadWorkerDeviceDiscovery::deleteLater); connect(threadClassic, &QThread::finished, threadClassic, &QThread::deleteLater); threadClassic->start(); } @@ -460,7 +438,14 @@ void QBluetoothDeviceDiscoveryAgentPrivate::finishDiscovery(QBluetoothDeviceDisc void QBluetoothDeviceDiscoveryAgentPrivate::startLeDevicesDiscovery() { - QMetaObject::invokeMethod(threadWorkerLE, "discover", Qt::QueuedConnection); + const auto threadWorker = threadWorkerLE; + QMetaObject::invokeMethod(threadWorkerLE, [threadWorker]() + { + DiscoveryResult result; // Do not use hSearch here! + result.systemErrorCode = NO_ERROR; + result.devices = enumerateLeDevices(&result.systemErrorCode); + emit threadWorker->discoveryCompleted(QVariant::fromValue(result)); + }, Qt::QueuedConnection); } void QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery(const QVariant &res) @@ -491,8 +476,20 @@ void QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery(const QVa void QBluetoothDeviceDiscoveryAgentPrivate::startClassicDevicesDiscovery(Qt::HANDLE hSearch) { - QMetaObject::invokeMethod(threadWorkerClassic, "discover", Qt::QueuedConnection, - Q_ARG(QVariant, QVariant::fromValue(hSearch))); + const auto threadWorker = threadWorkerClassic; + QMetaObject::invokeMethod(threadWorker, [threadWorker, hSearch]() + { + DiscoveryResult result; + result.hSearch = hSearch; + result.systemErrorCode = NO_ERROR; + + const QBluetoothDeviceInfo device = hSearch + ? findNextClassicDevice(&result.systemErrorCode, result.hSearch) + : findFirstClassicDevice(&result.systemErrorCode, &result.hSearch); + + result.devices.append(device); + emit threadWorker->discoveryCompleted(QVariant::fromValue(result)); + }, Qt::QueuedConnection); } void QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery(const QVariant &res) @@ -562,20 +559,6 @@ void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevice( } } - -void ThreadWorkerLE::discover() -{ - const QVariant res = discoverLeDevicesStatic(); - emit discoveryCompleted(res); -} - -void ThreadWorkerClassic::discover(const 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/qbluetoothservicediscoveryagent_p.h b/src/bluetooth/qbluetoothservicediscoveryagent_p.h index c845b528..37943c15 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_p.h +++ b/src/bluetooth/qbluetoothservicediscoveryagent_p.h @@ -81,9 +81,6 @@ class QThread; class ThreadWorkerFind : public QObject { Q_OBJECT -public: - Q_INVOKABLE void findFirst(const QVariant &data); - Q_INVOKABLE void findNext(const QVariant &data); signals: void findFinished(QVariant result); }; diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_win.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_win.cpp index d4e00c0a..2c3c6437 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_win.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent_win.cpp @@ -389,8 +389,12 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr pendingFinish = true; pendingStop = false; - QMetaObject::invokeMethod(threadWorkerFind, "findFirst", Qt::QueuedConnection, - Q_ARG(QVariant, QVariant::fromValue(address))); + const auto threadWorker = threadWorkerFind; + QMetaObject::invokeMethod(threadWorkerFind, [threadWorker, address]() + { + FindServiceResult result = findFirstService(address); + emit threadWorker->findFinished(QVariant::fromValue(result)); + }, Qt::QueuedConnection); } } @@ -429,8 +433,13 @@ void QBluetoothServiceDiscoveryAgentPrivate::_q_nextSdpScan(const QVariant &inpu emit q->serviceDiscovered(result.info); } } - QMetaObject::invokeMethod(threadWorkerFind, "findNext", Qt::QueuedConnection, - Q_ARG(QVariant, QVariant::fromValue(result.hSearch))); + const auto threadWorker = threadWorkerFind; + const auto hSearch = result.hSearch; + QMetaObject::invokeMethod(threadWorkerFind, [threadWorker, hSearch]() + { + FindServiceResult result = findNextService(hSearch); + emit threadWorker->findFinished(QVariant::fromValue(result)); + }, Qt::QueuedConnection); return; } pendingFinish = false; @@ -438,21 +447,6 @@ void QBluetoothServiceDiscoveryAgentPrivate::_q_nextSdpScan(const QVariant &inpu } } -void ThreadWorkerFind::findFirst(const QVariant &data) -{ - auto address = data.value<QBluetoothAddress>(); - FindServiceResult result = findFirstService(address); - emit findFinished(QVariant::fromValue(result)); -} - - void ThreadWorkerFind::findNext(const QVariant &data) -{ - auto hSearch = data.value<Qt::HANDLE>(); - FindServiceResult result = findNextService(hSearch); - emit findFinished(QVariant::fromValue(result)); -} - QT_END_NAMESPACE -Q_DECLARE_METATYPE(Qt::HANDLE) Q_DECLARE_METATYPE(FindServiceResult) |