diff options
Diffstat (limited to 'src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp | 286 |
1 files changed, 220 insertions, 66 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp index 2562395a..90ebbdd1 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_winrt.cpp @@ -77,6 +77,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; \ @@ -156,6 +163,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: @@ -193,9 +201,13 @@ QWinRTBluetoothDeviceDiscoveryWorker::QWinRTBluetoothDeviceDiscoveryWorker(QBlue CoInitialize(NULL); #endif 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() @@ -226,10 +238,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) } } } @@ -239,14 +255,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) { @@ -254,7 +274,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) @@ -264,10 +286,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. @@ -286,7 +312,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 @@ -299,7 +327,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); } } @@ -307,24 +337,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)) { @@ -338,15 +378,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); QVector<QBluetoothUuid> serviceUuids; + EMIT_WORKER_ERROR_AND_RETURN_IF_FAILED("Could not obtain service uuid list size", + QBluetoothDeviceDiscoveryAgent::Error::UnknownError, + return S_OK); 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); } @@ -409,9 +455,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() @@ -458,7 +508,12 @@ void QWinRTBluetoothDeviceDiscoveryWorker::classicBluetoothInfoFromDeviceIdAsync } return S_OK; }); - Q_ASSERT_SUCCEEDED(hr); + if (FAILED(hr)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); + --m_pendingPairedDevices; + qCWarning(QT_BT_WINRT) << "Could not obtain bluetooth device from id"; + return; + } } // "deviceFound" will be emitted at the end of the deviceFromIdOperation callback @@ -492,7 +547,12 @@ void QWinRTBluetoothDeviceDiscoveryWorker::leBluetoothInfoFromDeviceIdAsync(HSTR } return S_OK; }); - Q_ASSERT_SUCCEEDED(hr); + if (FAILED(hr)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); + --m_pendingPairedDevices; + qCWarning(QT_BT_WINRT) << "Could not obtain bluetooth device from id"; + return; + } } // "deviceFound" will be emitted at the end of the deviceFromAdressOperation callback @@ -522,7 +582,11 @@ void QWinRTBluetoothDeviceDiscoveryWorker::leBluetoothInfoFromAddressAsync(quint } return S_OK; }); - Q_ASSERT_SUCCEEDED(hr); + if (FAILED(hr)) { + emit errorOccured(QBluetoothDeviceDiscoveryAgent::UnknownError); + qCWarning(QT_BT_WINRT) << "Could not obtain bluetooth device from id"; + return; + } } HRESULT QWinRTBluetoothDeviceDiscoveryWorker::onPairedClassicBluetoothDeviceFoundAsync(IAsyncOperation<BluetoothDevice *> *op, AsyncStatus status) @@ -533,7 +597,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; @@ -543,35 +609,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); QVector<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)); } @@ -600,7 +684,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); @@ -617,7 +703,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); @@ -636,31 +724,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; @@ -670,19 +772,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; } } @@ -690,25 +801,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); QVector<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); @@ -742,30 +865,44 @@ 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); QVector<QBluetoothUuid> uuids; const LEAdvertisingInfo adInfo = m_foundLEDevicesMap.value(address); @@ -776,17 +913,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)); } } @@ -849,6 +994,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(); @@ -925,6 +1072,13 @@ void QBluetoothDeviceDiscoveryAgentPrivate::updateDeviceData(const QBluetoothAdd } } +void QBluetoothDeviceDiscoveryAgentPrivate::onErrorOccured(QBluetoothDeviceDiscoveryAgent::Error e) +{ + Q_Q(QBluetoothDeviceDiscoveryAgent); + lastError = e; + emit q->error(e); +} + void QBluetoothDeviceDiscoveryAgentPrivate::onScanFinished() { Q_Q(QBluetoothDeviceDiscoveryAgent); |