From 6a96181d9ac673a7aa66a9077e2f4f8aba90c30f Mon Sep 17 00:00:00 2001 From: Juha Vuolle Date: Thu, 20 Jan 2022 13:01:13 +0200 Subject: Decrement the pending paired device counter only once per device The m_pendingPairedDevices variable keeps track of if there are more devices which should be discovered. The counter was decrement twice instead of once leading to possibly non-ending discovery or missing a discovered device. This patch also introduces a helper method to decrement-and-check the amount of pending paired devices. Using this method in classicBluetoothInfoFromDeviceIdAsync allows to properly finish device discovery in case of an async operation error for the last device in the list. Task-number: QTBUG-99685 Done-with: Ivan Solovev Change-Id: Iaa36b212e8754940d9d574a60379fa0c32fdad2c Reviewed-by: Oliver Wolff (cherry picked from commit 0c76617b0cbb7ad4903c2573c206e00bfb6add59) Reviewed-by: Juha Vuolle --- .../qbluetoothdevicediscoveryagent_winrt.cpp | 32 ++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp') diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp index e515d4e1..8967e424 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp @@ -157,6 +157,8 @@ private: #endif HRESULT onBluetoothLEAdvertisementReceived(IBluetoothLEAdvertisementReceivedEventArgs *args); + void decrementPairedDevicesAndCheckFinished(); + public slots: void finishDiscovery(); @@ -492,10 +494,7 @@ void QWinRTBluetoothDeviceDiscoveryWorker::classicBluetoothInfoFromDeviceIdAsync // on Windows 10 FromIdAsync might ask for device permission. We cannot wait here but have to handle that asynchronously HRESULT hr = m_deviceStatics->FromIdAsync(deviceId, &deviceFromIdOperation); if (FAILED(hr)) { - --m_pendingPairedDevices; - if (!m_pendingPairedDevices - && !(requestedModes & QBluetoothDeviceDiscoveryAgent::LowEnergyMethod)) - finishDiscovery(); + decrementPairedDevicesAndCheckFinished(); qCWarning(QT_BT_WINRT) << "Could not obtain bluetooth device from id"; return S_OK; } @@ -506,15 +505,13 @@ void QWinRTBluetoothDeviceDiscoveryWorker::classicBluetoothInfoFromDeviceIdAsync if (thisPointer) { if (status == Completed) thisPointer->onPairedClassicBluetoothDeviceFoundAsync(op, status); - --thisPointer->m_pendingPairedDevices; + else + thisPointer->decrementPairedDevicesAndCheckFinished(); } return S_OK; }).Get()); if (FAILED(hr)) { - --m_pendingPairedDevices; - if (!m_pendingPairedDevices - && !(requestedModes & QBluetoothDeviceDiscoveryAgent::LowEnergyMethod)) - finishDiscovery(); + decrementPairedDevicesAndCheckFinished(); qCWarning(QT_BT_WINRT) << "Could not register device found callback"; return S_OK; } @@ -522,7 +519,7 @@ void QWinRTBluetoothDeviceDiscoveryWorker::classicBluetoothInfoFromDeviceIdAsync }); if (FAILED(hr)) { emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); - --m_pendingPairedDevices; + decrementPairedDevicesAndCheckFinished(); qCWarning(QT_BT_WINRT) << "Could not obtain bluetooth device from id"; return; } @@ -531,6 +528,10 @@ void QWinRTBluetoothDeviceDiscoveryWorker::classicBluetoothInfoFromDeviceIdAsync // "deviceFound" will be emitted at the end of the deviceFromIdOperation callback void QWinRTBluetoothDeviceDiscoveryWorker::leBluetoothInfoFromDeviceIdAsync(HSTRING deviceId) { + // Note: in this method we do not need to call + // decrementPairedDevicesAndCheckFinished() because we *do* run LE + // scanning, so the condition in the check will always be false. + // It's enough to just decrement m_pendingPairedDevices. HRESULT hr; hr = QEventDispatcherWinRT::runOnXamlThread([deviceId, this]() { ComPtr> deviceFromIdOperation; @@ -548,7 +549,8 @@ void QWinRTBluetoothDeviceDiscoveryWorker::leBluetoothInfoFromDeviceIdAsync(HSTR if (thisPointer) { if (status == Completed) thisPointer->onPairedBluetoothLEDeviceFoundAsync(op, status); - --thisPointer->m_pendingPairedDevices; + else + --thisPointer->m_pendingPairedDevices; } return S_OK; }).Get()); @@ -687,6 +689,14 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onPairedClassicBluetoothDeviceFoun return S_OK; } +void QWinRTBluetoothDeviceDiscoveryWorker::decrementPairedDevicesAndCheckFinished() +{ + if ((--m_pendingPairedDevices == 0) + && !(requestedModes & QBluetoothDeviceDiscoveryAgent::LowEnergyMethod)) { + finishDiscovery(); + } +} + HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onPairedBluetoothLEDeviceFoundAsync(IAsyncOperation *op, AsyncStatus status) { --m_pendingPairedDevices; -- cgit v1.2.3