diff options
author | Oliver Wolff <oliver.wolff@qt.io> | 2021-03-31 10:30:08 +0200 |
---|---|---|
committer | Oliver Wolff <oliver.wolff@qt.io> | 2021-04-08 08:50:03 +0200 |
commit | 085af5ef811e293377ba92cd12dbda926db27620 (patch) | |
tree | f11c3cc9a0bce98e0a9715b07bff2cbf9c0c8485 /src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp | |
parent | 1cddac5027944cf7ac5296b5e8bbd18be3f9fdbf (diff) |
QWinRTBluetoothDeviceDiscoveryWorker: Add error handling
Instead of asserting everywhere, we at least set the error and return
gracefully.
Fixes: QTBUG-90369
Change-Id: I143408b4b30322bb8b490d5848f731f753669015
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
Diffstat (limited to 'src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp | 273 |
1 files changed, 209 insertions, 64 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp index 94abc3d3..87e09a99 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp @@ -71,6 +71,13 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINRT) +#define EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED(msg, error, ret) \ + if (FAILED(hr)) { \ + emit errorOccured(error); \ + qCWarning(QT_BT_WINRT) << msg; \ + ret; \ + } + #define WARN_AND_RETURN_IF_FAILED(msg, ret) \ if (FAILED(hr)) { \ qCWarning(QT_BT_WINRT) << msg; \ @@ -150,6 +157,7 @@ Q_SIGNALS: void deviceFound(const QBluetoothDeviceInfo &info); void deviceDataChanged(const QBluetoothAddress &address, QBluetoothDeviceInfo::Fields, qint16 rssi, ManufacturerData manufacturerData); + void errorOccured(QBluetoothDeviceDiscoveryAgent::Error error); void scanFinished(); public: @@ -185,9 +193,13 @@ QWinRTBluetoothDeviceDiscoveryWorker::QWinRTBluetoothDeviceDiscoveryWorker(QBlue CoInitialize(NULL); HRESULT hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Bluetooth_BluetoothDevice).Get(), &m_deviceStatics); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth device factory", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return) hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Bluetooth_BluetoothLEDevice).Get(), &m_leDeviceStatics); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth le device factory", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return) } QWinRTBluetoothDeviceDiscoveryWorker::~QWinRTBluetoothDeviceDiscoveryWorker() @@ -213,10 +225,14 @@ void QWinRTBluetoothDeviceDiscoveryWorker::stopLEWatcher() { if (m_leWatcher) { HRESULT hr = m_leWatcher->Stop(); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not stop le watcher", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return) if (m_leDeviceAddedToken.value) { hr = m_leWatcher->remove_Received(m_leDeviceAddedToken); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could remove le watcher token", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return) } } } @@ -226,14 +242,18 @@ void QWinRTBluetoothDeviceDiscoveryWorker::startDeviceDiscovery(QBluetoothDevice HString deviceSelector; ComPtr<IDeviceInformationStatics> deviceInformationStatics; HRESULT hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Enumeration_DeviceInformation).Get(), &deviceInformationStatics); - WARN_AND_RETURN_IF_FAILED("Could not obtain device information statics", return); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device information statics", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); if (mode == QBluetoothDeviceDiscoveryAgent::LowEnergyMethod) m_leDeviceStatics->GetDeviceSelector(deviceSelector.GetAddressOf()); else m_deviceStatics->GetDeviceSelector(deviceSelector.GetAddressOf()); ComPtr<IAsyncOperation<DeviceInformationCollection *>> op; hr = deviceInformationStatics->FindAllAsyncAqsFilter(deviceSelector.Get(), &op); - WARN_AND_RETURN_IF_FAILED("Could not start bluetooth device discovery operation", return); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not start bluetooth device discovery operation", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); QPointer<QWinRTBluetoothDeviceDiscoveryWorker> thisPointer(this); hr = op->put_Completed( Callback<IAsyncOperationCompletedHandler<DeviceInformationCollection *>>([thisPointer, mode](IAsyncOperation<DeviceInformationCollection *> *op, AsyncStatus status) { @@ -241,7 +261,9 @@ void QWinRTBluetoothDeviceDiscoveryWorker::startDeviceDiscovery(QBluetoothDevice thisPointer->onDeviceDiscoveryFinished(op, mode); return S_OK; }).Get()); - WARN_AND_RETURN_IF_FAILED("Could not add callback to bluetooth device discovery operation", return); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not add device discovery callback", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); } void QWinRTBluetoothDeviceDiscoveryWorker::onDeviceDiscoveryFinished(IAsyncOperation<DeviceInformationCollection *> *op, QBluetoothDeviceDiscoveryAgent::DiscoveryMethod mode) @@ -251,10 +273,14 @@ void QWinRTBluetoothDeviceDiscoveryWorker::onDeviceDiscoveryFinished(IAsyncOpera ComPtr<IVectorView<DeviceInformation *>> devices; HRESULT hr; hr = op->GetResults(&devices); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain discovery result", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); quint32 deviceCount; hr = devices->get_Size(&deviceCount); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain discovery result size", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); // For classic discovery only paired devices will be found. If we only do classic disovery and // no device is found, the scan is finished. @@ -273,7 +299,9 @@ void QWinRTBluetoothDeviceDiscoveryWorker::gatherDeviceInformation(IDeviceInform HString deviceId; HRESULT hr; hr = deviceInfo->get_Id(deviceId.GetAddressOf()); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device ID", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); if (mode == QBluetoothDeviceDiscoveryAgent::LowEnergyMethod) leBluetoothInfoFromDeviceIdAsync(deviceId.Get()); else @@ -286,7 +314,9 @@ void QWinRTBluetoothDeviceDiscoveryWorker::gatherMultipleDeviceInformation(quint ComPtr<IDeviceInformation> device; HRESULT hr; hr = devices->GetAt(i, &device); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); gatherDeviceInformation(device.Get(), mode); } } @@ -294,24 +324,34 @@ void QWinRTBluetoothDeviceDiscoveryWorker::gatherMultipleDeviceInformation(quint void QWinRTBluetoothDeviceDiscoveryWorker::setupLEDeviceWatcher() { HRESULT hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Devices_Bluetooth_Advertisement_BluetoothLEAdvertisementWatcher).Get(), &m_leWatcher); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not create advertisment watcher", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); #if QT_CONFIG(winrt_btle_no_pairing) if (supportsNewLEApi()) { hr = m_leWatcher->put_ScanningMode(BluetoothLEScanningMode_Active); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not set scanning mode", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); } #endif // winrt_btle_no_pairing hr = m_leWatcher->add_Received(Callback<ITypedEventHandler<BluetoothLEAdvertisementWatcher *, BluetoothLEAdvertisementReceivedEventArgs *>>([this](IBluetoothLEAdvertisementWatcher *, IBluetoothLEAdvertisementReceivedEventArgs *args) { quint64 address; HRESULT hr; hr = args->get_BluetoothAddress(&address); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth address", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); qint16 rssi; hr = args->get_RawSignalStrengthInDBm(&rssi); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain signal strength", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); ComPtr<IBluetoothLEAdvertisement> ad; hr = args->get_Advertisement(&ad); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could get advertisement", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); const ManufacturerData manufacturerData = extractManufacturerData(ad); QBluetoothDeviceInfo::Fields changedFields = QBluetoothDeviceInfo::Field::None; if (!m_foundLEManufacturerData.contains(address)) { @@ -325,15 +365,21 @@ void QWinRTBluetoothDeviceDiscoveryWorker::setupLEDeviceWatcher() if (supportsNewLEApi()) { ComPtr<IVector<GUID>> guids; hr = ad->get_ServiceUuids(&guids); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain service uuid list", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); quint32 size; hr = guids->get_Size(&size); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain service uuid list size", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); QList<QBluetoothUuid> serviceUuids; for (quint32 i = 0; i < size; ++i) { GUID guid; hr = guids->GetAt(i, &guid); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain uuid", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); QBluetoothUuid uuid(guid); serviceUuids.append(uuid); } @@ -396,9 +442,13 @@ void QWinRTBluetoothDeviceDiscoveryWorker::setupLEDeviceWatcher() leBluetoothInfoFromAddressAsync(address); return S_OK; }).Get(), &m_leDeviceAddedToken); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not add device callback", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); hr = m_leWatcher->Start(); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not start device watcher", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return); } void QWinRTBluetoothDeviceDiscoveryWorker::finishDiscovery() @@ -415,6 +465,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)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); --m_pendingPairedDevices; if (!m_pendingPairedDevices && !(requestedModes & QBluetoothDeviceDiscoveryAgent::LowEnergyMethod)) @@ -434,6 +485,7 @@ void QWinRTBluetoothDeviceDiscoveryWorker::classicBluetoothInfoFromDeviceIdAsync return S_OK; }).Get()); if (FAILED(hr)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); --m_pendingPairedDevices; if (!m_pendingPairedDevices && !(requestedModes & QBluetoothDeviceDiscoveryAgent::LowEnergyMethod)) @@ -450,6 +502,7 @@ void QWinRTBluetoothDeviceDiscoveryWorker::leBluetoothInfoFromDeviceIdAsync(HSTR // on Windows 10 FromIdAsync might ask for device permission. We cannot wait here but have to handle that asynchronously HRESULT hr = m_leDeviceStatics->FromIdAsync(deviceId, &deviceFromIdOperation); if (FAILED(hr)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); --m_pendingPairedDevices; qCWarning(QT_BT_WINRT) << "Could not obtain bluetooth device from id"; return; @@ -466,6 +519,7 @@ void QWinRTBluetoothDeviceDiscoveryWorker::leBluetoothInfoFromDeviceIdAsync(HSTR return S_OK; }).Get()); if (FAILED(hr)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); --m_pendingPairedDevices; qCWarning(QT_BT_WINRT) << "Could not register device found callback"; return; @@ -480,6 +534,7 @@ void QWinRTBluetoothDeviceDiscoveryWorker::leBluetoothInfoFromAddressAsync(quint // here but have to handle that asynchronously HRESULT hr = m_leDeviceStatics->FromBluetoothAddressAsync(address, &deviceFromAddressOperation); if (FAILED(hr)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); qCWarning(QT_BT_WINRT) << "Could not obtain bluetooth device from address"; return; } @@ -492,6 +547,7 @@ void QWinRTBluetoothDeviceDiscoveryWorker::leBluetoothInfoFromAddressAsync(quint return S_OK; }).Get()); if (FAILED(hr)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); qCWarning(QT_BT_WINRT) << "Could not register device found callback"; return; } @@ -505,7 +561,9 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onPairedClassicBluetoothDeviceFoun ComPtr<IBluetoothDevice> device; HRESULT hr = op->GetResults(&device); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth device", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); if (!device) return S_OK; @@ -515,35 +573,53 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onPairedClassicBluetoothDeviceFoun ComPtr<IBluetoothClassOfDevice> classOfDevice; UINT32 classOfDeviceInt; hr = device->get_BluetoothAddress(&address); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth address", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); hr = device->get_Name(name.GetAddressOf()); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device name", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); const QString btName = QString::fromWCharArray(WindowsGetStringRawBuffer(name.Get(), nullptr)); hr = device->get_ClassOfDevice(&classOfDevice); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device class", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); hr = classOfDevice->get_RawValue(&classOfDeviceInt); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain raw value of device class", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); IVectorView <Rfcomm::RfcommDeviceService *> *deviceServices; hr = device->get_RfcommServices(&deviceServices); if (hr == E_ACCESSDENIED) { qCWarning(QT_BT_WINRT) << "Could not obtain device services. Please check you have " "permission to access the device."; } else { - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device services", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); uint serviceCount; hr = deviceServices->get_Size(&serviceCount); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain service list size", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); QList<QBluetoothUuid> uuids; for (uint i = 0; i < serviceCount; ++i) { ComPtr<Rfcomm::IRfcommDeviceService> service; hr = deviceServices->GetAt(i, &service); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device service", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); ComPtr<Rfcomm::IRfcommServiceId> id; hr = service->get_ServiceId(&id); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain service id", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); GUID uuid; hr = id->get_Uuid(&uuid); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain uuid", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); uuids.append(QBluetoothUuid(uuid)); } @@ -572,7 +648,9 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onPairedBluetoothLEDeviceFoundAsyn ComPtr<IBluetoothLEDevice> device; HRESULT hr; hr = op->GetResults(&device); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth le device", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); #if QT_CONFIG(winrt_btle_no_pairing) if (supportsNewLEApi()) return onBluetoothLEDeviceFound(device); @@ -589,7 +667,9 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onBluetoothLEDeviceFoundAsync(IAsy ComPtr<IBluetoothLEDevice> device; HRESULT hr; hr = op->GetResults(&device); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth le device", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); #if QT_CONFIG(winrt_btle_no_pairing) if (supportsNewLEApi()) return onBluetoothLEDeviceFound(device); @@ -608,31 +688,45 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onBluetoothLEDeviceFound(ComPtr<IB if (pairingCheck == CheckForPairing) { ComPtr<IBluetoothLEDevice2> device2; HRESULT hr = device.As(&device2); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not cast device to Device2", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); ComPtr<IDeviceInformation> deviceInfo; hr = device2->get_DeviceInformation(&deviceInfo); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device info", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); if (!deviceInfo) { qCDebug(QT_BT_WINRT) << "onBluetoothLEDeviceFound: Could not obtain device information"; return S_OK; } ComPtr<IDeviceInformation2> deviceInfo2; hr = deviceInfo.As(&deviceInfo2); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not cast device to DeviceInfo2", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); ComPtr<IDeviceInformationPairing> pairing; hr = deviceInfo2->get_Pairing(&pairing); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain pairing information", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); boolean isPaired; hr = pairing->get_IsPaired(&isPaired); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain pairing status", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); // We need a paired device in order to be able to obtain its information if (!isPaired) { ComPtr<IAsyncOperation<DevicePairingResult *>> pairingOp; QPointer<QWinRTBluetoothDeviceDiscoveryWorker> tPointer(this); hr = pairing.Get()->PairAsync(&pairingOp); - Q_ASSERT_SUCCEEDED(hr); - pairingOp->put_Completed( - Callback<IAsyncOperationCompletedHandler<DevicePairingResult *>>([device, tPointer](IAsyncOperation<DevicePairingResult *> *op, AsyncStatus status) { + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not initiate pairing", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); + hr = pairingOp->put_Completed( + Callback<IAsyncOperationCompletedHandler<DevicePairingResult *>> + ([device, tPointer] + (IAsyncOperation<DevicePairingResult *> *op, AsyncStatus status) { if (!tPointer) return S_OK; @@ -642,19 +736,28 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onBluetoothLEDeviceFound(ComPtr<IB } ComPtr<IDevicePairingResult> result; - op->GetResults(&result); + HRESULT hr; + hr = op->GetResults(&result); + if (FAILED(hr)) { + emit tPointer->errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); + qCWarning(QT_BT_WINRT) << "Could not obtain pairing result"; + return S_OK; + } DevicePairingResultStatus pairingStatus; - result.Get()->get_Status(&pairingStatus); - - if (pairingStatus != DevicePairingResultStatus_Paired) { - qCDebug(QT_BT_WINRT) << "Could not pair device"; + hr = result.Get()->get_Status(&pairingStatus); + if (FAILED(hr) || pairingStatus != DevicePairingResultStatus_Paired) { + emit tPointer->errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); + qCWarning(QT_BT_WINRT) << "Device pairing failed"; return S_OK; } tPointer->onBluetoothLEDeviceFound(device, OmitPairingCheck); return S_OK; }).Get()); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain register pairing callback", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); return S_OK; } } @@ -662,25 +765,37 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onBluetoothLEDeviceFound(ComPtr<IB UINT64 address; HString name; HRESULT hr = device->get_BluetoothAddress(&address); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth address", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); hr = device->get_Name(name.GetAddressOf()); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device name", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); const QString btName = QString::fromWCharArray(WindowsGetStringRawBuffer(name.Get(), nullptr)); IVectorView <GenericAttributeProfile::GattDeviceService *> *deviceServices; hr = device->get_GattServices(&deviceServices); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain gatt service list", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); uint serviceCount; hr = deviceServices->get_Size(&serviceCount); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain gatt service list size", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); QList<QBluetoothUuid> uuids; for (uint i = 0; i < serviceCount; ++i) { ComPtr<GenericAttributeProfile::IGattDeviceService> service; hr = deviceServices->GetAt(i, &service); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain gatt service", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); ComPtr<Rfcomm::IRfcommServiceId> id; GUID uuid; hr = service->get_Uuid(&uuid); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain gatt service uuid", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); uuids.append(QBluetoothUuid(uuid)); } const qint16 rssi = m_foundLEDevices.value(address); @@ -714,30 +829,43 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onBluetoothLEDeviceFound(ComPtr<IB UINT64 address; HString name; HRESULT hr = device->get_BluetoothAddress(&address); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain bluetooth address", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); hr = device->get_Name(name.GetAddressOf()); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device name", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); const QString btName = QString::fromWCharArray(WindowsGetStringRawBuffer(name.Get(), nullptr)); ComPtr<IBluetoothLEDevice2> device2; - hr = device.As(&device2); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not cast device", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); ComPtr<IDeviceInformation> deviceInfo; hr = device2->get_DeviceInformation(&deviceInfo); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain device info", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); if (!deviceInfo) { qCDebug(QT_BT_WINRT) << "onBluetoothLEDeviceFound: Could not obtain device information"; return S_OK; } ComPtr<IDeviceInformation2> deviceInfo2; hr = deviceInfo.As(&deviceInfo2); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain cast device info", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); ComPtr<IDeviceInformationPairing> pairing; hr = deviceInfo2->get_Pairing(&pairing); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain pairing information", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); boolean isPaired; hr = pairing->get_IsPaired(&isPaired); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain pairing status", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); QList<QBluetoothUuid> uuids; const LEAdvertisingInfo adInfo = m_foundLEDevicesMap.value(address); @@ -748,17 +876,25 @@ HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onBluetoothLEDeviceFound(ComPtr<IB } else { IVectorView <GenericAttributeProfile::GattDeviceService *> *deviceServices; hr = device->get_GattServices(&deviceServices); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain gatt service list", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); uint serviceCount; hr = deviceServices->get_Size(&serviceCount); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain gatt service list size", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); for (uint i = 0; i < serviceCount; ++i) { ComPtr<GenericAttributeProfile::IGattDeviceService> service; hr = deviceServices->GetAt(i, &service); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain gatt service", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); GUID uuid; hr = service->get_Uuid(&uuid); - Q_ASSERT_SUCCEEDED(hr); + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain uuid", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); uuids.append(QBluetoothUuid(uuid)); } } @@ -816,6 +952,8 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent this, &QBluetoothDeviceDiscoveryAgentPrivate::registerDevice); connect(worker, &QWinRTBluetoothDeviceDiscoveryWorker::deviceDataChanged, this, &QBluetoothDeviceDiscoveryAgentPrivate::updateDeviceData); + connect(worker, &QWinRTBluetoothDeviceDiscoveryWorker::errorOccured, + this, &QBluetoothDeviceDiscoveryAgentPrivate::onErrorOccured); connect(worker, &QWinRTBluetoothDeviceDiscoveryWorker::scanFinished, this, &QBluetoothDeviceDiscoveryAgentPrivate::onScanFinished); worker->start(); @@ -896,6 +1034,13 @@ void QBluetoothDeviceDiscoveryAgentPrivate::updateDeviceData(const QBluetoothAdd } } +void QBluetoothDeviceDiscoveryAgentPrivate::onErrorOccured(QBluetoothDeviceDiscoveryAgent::Error e) +{ + Q_Q(QBluetoothDeviceDiscoveryAgent); + lastError = e; + emit q->errorOccurred(e); +} + void QBluetoothDeviceDiscoveryAgentPrivate::onScanFinished() { Q_Q(QBluetoothDeviceDiscoveryAgent); |