summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qbluetoothlocaldevice_winrt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bluetooth/qbluetoothlocaldevice_winrt.cpp')
-rw-r--r--src/bluetooth/qbluetoothlocaldevice_winrt.cpp90
1 files changed, 28 insertions, 62 deletions
diff --git a/src/bluetooth/qbluetoothlocaldevice_winrt.cpp b/src/bluetooth/qbluetoothlocaldevice_winrt.cpp
index d97ba507..06fb5340 100644
--- a/src/bluetooth/qbluetoothlocaldevice_winrt.cpp
+++ b/src/bluetooth/qbluetoothlocaldevice_winrt.cpp
@@ -1,11 +1,12 @@
-// Copyright (C) 2019 The Qt Company Ltd.
+// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
-#include <qbluetoothutils_winrt_p.h>
+#include <private/qbluetoothutils_winrt_p.h>
#include "qbluetoothlocaldevice.h"
#include "qbluetoothaddress.h"
+#include "qbluetoothdevicewatcher_winrt_p.h"
#include "qbluetoothlocaldevice_p.h"
#include "qbluetoothutils_winrt_p.h"
@@ -14,20 +15,18 @@
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Radios.h>
-#include <wrl.h>
-#include <QtCore/private/qfunctions_winrt_p.h>
+#include <QtCore/QCoreApplication>
+#include <QtCore/QElapsedTimer>
+#include <QtCore/QLoggingCategory>
+#include <QtCore/QMutex>
#include <QtCore/QPointer>
#include <QtCore/QTimer>
-#include <QtCore/QMutex>
-#include <QtCore/QLoggingCategory>
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::Devices::Enumeration;
using namespace winrt::Windows::Devices::Bluetooth;
-using namespace Microsoft::WRL;
-using namespace Microsoft::WRL::Wrappers;
using namespace winrt::Windows::Devices::Radios;
QT_BEGIN_NAMESPACE
@@ -75,66 +74,35 @@ static RadioState windowsStateFromMode(QBluetoothLocalDevice::HostMode mode)
class AdapterManager;
-class WatcherWrapper : public QObject, public std::enable_shared_from_this<WatcherWrapper>
+class WatcherWrapper
{
- Q_OBJECT
public:
- WatcherWrapper(winrt::hstring selector) : mWatcher(DeviceInformation::CreateWatcher(selector))
+ // we do not really care about unique ids here
+ WatcherWrapper(winrt::hstring selector) :
+ mWatcher(std::make_shared<QBluetoothDeviceWatcherWinRT>(0, selector))
{
}
- ~WatcherWrapper()
- {
- if (mWatcher && mInitialized) {
- mWatcher.Stop();
- unsubscribeFromEvents();
- }
- }
template<typename AddedSlot, typename RemovedSlot>
- bool init(AdapterManager *manager, AddedSlot onAdded, RemovedSlot onRemoved)
+ void init(AdapterManager *manager, AddedSlot onAdded, RemovedSlot onRemoved)
{
if (!mWatcher) {
- qWarning() << "Windows failed to create an instance of DeviceWatcher. "
- << "QBluetoothLocalDevice might fail to provide some information.";
- return false;
+ qWarning("QBluetoothLocalDevice: failed to create device watcher!");
+ return;
}
- subscribeToEvents();
- connect(this, &WatcherWrapper::deviceAdded, manager, onAdded, Qt::QueuedConnection);
- connect(this, &WatcherWrapper::deviceRemoved, manager, onRemoved, Qt::QueuedConnection);
- mInitialized = true;
- mWatcher.Start();
- return true;
- }
-
-signals:
- void deviceAdded(winrt::hstring id);
- void deviceRemoved(winrt::hstring id);
+ QObject::connect(mWatcher.get(), &QBluetoothDeviceWatcherWinRT::deviceAdded,
+ manager, onAdded, Qt::QueuedConnection);
+ QObject::connect(mWatcher.get(), &QBluetoothDeviceWatcherWinRT::deviceRemoved,
+ manager, onRemoved, Qt::QueuedConnection);
-private:
- void subscribeToEvents()
- {
- // The callbacks are triggered from separate threads. So we capture
- // thisPtr to make sure that the object is valid.
- auto thisPtr = shared_from_this();
- mAddedToken = mWatcher.Added([thisPtr](DeviceWatcher, const DeviceInformation &info) {
- emit thisPtr->deviceAdded(info.Id());
- });
- mRemovedToken =
- mWatcher.Removed([thisPtr](DeviceWatcher, const DeviceInformationUpdate &upd) {
- emit thisPtr->deviceRemoved(upd.Id());
- });
- }
- void unsubscribeFromEvents()
- {
- mWatcher.Added(mAddedToken);
- mWatcher.Removed(mRemovedToken);
+ // This will also print a warning on failure.
+ if (mWatcher->init())
+ mWatcher->start();
}
- bool mInitialized = false;
- DeviceWatcher mWatcher = nullptr;
- winrt::event_token mAddedToken;
- winrt::event_token mRemovedToken;
+private:
+ std::shared_ptr<QBluetoothDeviceWatcherWinRT> mWatcher = nullptr;
};
/*
@@ -193,9 +161,9 @@ private:
Q_SLOT void onDeviceAdded(winrt::hstring id);
Q_SLOT void onDeviceRemoved(winrt::hstring id);
- std::shared_ptr<WatcherWrapper> mAdapterWatcher = nullptr;
- std::shared_ptr<WatcherWrapper> mLeDevicesWatcher = nullptr;
- std::shared_ptr<WatcherWrapper> mClassicDevicesWatcher = nullptr;
+ std::unique_ptr<WatcherWrapper> mAdapterWatcher = nullptr;
+ std::unique_ptr<WatcherWrapper> mLeDevicesWatcher = nullptr;
+ std::unique_ptr<WatcherWrapper> mClassicDevicesWatcher = nullptr;
QMutex mMutex;
// Key for this map is BluetoothAdapter Id, *not* Radio Id.
QMap<winrt::hstring, RadioInfo> mRadios;
@@ -204,10 +172,8 @@ private:
AdapterManager::AdapterManager() : QObject()
{
- qRegisterMetaType<winrt::hstring>("winrt::hstring");
-
const auto adapterSelector = BluetoothAdapter::GetDeviceSelector();
- mAdapterWatcher = std::make_shared<WatcherWrapper>(adapterSelector);
+ mAdapterWatcher = std::make_unique<WatcherWrapper>(adapterSelector);
mAdapterWatcher->init(this, &AdapterManager::onAdapterAdded, &AdapterManager::onAdapterRemoved);
// Once created, device watchers will also populate the initial list of
@@ -215,7 +181,7 @@ AdapterManager::AdapterManager() : QObject()
const auto leSelector = BluetoothLEDevice::GetDeviceSelectorFromConnectionStatus(
BluetoothConnectionStatus::Connected);
- mLeDevicesWatcher = std::make_shared<WatcherWrapper>(leSelector);
+ mLeDevicesWatcher = std::make_unique<WatcherWrapper>(leSelector);
mLeDevicesWatcher->init(this, &AdapterManager::onDeviceAdded, &AdapterManager::onDeviceRemoved);
const auto classicSelector = BluetoothDevice::GetDeviceSelectorFromConnectionStatus(