Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbluetoothdevicediscoveryagent_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2014 Denis Shienkov <denis.shienkov@gmail.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QBLUETOOTHDEVICEDISCOVERYAGENT_P_H
6#define QBLUETOOTHDEVICEDISCOVERYAGENT_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
20#ifdef QT_ANDROID_BLUETOOTH
21#include <QtCore/QJniObject>
22#include "android/devicediscoverybroadcastreceiver_p.h"
23#include <QtCore/QTimer>
24#endif
25
26#ifdef Q_OS_DARWIN
27#include "darwin/btdelegates_p.h"
28#include "darwin/btraii_p.h"
29#endif // Q_OS_DARWIN
30
31#include <QtCore/QVariantMap>
32
33#include <QtBluetooth/QBluetoothAddress>
34#include <QtBluetooth/QBluetoothLocalDevice>
35
36#if QT_CONFIG(bluez)
37#include "bluez/bluez5_helper_p.h"
38
39namespace QtBluetoothPrivate {
40
41class OrgBluezManagerInterface;
42class OrgBluezAdapterInterface;
43class OrgFreedesktopDBusObjectManagerInterface;
44class OrgFreedesktopDBusPropertiesInterface;
45class OrgBluezAdapter1Interface;
46class OrgBluezDevice1Interface;
47
48} // namespace QtBluetoothPrivate
49
50QT_BEGIN_NAMESPACE
51class QDBusVariant;
52QT_END_NAMESPACE
53#endif
54
55#ifdef QT_WINRT_BLUETOOTH
56#include <QtCore/QPointer>
57#include <QtCore/QTimer>
58
59using ManufacturerData = QHash<quint16, QByteArray>;
60using ServiceData = QHash<QBluetoothUuid, QByteArray>;
61QT_DECL_METATYPE_EXTERN(ManufacturerData, Q_BLUETOOTH_EXPORT)
62QT_DECL_METATYPE_EXTERN(ServiceData, Q_BLUETOOTH_EXPORT)
63#endif
64
65#ifdef QT_HARMONY_BLUETOOTH
66#include <QtBluetooth/private/qohosbluetoothdevicediscovery_p.h>
67#endif
68
69QT_BEGIN_NAMESPACE
70
71#ifdef QT_WINRT_BLUETOOTH
72class QWinRTBluetoothDeviceDiscoveryWorker;
73#endif
74
75class QBluetoothDeviceDiscoveryAgentPrivate
76#if defined(QT_ANDROID_BLUETOOTH) || defined(QT_WINRT_BLUETOOTH)
77 || defined(Q_OS_DARWIN) || defined(QT_HARMONY_BLUETOOTH)
78 : public QObject
79#if defined(Q_OS_MACOS)
80 , public DarwinBluetooth::DeviceInquiryDelegate
81#endif // Q_OS_MACOS
82{
83 Q_OBJECT
84#else // BlueZ
85{
86#endif
87 Q_DECLARE_PUBLIC(QBluetoothDeviceDiscoveryAgent)
88public:
89 QBluetoothDeviceDiscoveryAgentPrivate(
90 const QBluetoothAddress &deviceAdapter,
91 QBluetoothDeviceDiscoveryAgent *parent);
92 ~QBluetoothDeviceDiscoveryAgentPrivate();
93
94 void start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods);
95 void stop();
96 bool isActive() const;
97
98#if QT_CONFIG(bluez)
99 void _q_InterfacesAdded(const QDBusObjectPath &object_path,
100 InterfaceList interfaces_and_properties);
101 void _q_discoveryFinished();
102 void _q_discoveryInterrupted(const QString &path);
103 void _q_PropertiesChanged(const QString &interface,
104 const QString &path,
105 const QVariantMap &changed_properties,
106 const QStringList &invalidated_properties);
107#endif
108
109private:
110 QList<QBluetoothDeviceInfo> discoveredDevices;
111
112 QBluetoothDeviceDiscoveryAgent::Error lastError = QBluetoothDeviceDiscoveryAgent::NoError;
113 QString errorString;
114 QBluetoothAddress adapterAddress;
115
116#ifdef QT_ANDROID_BLUETOOTH
117private slots:
118 void processSdpDiscoveryFinished();
119 void processDiscoveredDevices(const QBluetoothDeviceInfo &info, bool isLeResult);
120 friend void QtBluetoothLE_leScanResult(JNIEnv *, jobject, jlong, jobject);
121 void stopLowEnergyScan();
122
123private:
124 void startLowEnergyScan();
125 void processLeDiscoveryFinished(int epoch);
126 void classicDiscoveryStartFail();
127 bool setErrorIfPowerOff();
128
129 DeviceDiscoveryBroadcastReceiver *receiver = nullptr;
130 short m_active;
131 QJniObject adapter;
132 QJniObject leScanner;
133 QTimer *leScanTimeout = nullptr;
134 QTimer *deviceDiscoveryStartTimeout = nullptr;
135 short deviceDiscoveryStartAttemptsLeft;
136 int leScanEpoch = 0;
137
138 bool pendingCancel = false;
139 bool pendingStart = false;
140#elif QT_CONFIG(bluez)
141 bool pendingCancel = false;
142 bool pendingStart = false;
143 QtBluetoothPrivate::OrgFreedesktopDBusObjectManagerInterface *manager = nullptr;
144 QtBluetoothPrivate::OrgBluezAdapter1Interface *adapter = nullptr;
145 QTimer *discoveryTimer = nullptr;
146 QList<QtBluetoothPrivate::OrgFreedesktopDBusPropertiesInterface *> propertyMonitors;
147
148 void deviceFound(const QString &devicePath, const QVariantMap &properties);
149
150 QMap<QString, QVariantMap> devicesProperties;
151#endif
152
153#ifdef QT_WINRT_BLUETOOTH
154private slots:
155 void registerDevice(const QBluetoothDeviceInfo &info);
156 void updateDeviceData(const QBluetoothAddress &address, QBluetoothDeviceInfo::Fields fields,
157 qint16 rssi, ManufacturerData manufacturerData, ServiceData serviceData);
158 void onErrorOccured(QBluetoothDeviceDiscoveryAgent::Error e);
159 void onScanFinished();
160
161private:
162 void disconnectAndClearWorker();
163 std::shared_ptr<QWinRTBluetoothDeviceDiscoveryWorker> worker = nullptr;
164#endif
165
166#ifdef Q_OS_DARWIN
167
168 void startLE();
169
170#ifdef Q_OS_MACOS
171
172 void startClassic();
173
174 // Classic (IOBluetooth) inquiry delegate's methods:
175 void inquiryFinished() override;
176 void error(IOReturn error) override;
177 void classicDeviceFound(void *device) override;
178 // Classic (IOBluetooth) errors:
179 void setError(IOReturn error, const QString &text = QString());
180
181#endif // Q_OS_MACOS
182
183 // LE scan delegates (CoreBluetooth, all Darwin OSes):
184 void LEinquiryFinished();
185 void LEinquiryError(QBluetoothDeviceDiscoveryAgent::Error error);
186 void LEnotSupported();
187
188 // LE errors:
189 void setError(QBluetoothDeviceDiscoveryAgent::Error,
190 const QString &text = QString());
191
192 // Both LE and Classic devices go there:
193 void deviceFound(const QBluetoothDeviceInfo &newDeviceInfo);
194
195 enum AgentState {
196 NonActive,
197 ClassicScan, // macOS (IOBluetooth) only
198 LEScan
199 } agentState;
200
201 bool startPending = false;
202 bool stopPending = false;
203
204#ifdef Q_OS_MACOS
205
206 DarwinBluetooth::ScopedPointer controller;
207 DarwinBluetooth::ScopedPointer inquiry;
208
209#endif // Q_OS_MACOS
210
211 DarwinBluetooth::ScopedPointer inquiryLE;
212
213#endif // Q_OS_DARWIN
214
215#ifdef QT_HARMONY_BLUETOOTH
216 enum class DiscoveryStopReason { Finished, BluetoothPoweredOff };
217 void reportDiscoveryStopped(DiscoveryStopReason reason);
218 void clearDiscoveryState();
219 void emitAsyncError();
220 void reportPairedDevicesAsync();
221 void reportDiscoveredDevices(const std::vector<QBluetoothDeviceInfo> &bluetoothDevicesInfos);
222 std::shared_ptr<QtOhosBluetooth::QOhosBluetoothDeviceDiscoveryAgentProxy> m_discoveryAgentProxy;
223 bool m_discoveryRequested = false;
224 bool m_pendingStart = false;
225 bool m_pendingCancel = false;
226#endif
227
228 int lowEnergySearchTimeout = 40000;
229 QBluetoothDeviceDiscoveryAgent::DiscoveryMethods requestedMethods;
230 QBluetoothDeviceDiscoveryAgent *q_ptr;
231};
232
234
235#endif
\inmodule QtBluetooth
Combined button and popup list for selecting options.