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
qbluetoothservicediscoveryagent_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 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
4#ifndef QBLUETOOTHSERVICEDISCOVERYAGENT_P_H
5#define QBLUETOOTHSERVICEDISCOVERYAGENT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
22
23#include <QStack>
24#include <QStringList>
25
26#ifdef QT_HARMONY_BLUETOOTH
27#include <memory>
28#include <optional>
29#include <string>
30
31QT_BEGIN_NAMESPACE
32namespace QtOhosBluetooth {
33class QOhosBluetoothRemoteDeviceProxy;
34struct RemoteDeviceServices;
35}
36QT_END_NAMESPACE
37#endif
38
39#if QT_CONFIG(bluez)
40
41namespace QtBluetoothPrivate {
42
43class OrgBluezManagerInterface;
44class OrgBluezAdapterInterface;
45class OrgBluezDeviceInterface;
46class OrgFreedesktopDBusObjectManagerInterface;
47
48} // namespace QtBluetoothPrivate
49
50#include <QtCore/qprocess.h>
51
52QT_BEGIN_NAMESPACE
53class QDBusPendingCallWatcher;
54class QXmlStreamReader;
55QT_END_NAMESPACE
56#endif
57
58#ifdef QT_WINRT_BLUETOOTH
59#include <QtCore/QPointer>
60#endif
61
62#ifdef QT_OSX_BLUETOOTH
63#include "darwin/btdelegates_p.h"
64#include "darwin/btraii_p.h"
65#endif
66
67#ifdef QT_ANDROID_BLUETOOTH
68#include <QtCore/QJniObject>
69#include <QtBluetooth/QBluetoothLocalDevice>
70#endif
71
72// intentionally outside of Qt namespace
73class tst_QBluetoothServiceDiscoveryAgent;
74
75QT_BEGIN_NAMESPACE
76
77class QBluetoothDeviceDiscoveryAgent;
78#ifdef QT_ANDROID_BLUETOOTH
79class ServiceDiscoveryBroadcastReceiver;
80class LocalDeviceBroadcastReceiver;
81#endif
82
83#ifdef QT_WINRT_BLUETOOTH
84class QWinRTBluetoothServiceDiscoveryWorker;
85#endif
86
88#if defined(QT_WINRT_BLUETOOTH)
89 : public QObject
90{
91 Q_OBJECT
92#elif defined(QT_OSX_BLUETOOTH)
93 : public QObject, public DarwinBluetooth::SDPInquiryDelegate
94{
95#else
96{
97#endif
98 Q_DECLARE_PUBLIC(QBluetoothServiceDiscoveryAgent)
99 friend class ::tst_QBluetoothServiceDiscoveryAgent;
100
101public:
102 // Best-guess value. Should be enough for all real usecases.
103 static constexpr int kMaxSdpRecursionDepth = 64;
104
110
111 QBluetoothServiceDiscoveryAgentPrivate(QBluetoothServiceDiscoveryAgent *qp,
114
116 void stopDeviceDiscovery();
119
120 void setDiscoveryState(DiscoveryState s) { state = s; }
121 inline DiscoveryState discoveryState() { return state; }
122
123 void setDiscoveryMode(QBluetoothServiceDiscoveryAgent::DiscoveryMode m) { mode = m; }
125
127 void _q_deviceDiscovered(const QBluetoothDeviceInfo &info);
130#if QT_CONFIG(bluez)
131 void _q_sdpScannerDone(int exitCode, QProcess::ExitStatus status);
132 void _q_finishSdpScan(QBluetoothServiceDiscoveryAgent::Error errorCode,
133 const QString &errorDescription,
134 const QStringList &xmlRecords);
135#endif
136#ifdef QT_ANDROID_BLUETOOTH
137 void _q_processFetchedUuids(const QBluetoothAddress &address, const QList<QBluetoothUuid> &uuids);
138
139 void populateDiscoveredServices(const QBluetoothDeviceInfo &remoteDevice,
140 const QList<QBluetoothUuid> &uuids);
141 void _q_fetchUuidsTimeout();
142 void _q_hostModeStateChanged(QBluetoothLocalDevice::HostMode state);
143#endif
144
145private:
146 void start(const QBluetoothAddress &address);
147 void stop();
148 bool isDuplicatedService(const QBluetoothServiceInfo &serviceInfo) const;
149
150#if QT_CONFIG(bluez)
151 void startBluez5(const QBluetoothAddress &address);
152 void runExternalSdpScan(const QBluetoothAddress &remoteAddress,
153 const QBluetoothAddress &localAddress);
154 void sdpScannerDone(int exitCode, QProcess::ExitStatus exitStatus);
155 QVariant readAttributeValue(QXmlStreamReader &xml, int depth = 0);
156 QBluetoothServiceInfo parseServiceXml(const QString& xml);
157 void performMinimalServiceDiscovery(const QBluetoothAddress &deviceAddress);
158#endif
159
160public:
161 QBluetoothServiceDiscoveryAgent::Error error;
163 QBluetoothAddress deviceAddress;
166 QBluetoothAddress m_deviceAdapterAddress;
167
168private:
169 DiscoveryState state;
170 QList<QBluetoothUuid> uuidFilter;
171
172 QBluetoothDeviceDiscoveryAgent *deviceDiscoveryAgent = nullptr;
173
174 QBluetoothServiceDiscoveryAgent::DiscoveryMode mode;
175
176 bool singleDevice;
177#if QT_CONFIG(bluez)
178 QString foundHostAdapterPath;
179 QtBluetoothPrivate::OrgFreedesktopDBusObjectManagerInterface *manager = nullptr;
180 QProcess *sdpScannerProcess = nullptr;
181#endif
182
183#ifdef QT_ANDROID_BLUETOOTH
184 ServiceDiscoveryBroadcastReceiver *receiver = nullptr;
185 LocalDeviceBroadcastReceiver *localDeviceReceiver = nullptr;
186
187 QJniObject btAdapter;
188 // The sdpCache caches service discovery results while it is running, and is
189 // cleared once finished. The cache is used as we may (or may not) get more accurate
190 // results after the first result. This temporary caching allows to send the
191 // serviceDiscovered() signal once per service and with the most accurate information.
192 // Partial cache clearing may occur already during the scan if the second (more accurate)
193 // scan result is received.
194 QMap<QBluetoothAddress,QPair<QBluetoothDeviceInfo,QList<QBluetoothUuid> > > sdpCache;
195#endif
196
197#ifdef QT_WINRT_BLUETOOTH
198private slots:
199 void processFoundService(quint64 deviceAddress, const QBluetoothServiceInfo &info);
200 void onScanFinished(quint64 deviceAddress);
201 void onError();
202
203private:
204 void releaseWorker();
205 QPointer<QWinRTBluetoothServiceDiscoveryWorker> worker;
206#endif
207
208#ifdef QT_OSX_BLUETOOTH
209 // SDPInquiryDelegate:
210 void SDPInquiryFinished(void *device) override;
211 void SDPInquiryError(void *device, IOReturn errorCode) override;
212
213 void performMinimalServiceDiscovery(const QBluetoothAddress &deviceAddress);
214 //void serviceDiscoveryFinished();
215
216 bool serviceHasMatchingUuid(const QBluetoothServiceInfo &serviceInfo) const;
217
218 DarwinBluetooth::ScopedPointer serviceInquiry;
219#endif // QT_OSX_BLUETOOTH
220
221#ifdef QT_HARMONY_BLUETOOTH
222 void reportErrorAsync(
223 QBluetoothServiceDiscoveryAgent::Error discoveryError, const QString &discoveryErrorString);
224 void finishDeviceDiscoveryAsync();
225 void reportRemoteDeviceServices(
226 const QtOhosBluetooth::RemoteDeviceServices &remoteDeviceServices);
227
228 std::shared_ptr<QtOhosBluetooth::QOhosBluetoothRemoteDeviceProxy> m_remoteDeviceProxy;
229 std::optional<std::string> m_optRequestedDeviceId;
230#endif
231
232protected:
233 QBluetoothServiceDiscoveryAgent *q_ptr;
234};
235
236QT_END_NAMESPACE
237
238#endif
void _q_deviceDiscovered(const QBluetoothDeviceInfo &info)
void setDiscoveryMode(QBluetoothServiceDiscoveryAgent::DiscoveryMode m)
void _q_deviceDiscoveryFinished()
Called when device discovery finishes.
void startServiceDiscovery()
Starts service discovery for the next device.
QBluetoothServiceDiscoveryAgent::DiscoveryMode DiscoveryMode()
void _q_deviceDiscoveryError(QBluetoothDeviceDiscoveryAgent::Error)
void qt_test_iobluetooth_runloop()
Definition btutility.mm:125