diff options
Diffstat (limited to 'src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp | 75 |
1 files changed, 36 insertions, 39 deletions
diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp index f1476758..e8d64efc 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp +++ b/src/bluetooth/qbluetoothservicediscoveryagent_winrt.cpp @@ -82,6 +82,13 @@ Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINRT) #define TYPE_STRING 37 #define TYPE_SEQUENCE 53 +// Helper to reverse given uchar array +static void reverseArray(uchar data[], size_t length) +{ + for (size_t i = length; i > length/2; i--) + std::swap(data[length - i], data[i - 1]); +} + class QWinRTBluetoothServiceDiscoveryWorker : public QObject { Q_OBJECT @@ -94,7 +101,6 @@ public: Q_SIGNALS: void serviceFound(quint64 deviceAddress, const QBluetoothServiceInfo &info); void scanFinished(quint64 deviceAddress); - void scanCanceled(); void errorOccured(); private: @@ -309,6 +315,8 @@ void QWinRTBluetoothServiceDiscoveryWorker::processServiceSearchResult(quint64 a GUID value; hr = dataReader->ReadGuid(&value); Q_ASSERT_SUCCEEDED(hr); + // The latter 8 bytes are in reverse order + reverseArray(value.Data4, sizeof(value.Data4)/sizeof(value.Data4[0])); const QBluetoothUuid uuid(value); info.setAttribute(key, uuid); qCDebug(QT_BT_WINRT) << "UUID" << uuid << "KEY" << hex << key << "TYPE" << dec << type << "UUID" << hex << uuid; @@ -422,7 +430,8 @@ QBluetoothServiceInfo::Sequence QWinRTBluetoothServiceDiscoveryWorker::readSeque GUID b; hr = dataReader->ReadGuid(&b); Q_ASSERT_SUCCEEDED(hr); - + // The latter 8 bytes are in reverse order + reverseArray(b.Data4, sizeof(b.Data4)/sizeof(b.Data4[0])); const QBluetoothUuid uuid(b); result.append(QVariant::fromValue(uuid)); remainingLength -= sizeof(GUID); @@ -494,7 +503,7 @@ QBluetoothServiceDiscoveryAgentPrivate::QBluetoothServiceDiscoveryAgentPrivate( QBluetoothServiceDiscoveryAgentPrivate::~QBluetoothServiceDiscoveryAgentPrivate() { - stop(); + releaseWorker(); } void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &address) @@ -508,8 +517,6 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr this, &QBluetoothServiceDiscoveryAgentPrivate::processFoundService, Qt::QueuedConnection); connect(worker, &QWinRTBluetoothServiceDiscoveryWorker::scanFinished, this, &QBluetoothServiceDiscoveryAgentPrivate::onScanFinished, Qt::QueuedConnection); - connect(worker, &QWinRTBluetoothServiceDiscoveryWorker::scanCanceled, - this, &QBluetoothServiceDiscoveryAgentPrivate::onScanCanceled, Qt::QueuedConnection); connect(worker, &QWinRTBluetoothServiceDiscoveryWorker::errorOccured, this, &QBluetoothServiceDiscoveryAgentPrivate::onError, Qt::QueuedConnection); worker->start(); @@ -517,20 +524,9 @@ void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &addr void QBluetoothServiceDiscoveryAgentPrivate::stop() { - if (!worker) - return; - - disconnect(worker, &QWinRTBluetoothServiceDiscoveryWorker::serviceFound, - this, &QBluetoothServiceDiscoveryAgentPrivate::processFoundService); - disconnect(worker, &QWinRTBluetoothServiceDiscoveryWorker::scanFinished, - this, &QBluetoothServiceDiscoveryAgentPrivate::onScanFinished); - disconnect(worker, &QWinRTBluetoothServiceDiscoveryWorker::scanCanceled, - this, &QBluetoothServiceDiscoveryAgentPrivate::onScanCanceled); - disconnect(worker, &QWinRTBluetoothServiceDiscoveryWorker::errorOccured, - this, &QBluetoothServiceDiscoveryAgentPrivate::onError); - // mWorker will delete itself as soon as it is done with its discovery - worker = nullptr; - setDiscoveryState(Inactive); + releaseWorker(); + Q_Q(QBluetoothServiceDiscoveryAgent); + emit q->canceled(); } void QBluetoothServiceDiscoveryAgentPrivate::processFoundService(quint64 deviceAddress, const QBluetoothServiceInfo &info) @@ -579,26 +575,12 @@ void QBluetoothServiceDiscoveryAgentPrivate::processFoundService(quint64 deviceA void QBluetoothServiceDiscoveryAgentPrivate::onScanFinished(quint64 deviceAddress) { - Q_Q(QBluetoothServiceDiscoveryAgent); - bool deviceFound; - for (const QBluetoothDeviceInfo &deviceInfo : qAsConst(discoveredDevices)) { - if (deviceInfo.address().toUInt64() == deviceAddress) { - deviceFound = true; - discoveredDevices.removeOne(deviceInfo); - if (discoveredDevices.isEmpty()) - setDiscoveryState(Inactive); - break; - } - } - Q_ASSERT(deviceFound); - stop(); - emit q->finished(); -} - -void QBluetoothServiceDiscoveryAgentPrivate::onScanCanceled() -{ - Q_Q(QBluetoothServiceDiscoveryAgent); - emit q->canceled(); + // The scan for a device's services has finished. Disconnect the + // worker and call the baseclass function which starts the scan for + // the next device if there are any unscanned devices left (or finishes + // the scan if none left) + releaseWorker(); + _q_serviceDiscoveryFinished(); } void QBluetoothServiceDiscoveryAgentPrivate::onError() @@ -610,6 +592,21 @@ void QBluetoothServiceDiscoveryAgentPrivate::onError() emit q->error(error); } +void QBluetoothServiceDiscoveryAgentPrivate::releaseWorker() +{ + if (!worker) + return; + + disconnect(worker, &QWinRTBluetoothServiceDiscoveryWorker::serviceFound, + this, &QBluetoothServiceDiscoveryAgentPrivate::processFoundService); + disconnect(worker, &QWinRTBluetoothServiceDiscoveryWorker::scanFinished, + this, &QBluetoothServiceDiscoveryAgentPrivate::onScanFinished); + disconnect(worker, &QWinRTBluetoothServiceDiscoveryWorker::errorOccured, + this, &QBluetoothServiceDiscoveryAgentPrivate::onError); + // mWorker will delete itself as soon as it is done with its discovery + worker = nullptr; +} + QT_END_NAMESPACE #include <qbluetoothservicediscoveryagent_winrt.moc> |