Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qbluetoothlocaldevice.cpp
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
#
include
"qbluetoothlocaldevice.h"
5
#
include
"qbluetoothlocaldevice_p.h"
6
#
include
"qbluetoothaddress.h"
7
8
#
include
<
QtCore
/
QString
>
9
10
QT_BEGIN_NAMESPACE
11
12
QT_IMPL_METATYPE_EXTERN_TAGGED(QBluetoothLocalDevice::Pairing, QBluetoothLocalDevice__Pairing)
13
QT_IMPL_METATYPE_EXTERN_TAGGED(QBluetoothLocalDevice::HostMode, QBluetoothLocalDevice__HostMode)
14
QT_IMPL_METATYPE_EXTERN_TAGGED(QBluetoothLocalDevice::Error, QBluetoothLocalDevice__Error)
15
16
/*!
17
\class QBluetoothLocalDevice
18
\inmodule QtBluetooth
19
\brief The QBluetoothLocalDevice class enables access to the local Bluetooth
20
device.
21
22
\since 5.2
23
24
QBluetoothLocalDevice provides functions for getting and setting the state of local Bluetooth
25
devices.
26
27
On iOS, this class cannot be used because the platform does not expose
28
any data or API which may provide information on the local Bluetooth device.
29
*/
30
31
/*!
32
\enum QBluetoothLocalDevice::Pairing
33
34
This enum describes the pairing state between the two Bluetooth devices.
35
36
\value Unpaired The Bluetooth devices are not paired.
37
\value Paired The Bluetooth devices are paired. The system will prompt the user for
38
authorization when the remote device initiates a connection to the
39
local device.
40
\value AuthorizedPaired The Bluetooth devices are paired. The system will not prompt the user
41
for authorization when the remote device initiates a connection to the
42
local device.
43
*/
44
45
/*!
46
\enum QBluetoothLocalDevice::Error
47
48
This enum describes errors that maybe returned
49
50
\value NoError No known error
51
\value PairingError Error in pairing
52
\value [since 6.4] MissingPermissionsError The operating system requests
53
permissions which were not
54
granted by the user.
55
\value UnknownError Unknown error
56
57
*/
58
59
/*!
60
\enum QBluetoothLocalDevice::HostMode
61
62
This enum describes the most of the local Bluetooth device.
63
64
\value HostPoweredOff Power off the device
65
\value HostConnectable Remote Bluetooth devices can connect to the local Bluetooth device
66
if they have previously been paired with it or otherwise know its address. This powers up the
67
device if it was powered off.
68
\value HostDiscoverable Remote Bluetooth devices can discover the presence of the local
69
Bluetooth device. The device will also be connectable, and powered on. On Android, this mode can only be active
70
for a maximum of 5 minutes.
71
\value HostDiscoverableLimitedInquiry Remote Bluetooth devices can discover the presence of the local
72
Bluetooth device when performing a limited inquiry. This should be used for locating services that are
73
only made discoverable for a limited period of time. This can speed up discovery between gaming devices,
74
as service discovery can be skipped on devices not in LimitedInquiry mode. In this mode, the device will
75
be connectable and powered on, if required. This mode is is not supported on Android.
76
77
\note On \macos, it is not possible to set the \l hostMode(). The
78
reported host modes are limited to HostPoweredOff and HostConnectable.
79
80
\note On Windows, it is not possible to set the \l hostMode() to
81
HostDiscoverable or HostDiscoverableLimitedInquiry. Using these modes is
82
equivalent to HostConnectable.
83
84
\note On HarmonyOS, changing the power state of the adapter always raises a
85
system dialog that the user must answer. The request therefore completes
86
asynchronously and may be declined, in which case \l errorOccurred() is
87
emitted and the host mode is left unchanged. HostDiscoverableLimitedInquiry
88
is not given a timeout on this platform, so the device stays discoverable
89
until the host mode is changed again, and the adapter reports itself back
90
as \l {QBluetoothLocalDevice::}{HostDiscoverable}.
91
92
\note Starting from Android 13 (API level 33) the HostPoweredOff state relies on
93
non-public Android API as the public one has been deprecated, see
94
(\l {https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#disable()}
95
{disable()}). This may change in a future version of Android.
96
97
\note At least on Android 12 the device's Bluetooth visibility setting may dictate the result
98
of setting either HostDiscoverable or HostConnectable. For example if the visibility is set
99
\e off, it may not be possible to enter the HostDiscoverable mode, but HostConnectable will be
100
used instead. This may change in future version of Android.
101
102
*/
103
104
void
registerQBluetoothLocalDeviceMetaType()
105
{
106
static
bool
initDone =
false
;
107
if
(!initDone) {
108
qRegisterMetaType<QBluetoothLocalDevice::HostMode>();
109
qRegisterMetaType<QBluetoothLocalDevice::Pairing>();
110
qRegisterMetaType<QBluetoothLocalDevice::Error>();
111
initDone =
true
;
112
}
113
}
114
115
#
ifndef
QT_OSX_BLUETOOTH
116
117
/*!
118
Destroys the QBluetoothLocalDevice.
119
*/
120
QBluetoothLocalDevice::~QBluetoothLocalDevice()
121
{
122
delete
d_ptr;
123
}
124
125
/*!
126
Returns \c true if the QBluetoothLocalDevice represents an available local Bluetooth device;
127
otherwise return false.
128
129
If the local Bluetooth adapter represented by an instance of this class
130
is removed from the system (e.g. removal of the underlying Bluetooth dongle)
131
then this instance will become invalid. An already invalid QBluetoothLocalDevice instance
132
remains invalid even if the same Bluetooth adapter is returned to
133
the system.
134
135
//! [android-permissions-valid]
136
\note Starting from Android 12 (API level 31), the construction of this class requires
137
\l {https://developer.android.com/guide/topics/connectivity/bluetooth/permissions}
138
{bluetooth runtime permissions} (\e BLUETOOTH_SCAN and \e BLUETOOTH_CONNECT). If the
139
permissions are not granted, the device will not be valid.
140
//! [android-permissions-valid]
141
142
\sa allDevices()
143
*/
144
bool
QBluetoothLocalDevice::isValid()
const
145
{
146
if
(d_ptr)
147
return
d_ptr->isValid();
148
return
false
;
149
}
150
151
#
endif
152
153
/*!
154
\fn void QBluetoothLocalDevice::setHostMode(QBluetoothLocalDevice::HostMode mode)
155
156
Sets the host mode of this local Bluetooth device to \a mode.
157
158
Some transitions such as turning the device on or off may take some time. Therefore
159
subsequent calls should only be made once the \l hostModeStateChanged() signal
160
has concluded the previous request. If this is ignored the result of such a series
161
of calls is not well defined.
162
163
\note Due to varying security policies on the supported platforms, this method may have
164
differing behaviors on the various platforms. For example the system may ask the user for
165
confirmation before turning Bluetooth on or off and not all host modes may be supported.
166
On \macos, it is not possbile to programmatically change the \l hostMode().
167
A user can only switch Bluetooth on/off in the System Preferences.
168
On Windows this method \e must be called from the UI thread because it might
169
require user confirmation.
170
Please refer to the platform specific Bluetooth documentation for details.
171
*/
172
173
/*!
174
\fn QBluetoothLocalDevice::HostMode QBluetoothLocalDevice::hostMode() const
175
176
Returns the current host mode of this local Bluetooth device. On \macos, it is either
177
HostPoweredOff or HostConnectable.
178
*/
179
180
/*!
181
\fn QBluetoothLocalDevice::name() const
182
183
Returns the name assgined by the user to this Bluetooth device.
184
*/
185
186
/*!
187
\fn QBluetoothLocalDevice::address() const
188
189
Returns the MAC address of this Bluetooth device.
190
191
\note On Android, this function always returns the constant
192
value \c {02:00:00:00:00:00} as local address starting with Android 6.0.
193
The programmatic access to the device's local MAC address was removed.
194
195
\note On HarmonyOS, this function always returns a null address. The system
196
provides no API to read the local adapter address.
197
*/
198
199
/*!
200
\fn QList<QBluetoothLocalDevice> QBluetoothLocalDevice::allDevices()
201
202
Returns a list of all available local Bluetooth devices. On \macos and
203
HarmonyOS, there is only the "default" local device.
204
205
\note On HarmonyOS the entry is reported only while the application holds
206
the Bluetooth access permission and the adapter is powered on, and its
207
address is null, see address(). An empty list therefore does not imply
208
the absence of an adapter.
209
*/
210
211
/*!
212
\fn QBluetoothLocalDevice::powerOn()
213
214
Powers on the device after returning it to the hostMode() state, if it was powered off.
215
216
\note Due to varying security policies on the supported platforms, this method may have
217
differing behaviors on the various platforms. For example
218
the system may ask the user for confirmation before turning Bluetooth on or off.
219
On \macos it is not possible to power on/off Bluetooth.
220
Please refer to the platform specific Bluetooth documentation for details.
221
*/
222
223
/*!
224
\fn QBluetoothLocalDevice::QBluetoothLocalDevice(QObject *parent)
225
Constructs a QBluetoothLocalDevice with \a parent.
226
227
\include qbluetoothlocaldevice.cpp android-permissions-valid
228
\sa isValid()
229
*/
230
231
/*!
232
\fn QBluetoothLocalDevice::hostModeStateChanged(QBluetoothLocalDevice::HostMode state)
233
The \a state of the host has transitioned to a different HostMode.
234
*/
235
236
/*!
237
\fn QBluetoothLocalDevice::deviceConnected(const QBluetoothAddress &address)
238
\since 5.3
239
240
This signal is emitted when the local device establishes a connection to a remote device
241
with \a address.
242
243
\sa deviceDisconnected(), connectedDevices()
244
*/
245
246
/*!
247
\fn QBluetoothLocalDevice::deviceDisconnected(const QBluetoothAddress &address)
248
\since 5.3
249
250
This signal is emitted when the local device disconnects from a remote Bluetooth device
251
with \a address.
252
253
\sa deviceConnected(), connectedDevices()
254
*/
255
256
/*!
257
\fn QList<QBluetoothAddress> QBluetoothLocalDevice::connectedDevices() const
258
\since 5.3
259
260
Returns the list of connected devices. This list is different from the list of currently
261
paired devices.
262
263
On Android and \macos, it is not possible to retrieve a list of connected devices. It is only possible to
264
listen to (dis)connect changes. For convenience, this class monitors all connect
265
and disconnect events since its instanciation and returns the current list when calling this function.
266
Therefore it is possible that this function returns an empty list shortly after creating an
267
instance.
268
269
On HarmonyOS, this function always returns an empty list. The system exposes no
270
device-level list of connected devices, and reports connection state only for
271
the individual profiles it implements, none of which Qt Bluetooth uses.
272
\l deviceConnected() and \l deviceDisconnected() are therefore never emitted.
273
274
\sa deviceConnected(), deviceDisconnected()
275
*/
276
277
/*!
278
\fn QBluetoothLocalDevice::pairingStatus(const QBluetoothAddress &address) const
279
280
Returns the current bluetooth pairing status of \a address, if it's unpaired, paired, or paired and authorized.
281
*/
282
283
/*!
284
\fn QBluetoothLocalDevice::requestPairing(const QBluetoothAddress &address, Pairing pairing)
285
286
Set the \a pairing status with \a address. The results are returned by the signal, pairingFinished().
287
288
On Android and \macos, AuthorizedPaired is not possible and will have the same behavior as Paired.
289
On Windows the exact pairing mode decision is up to the operating system.
290
291
On HarmonyOS, AuthorizedPaired has the same behavior as Paired, and it is not
292
possible to unpair a device. If Unpaired is requested for a paired device,
293
\l errorOccurred() is emitted and the device remains paired.
294
295
On \macos, it is not possible to unpair a device. If Unpaired is requested, \l pairingFinished()
296
is immediately emitted although the device remains paired. It is possible to request the pairing
297
for a previously unpaired device. In addition \l AuthorizedPaired has the same behavior as \l Paired.
298
299
Caution: creating a pairing may take minutes, and may require the user to acknowledge.
300
*/
301
302
/*!
303
\fn QBluetoothLocalDevice::pairingFinished(const QBluetoothAddress &address,
304
QBluetoothLocalDevice::Pairing pairing)
305
306
Pairing or unpairing has completed with \a address. Current pairing status is in \a pairing.
307
If the pairing request was not successful, this signal will not be emitted. The errorOccurred()
308
signal is emitted if the pairing request failed. The signal is only ever emitted for pairing
309
requests which have previously requested by calling \l requestPairing() of the current object
310
instance.
311
*/
312
313
/*!
314
\fn QBluetoothLocalDevice::errorOccurred(QBluetoothLocalDevice::Error error)
315
Signal emitted if there's an exceptional \a error while pairing or while
316
changing the host mode.
317
318
\since 6.2
319
*/
320
321
/*!
322
\fn QBluetoothLocalDevice::QBluetoothLocalDevice(const QBluetoothAddress &address, QObject *parent = 0)
323
324
Construct new QBluetoothLocalDevice for \a address. If \a address is default constructed
325
the resulting local device selects the local default device.
326
327
\include qbluetoothlocaldevice.cpp android-permissions-valid
328
\sa isValid()
329
*/
330
331
QT_END_NAMESPACE
332
333
#
include
"moc_qbluetoothlocaldevice.cpp"
qtconnectivity
src
bluetooth
qbluetoothlocaldevice.cpp
Generated on
for Qt by
1.18.0