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
65QT_BEGIN_NAMESPACE
66
67#ifdef QT_WINRT_BLUETOOTH
68class QWinRTBluetoothDeviceDiscoveryWorker;
69#endif
70
71class QBluetoothDeviceDiscoveryAgentPrivate
72#if defined(QT_ANDROID_BLUETOOTH) || defined(QT_WINRT_BLUETOOTH)
73 || defined(Q_OS_DARWIN)
74 : public QObject
75#if defined(Q_OS_MACOS)
76 , public DarwinBluetooth::DeviceInquiryDelegate
77#endif // Q_OS_MACOS
78{
79 Q_OBJECT
80#else // BlueZ
81{
82#endif
83 Q_DECLARE_PUBLIC(QBluetoothDeviceDiscoveryAgent)
84public:
85 QBluetoothDeviceDiscoveryAgentPrivate(
86 const QBluetoothAddress &deviceAdapter,
87 QBluetoothDeviceDiscoveryAgent *parent);
88 ~QBluetoothDeviceDiscoveryAgentPrivate();
89
90 void start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods);
91 void stop();
92 bool isActive() const;
93
94#if QT_CONFIG(bluez)
95 void _q_InterfacesAdded(const QDBusObjectPath &object_path,
96 InterfaceList interfaces_and_properties);
97 void _q_discoveryFinished();
98 void _q_discoveryInterrupted(const QString &path);
99 void _q_PropertiesChanged(const QString &interface,
100 const QString &path,
101 const QVariantMap &changed_properties,
102 const QStringList &invalidated_properties);
103#endif
104
105private:
106 QList<QBluetoothDeviceInfo> discoveredDevices;
107
108 QBluetoothDeviceDiscoveryAgent::Error lastError = QBluetoothDeviceDiscoveryAgent::NoError;
109 QString errorString;
110 QBluetoothAddress adapterAddress;
111
112#ifdef QT_ANDROID_BLUETOOTH
113private slots:
114 void processSdpDiscoveryFinished();
115 void processDiscoveredDevices(const QBluetoothDeviceInfo &info, bool isLeResult);
116 friend void QtBluetoothLE_leScanResult(JNIEnv *, jobject, jlong, jobject);
117 void stopLowEnergyScan();
118
119private:
120 void startLowEnergyScan();
121 void processLeDiscoveryFinished(int epoch);
122 void classicDiscoveryStartFail();
123 bool setErrorIfPowerOff();
124
125 DeviceDiscoveryBroadcastReceiver *receiver = nullptr;
126 short m_active;
127 QJniObject adapter;
128 QJniObject leScanner;
129 QTimer *leScanTimeout = nullptr;
130 QTimer *deviceDiscoveryStartTimeout = nullptr;
131 short deviceDiscoveryStartAttemptsLeft;
132 int leScanEpoch = 0;
133
134 bool pendingCancel = false;
135 bool pendingStart = false;
136#elif QT_CONFIG(bluez)
137 bool pendingCancel = false;
138 bool pendingStart = false;
139 QtBluetoothPrivate::OrgFreedesktopDBusObjectManagerInterface *manager = nullptr;
140 QtBluetoothPrivate::OrgBluezAdapter1Interface *adapter = nullptr;
141 QTimer *discoveryTimer = nullptr;
142 QList<QtBluetoothPrivate::OrgFreedesktopDBusPropertiesInterface *> propertyMonitors;
143
144 void deviceFound(const QString &devicePath, const QVariantMap &properties);
145
146 QMap<QString, QVariantMap> devicesProperties;
147#endif
148
149#ifdef QT_WINRT_BLUETOOTH
150private slots:
151 void registerDevice(const QBluetoothDeviceInfo &info);
152 void updateDeviceData(const QBluetoothAddress &address, QBluetoothDeviceInfo::Fields fields,
153 qint16 rssi, ManufacturerData manufacturerData, ServiceData serviceData);
154 void onErrorOccured(QBluetoothDeviceDiscoveryAgent::Error e);
155 void onScanFinished();
156
157private:
158 void disconnectAndClearWorker();
159 std::shared_ptr<QWinRTBluetoothDeviceDiscoveryWorker> worker = nullptr;
160#endif
161
162#ifdef Q_OS_DARWIN
163
164 void startLE();
165
166#ifdef Q_OS_MACOS
167
168 void startClassic();
169
170 // Classic (IOBluetooth) inquiry delegate's methods:
171 void inquiryFinished() override;
172 void error(IOReturn error) override;
173 void classicDeviceFound(void *device) override;
174 // Classic (IOBluetooth) errors:
175 void setError(IOReturn error, const QString &text = QString());
176
177#endif // Q_OS_MACOS
178
179 // LE scan delegates (CoreBluetooth, all Darwin OSes):
180 void LEinquiryFinished();
181 void LEinquiryError(QBluetoothDeviceDiscoveryAgent::Error error);
182 void LEnotSupported();
183
184 // LE errors:
185 void setError(QBluetoothDeviceDiscoveryAgent::Error,
186 const QString &text = QString());
187
188 // Both LE and Classic devices go there:
189 void deviceFound(const QBluetoothDeviceInfo &newDeviceInfo);
190
191 enum AgentState {
192 NonActive,
193 ClassicScan, // macOS (IOBluetooth) only
194 LEScan
195 } agentState;
196
197 bool startPending = false;
198 bool stopPending = false;
199
200#ifdef Q_OS_MACOS
201
202 DarwinBluetooth::ScopedPointer controller;
203 DarwinBluetooth::ScopedPointer inquiry;
204
205#endif // Q_OS_MACOS
206
207 DarwinBluetooth::ScopedPointer inquiryLE;
208
209#endif // Q_OS_DARWIN
210
211 int lowEnergySearchTimeout = 40000;
212 QBluetoothDeviceDiscoveryAgent::DiscoveryMethods requestedMethods;
213 QBluetoothDeviceDiscoveryAgent *q_ptr;
214};
215
217
218#endif
\inmodule QtBluetooth
Combined button and popup list for selecting options.