diff options
author | Oliver Wolff <oliver.wolff@qt.io> | 2021-04-07 13:28:41 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-09-28 09:23:24 +0000 |
commit | f2d15c8f8104b05b6a53712b2ff7a5fcaef033d1 (patch) | |
tree | 554b85795610d7f6b045a87be70539978f53f57e /src | |
parent | e3d2f7e22dd92b57482a8b12b4de77ccbeedeef7 (diff) |
Port QBluetoothLocalDevice_winrt to c++/winrt
Change-Id: I05062d40a6ae8b09f0b267dff2203f36c885d23e
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
(cherry picked from commit d6dc79a13c9066b4c5cee217524da02f28dbf359)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/bluetooth/qbluetoothlocaldevice_p.h | 17 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothlocaldevice_winrt.cpp | 99 |
2 files changed, 32 insertions, 84 deletions
diff --git a/src/bluetooth/qbluetoothlocaldevice_p.h b/src/bluetooth/qbluetoothlocaldevice_p.h index a03dfaf0..6415f920 100644 --- a/src/bluetooth/qbluetoothlocaldevice_p.h +++ b/src/bluetooth/qbluetoothlocaldevice_p.h @@ -80,21 +80,6 @@ QT_END_NAMESPACE #include <QtCore/QPair> #endif -#ifdef QT_WINRT_BLUETOOTH -#include <wrl.h> - -namespace ABI { - namespace Windows { - namespace Devices { - namespace Bluetooth { - struct IBluetoothDeviceStatics; - struct IBluetoothLEDeviceStatics; - } - } - } -} -#endif - QT_BEGIN_NAMESPACE extern void registerQBluetoothLocalDeviceMetaType(); @@ -207,8 +192,6 @@ public: private: QBluetoothLocalDevice *q_ptr; - Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothDeviceStatics> mStatics; - Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothLEDeviceStatics> mLEStatics; }; #elif !defined(QT_OSX_BLUETOOTH) // dummy backend class QBluetoothLocalDevicePrivate : public QObject diff --git a/src/bluetooth/qbluetoothlocaldevice_winrt.cpp b/src/bluetooth/qbluetoothlocaldevice_winrt.cpp index 4ad2adfb..59aa1046 100644 --- a/src/bluetooth/qbluetoothlocaldevice_winrt.cpp +++ b/src/bluetooth/qbluetoothlocaldevice_winrt.cpp @@ -37,60 +37,29 @@ ** ****************************************************************************/ +#include <qbluetoothutils_winrt_p.h> + #include "qbluetoothlocaldevice.h" #include "qbluetoothaddress.h" #include "qbluetoothlocaldevice_p.h" #include "qbluetoothutils_winrt_p.h" -#include <robuffer.h> -#include <windows.devices.bluetooth.h> +#include <winrt/Windows.Foundation.h> +#include <winrt/Windows.Devices.Enumeration.h> +#include <winrt/Windows.Devices.Bluetooth.h> #include <wrl.h> #include <QtCore/private/qfunctions_winrt_p.h> -using namespace ABI::Windows::Foundation; -using namespace ABI::Windows::Devices::Bluetooth; -using namespace ABI::Windows::Devices::Enumeration; +using namespace winrt::Windows::Foundation; +using namespace winrt::Windows::Devices::Enumeration; +using namespace winrt::Windows::Devices::Bluetooth; using namespace Microsoft::WRL; using namespace Microsoft::WRL::Wrappers; QT_BEGIN_NAMESPACE -template <class DeviceStatics, class OpResult, class Device, class Device2> -ComPtr<IDeviceInformationPairing> getPairingInfo(ComPtr<DeviceStatics> deviceStatics, - const QBluetoothAddress &address) -{ - ComPtr<IAsyncOperation<OpResult *>> op; - if (!deviceStatics) - return nullptr; - HRESULT hr = deviceStatics->FromBluetoothAddressAsync(address.toUInt64(), &op); - RETURN_IF_FAILED("Could not obtain device from address", return nullptr); - ComPtr<Device> device; - hr = QWinRTFunctions::await(op, device.GetAddressOf(), - QWinRTFunctions::ProcessMainThreadEvents, 5000); - if (FAILED(hr) || !device) { - qErrnoWarning("Could not obtain device from address"); - return nullptr; - } - ComPtr<Device2> device2; - hr = device.As(&device2); - RETURN_IF_FAILED("Could not cast device", return nullptr); - ComPtr<IDeviceInformation> deviceInfo; - hr = device2->get_DeviceInformation(&deviceInfo); - if (FAILED(hr) || !deviceInfo) { - qErrnoWarning("Could not obtain device information"); - return nullptr; - } - ComPtr<IDeviceInformation2> deviceInfo2; - hr = deviceInfo.As(&deviceInfo2); - RETURN_IF_FAILED("Could not cast device information", return nullptr); - ComPtr<IDeviceInformationPairing> pairingInfo; - hr = deviceInfo2->get_Pairing(&pairingInfo); - RETURN_IF_FAILED("Could not obtain pairing information", return nullptr); - return pairingInfo; -} - QBluetoothLocalDevice::QBluetoothLocalDevice(QObject *parent) : QObject(parent), d_ptr(new QBluetoothLocalDevicePrivate(this, QBluetoothAddress())) @@ -108,15 +77,13 @@ QBluetoothLocalDevice::QBluetoothLocalDevice(const QBluetoothAddress &address, Q QBluetoothLocalDevicePrivate::QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, QBluetoothAddress) : q_ptr(q) { - GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Bluetooth_BluetoothLEDevice).Get(), &mLEStatics); - GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Bluetooth_BluetoothDevice).Get(), &mStatics); } QBluetoothLocalDevicePrivate::~QBluetoothLocalDevicePrivate() = default; bool QBluetoothLocalDevicePrivate::isValid() const { - return (mStatics != nullptr && mLEStatics != nullptr); + return true; } void QBluetoothLocalDevice::requestPairing(const QBluetoothAddress &address, Pairing pairing) @@ -132,31 +99,29 @@ QBluetoothLocalDevice::Pairing QBluetoothLocalDevice::pairingStatus( const QBluetoothAddress &address) const { if (!isValid() || address.isNull()) - return QBluetoothLocalDevice::Unpaired; - - ComPtr<IDeviceInformationPairing> pairingInfo = getPairingInfo<IBluetoothLEDeviceStatics, - BluetoothLEDevice, IBluetoothLEDevice, IBluetoothLEDevice2>(d_ptr->mLEStatics, address); - if (!pairingInfo) - pairingInfo = getPairingInfo<IBluetoothDeviceStatics, BluetoothDevice, - IBluetoothDevice, IBluetoothDevice2>(d_ptr->mStatics, address); - if (!pairingInfo) - return QBluetoothLocalDevice::Unpaired; - boolean isPaired; - HRESULT hr = pairingInfo->get_IsPaired(&isPaired); - RETURN_IF_FAILED("Could not obtain device pairing", return QBluetoothLocalDevice::Unpaired); - if (!isPaired) - return QBluetoothLocalDevice::Unpaired; - - ComPtr<IDeviceInformationPairing2> pairingInfo2; - hr = pairingInfo.As(&pairingInfo2); - RETURN_IF_FAILED("Could not cast pairing info", return QBluetoothLocalDevice::Paired); - DevicePairingProtectionLevel protection = DevicePairingProtectionLevel_None; - hr = pairingInfo2->get_ProtectionLevel(&protection); - RETURN_IF_FAILED("Could not obtain pairing protection level", return QBluetoothLocalDevice::Paired); - if (protection == DevicePairingProtectionLevel_Encryption - || protection == DevicePairingProtectionLevel_EncryptionAndAuthentication) - return QBluetoothLocalDevice::AuthorizedPaired; - return QBluetoothLocalDevice::Paired; + return Unpaired; + + auto qtPairingFromPairingInfo = [](DeviceInformationPairing pairingInfo) { + if (!pairingInfo.IsPaired()) + return Unpaired; + + const DevicePairingProtectionLevel protection = pairingInfo.ProtectionLevel(); + if (protection == DevicePairingProtectionLevel::Encryption + || protection == DevicePairingProtectionLevel::EncryptionAndAuthentication) + return AuthorizedPaired; + return Paired; + }; + + const quint64 addr64 = address.toUInt64(); + BluetoothLEDevice leDevice { BluetoothLEDevice::FromBluetoothAddressAsync(addr64).get() }; + if (leDevice.BluetoothAddress() != 0) + return qtPairingFromPairingInfo(leDevice.DeviceInformation().Pairing()); + + BluetoothDevice device { BluetoothDevice::FromBluetoothAddressAsync(addr64).get() }; + if (device.BluetoothAddress() != 0) + return qtPairingFromPairingInfo(device.DeviceInformation().Pairing()); + + return Unpaired; } void QBluetoothLocalDevice::setHostMode(QBluetoothLocalDevice::HostMode mode) |