Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbluetoothdevicewatcher_winrt.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <winrt/Windows.Foundation.Collections.h>
7
8using namespace winrt::Windows::Foundation;
9using namespace winrt::Windows::Devices::Enumeration;
10
12
14 : m_id(id),
15 m_watcher(DeviceInformation::CreateWatcher(selector))
16{
17 qRegisterMetaType<winrt::hstring>("winrt::hstring");
18}
19
21 winrt::Windows::Devices::Enumeration::DeviceInformationKind kind)
22 : m_id(id)
23{
24 qRegisterMetaType<winrt::hstring>("winrt::hstring");
25 const winrt::param::iterable<winrt::hstring> extra {};
26 m_watcher = DeviceInformation::CreateWatcher(selector, extra, kind);
27}
28
33
35{
36 if (!m_watcher) {
37 qWarning("Windows failed to create an instance of DeviceWatcher. "
38 "Detection of Bluetooth devices might not work correctly.");
39 return false;
40 }
41 return true;
42}
43
45{
46 if (m_watcher) {
47 subscribeToEvents();
48 m_watcher.Start();
49 }
50}
51
53{
54 if (m_watcher && canStop()) {
55 unsubscribeFromEvents();
56 m_watcher.Stop();
57 }
58}
59
60void QBluetoothDeviceWatcherWinRT::subscribeToEvents()
61{
62 Q_ASSERT(m_watcher.Status() == DeviceWatcherStatus::Created);
63 // The callbacks are triggered from separate threads. So we capture
64 // thisPtr to make sure that the object is valid.
65 auto thisPtr = shared_from_this();
66 m_addedToken = m_watcher.Added([thisPtr](DeviceWatcher, const DeviceInformation &info) {
67 emit thisPtr->deviceAdded(info.Id(), thisPtr->m_id);
68 });
69 m_removedToken =
70 m_watcher.Removed([thisPtr](DeviceWatcher, const DeviceInformationUpdate &upd) {
71 emit thisPtr->deviceRemoved(upd.Id(), thisPtr->m_id);
72 });
73 m_updatedToken =
74 m_watcher.Updated([thisPtr](DeviceWatcher, const DeviceInformationUpdate &upd) {
75 emit thisPtr->deviceUpdated(upd.Id(), thisPtr->m_id);
76 });
77 // because of ambiguous declaration
78 using WinRtInspectable = winrt::Windows::Foundation::IInspectable;
79 m_enumerationToken =
80 m_watcher.EnumerationCompleted([thisPtr](DeviceWatcher, const WinRtInspectable &) {
81 emit thisPtr->enumerationCompleted(thisPtr->m_id);
82 });
83 m_stoppedToken = m_watcher.Stopped([thisPtr](DeviceWatcher, const WinRtInspectable &) {
84 emit thisPtr->watcherStopped(thisPtr->m_id);
85 });
86}
87
88void QBluetoothDeviceWatcherWinRT::unsubscribeFromEvents()
89{
90 m_watcher.Added(m_addedToken);
91 m_watcher.Removed(m_removedToken);
92 m_watcher.Updated(m_updatedToken);
93 m_watcher.EnumerationCompleted(m_enumerationToken);
94 m_watcher.Stopped(m_stoppedToken);
95}
96
97bool QBluetoothDeviceWatcherWinRT::canStop() const
98{
99 const auto status = m_watcher.Status();
100 // Also 'Aborted', but calling Stop() there is a no-op
101 return status == DeviceWatcherStatus::Started
102 || status == DeviceWatcherStatus::EnumerationCompleted;
103}
104
QBluetoothDeviceWatcherWinRT(int id, winrt::hstring selector)
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
GLenum GLuint id
[7]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define emit
QFileSelector selector
[1]
QHostInfo info
[0]