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 "qlowenergyadvertisingdata.h"
5
6#include <cstring>
7
8QT_BEGIN_NAMESPACE
9
10class QLowEnergyAdvertisingDataPrivate : public QSharedData
11{
12public:
13 QLowEnergyAdvertisingDataPrivate()
14 : manufacturerId(QLowEnergyAdvertisingData::invalidManufacturerId())
15 , discoverability(QLowEnergyAdvertisingData::DiscoverabilityNone)
16 , includePowerLevel(false)
17 {
18 }
19
20 QString localName;
21 QByteArray manufacturerData;
22 QByteArray rawData;
23 QList<QBluetoothUuid> services;
24 quint16 manufacturerId;
25 QLowEnergyAdvertisingData::Discoverability discoverability;
26 bool includePowerLevel;
27};
28
29/*!
30 \since 5.7
31 \class QLowEnergyAdvertisingData
32 \brief The QLowEnergyAdvertisingData class represents the data to be broadcast during
33 Bluetooth Low Energy advertising.
34 \inmodule QtBluetooth
35 \ingroup shared
36
37 This data can include the device name, GATT services offered by the device, and so on.
38 The data set via this class will be used when advertising is started by calling
39 \l QLowEnergyController::startAdvertising(). Objects of this class can represent an
40 Advertising Data packet or a Scan Response packet.
41 \note The actual data packets sent over the advertising channel cannot contain more than 31
42 bytes. If the variable-length data set via this class exceeds that limit, it will
43 be left out of the packet or truncated, depending on the type.
44 On Android, advertising will fail if advertising data is larger than 31 bytes.
45 On Bluez DBus backend the advertising length limit and the behavior when it is exceeded
46 is up to BlueZ; it may for example support extended advertising. For the most
47 predictable behavior keep the advertising data short.
48
49 \sa QLowEnergyAdvertisingParameters
50 \sa QLowEnergyController::startAdvertising()
51*/
52
53/*!
54 \enum QLowEnergyAdvertisingData::Discoverability
55
56 The discoverability of the advertising device as defined by the Generic Access Profile.
57
58 \value DiscoverabilityNone
59 The advertising device does not wish to be discoverable by scanning devices.
60 \value DiscoverabilityLimited
61 The advertising device wishes to be discoverable with a high priority. Note that this mode
62 is not compatible with using a white list. The value of
63 \l QLowEnergyAdvertisingParameters::filterPolicy() is always assumed to be
64 \l QLowEnergyAdvertisingParameters::IgnoreWhiteList when limited discoverability
65 is used.
66 \value DiscoverabilityGeneral
67 The advertising device wishes to be discoverable by scanning devices.
68 */
69
70/*!
71 Creates a new object of this class. All values are initialized to their defaults
72 according to the Bluetooth Low Energy specification.
73 */
74QLowEnergyAdvertisingData::QLowEnergyAdvertisingData() : d(new QLowEnergyAdvertisingDataPrivate)
75{
76}
77
78/*! Constructs a new object of this class that is a copy of \a other. */
79QLowEnergyAdvertisingData::QLowEnergyAdvertisingData(const QLowEnergyAdvertisingData &other)
80 : d(other.d)
81{
82}
83
84/*! Destroys this object. */
85QLowEnergyAdvertisingData::~QLowEnergyAdvertisingData()
86{
87}
88
89/*! Makes this object a copy of \a other and returns the new value of this object. */
90QLowEnergyAdvertisingData &QLowEnergyAdvertisingData::operator=(const QLowEnergyAdvertisingData &other)
91{
92 d = other.d;
93 return *this;
94}
95
96/*!
97 Specifies that \a name should be broadcast as the name of the device. If the full name does not
98 fit into the advertising data packet, an abbreviated name is sent, as described by the
99 Bluetooth Low Energy specification.
100
101 On Android, the local name cannot be changed. Android always uses the device name.
102 If this local name is not empty, the Android implementation includes the device name
103 in the advertisement packet; otherwise the device name is omitted from the advertisement
104 packet.
105
106 \sa localName()
107 */
108void QLowEnergyAdvertisingData::setLocalName(const QString &name)
109{
110 d->localName = name;
111}
112
113/*!
114 Returns the name of the local device that is to be advertised.
115
116 \sa setLocalName()
117 */
118QString QLowEnergyAdvertisingData::localName() const
119{
120 return d->localName;
121}
122
123/*!
124 Sets the manufacturer id and data. The \a id parameter is a company identifier as assigned
125 by the Bluetooth SIG. The \a data parameter is an arbitrary value.
126 */
127void QLowEnergyAdvertisingData::setManufacturerData(quint16 id, const QByteArray &data)
128{
129 d->manufacturerId = id;
130 d->manufacturerData = data;
131}
132
133/*!
134 Returns the manufacturer id.
135 The default is \l QLowEnergyAdvertisingData::invalidManufacturerId(), which means
136 the data will not be advertised.
137 */
138quint16 QLowEnergyAdvertisingData::manufacturerId() const
139{
140 return d->manufacturerId;
141}
142
143/*!
144 Returns the manufacturer data. The default is an empty byte array.
145 */
146QByteArray QLowEnergyAdvertisingData::manufacturerData() const
147{
148 return d->manufacturerData;
149}
150
151/*!
152 Specifies whether to include the device's transmit power level in the advertising data. If
153 \a doInclude is \c true, the data will be included, otherwise it will not.
154 */
155void QLowEnergyAdvertisingData::setIncludePowerLevel(bool doInclude)
156{
157 d->includePowerLevel = doInclude;
158}
159
160/*!
161 Returns whether to include the device's transmit power level in the advertising data.
162 The default is \c false.
163 */
164bool QLowEnergyAdvertisingData::includePowerLevel() const
165{
166 return d->includePowerLevel;
167}
168
169/*!
170 Sets the discoverability type of the advertising device to \a mode.
171 \note Discoverability information can only appear in an actual advertising data packet. If
172 this object acts as scan response data, a call to this function will have no effect
173 on the scan response sent.
174 */
175void QLowEnergyAdvertisingData::setDiscoverability(QLowEnergyAdvertisingData::Discoverability mode)
176{
177 d->discoverability = mode;
178}
179
180/*!
181 Returns the discoverability mode of the advertising device.
182 The default is \l DiscoverabilityNone.
183 */
184QLowEnergyAdvertisingData::Discoverability QLowEnergyAdvertisingData::discoverability() const
185{
186 return d->discoverability;
187}
188
189/*!
190 Specifies that the service UUIDs in \a services should be advertised.
191 If the entire list does not fit into the packet, an incomplete list is sent as specified
192 by the Bluetooth Low Energy specification.
193 */
194void QLowEnergyAdvertisingData::setServices(const QList<QBluetoothUuid> &services)
195{
196 d->services = services;
197}
198
199/*!
200 Returns the list of service UUIDs to be advertised.
201 By default, this list is empty.
202 */
203QList<QBluetoothUuid> QLowEnergyAdvertisingData::services() const
204{
205 return d->services;
206}
207
208/*!
209 Sets the data to be advertised to \a data. If the value is not an empty byte array, it will
210 be sent as-is as the advertising data and all other data in this object will be ignored.
211 This can be used to send non-standard data.
212 \note If \a data is longer than 31 bytes, it will be truncated. It is the caller's responsibility
213 to ensure that \a data is well-formed.
214
215 Providing the raw advertising data is not supported on BlueZ DBus backend as BlueZ does not
216 support it. This may change in a future release.
217 */
218void QLowEnergyAdvertisingData::setRawData(const QByteArray &data)
219{
220 d->rawData = data;
221}
222
223/*!
224 Returns the user-supplied raw data to be advertised. The default is an empty byte array.
225 */
226QByteArray QLowEnergyAdvertisingData::rawData() const
227{
228 return d->rawData;
229}
230
231/*!
232 \fn void QLowEnergyAdvertisingData::swap(QLowEnergyAdvertisingData &other)
233 Swaps this object with \a other.
234 */
235
236/*!
237 \brief Returns \c true if \a a and \a b are equal with respect to their public state,
238 otherwise returns \c false.
239 \internal
240 */
241bool QLowEnergyAdvertisingData::equals(const QLowEnergyAdvertisingData &a,
242 const QLowEnergyAdvertisingData &b)
243{
244 if (a.d == b.d)
245 return true;
246 return a.discoverability() == b.discoverability()
247 && a.includePowerLevel() == b.includePowerLevel() && a.localName() == b.localName()
248 && a.manufacturerData() == b.manufacturerData()
249 && a.manufacturerId() == b.manufacturerId() && a.services() == b.services()
250 && a.rawData() == b.rawData();
251}
252
253/*!
254 \fn bool QLowEnergyAdvertisingData::operator!=(const QLowEnergyAdvertisingData &data1,
255 const QLowEnergyAdvertisingData &data2)
256 \brief Returns \c true if \a data1 and \a data2 are not equal with respect to their
257 public state, otherwise returns \c false.
258 */
259
260/*!
261 \fn bool QLowEnergyAdvertisingData::operator==(const QLowEnergyAdvertisingData &data1,
262 const QLowEnergyAdvertisingData &data2)
263 \brief Returns \c true if \a data1 and \a data2 are equal with respect to their public
264 state, otherwise returns \c false.
265 */
266
267/*!
268 \fn static quint16 QLowEnergyAdvertisingData::invalidManufacturerId();
269 Returns an invalid manufacturer id. If this value is set as the manufacturer id
270 (which it is by default), no manufacturer data will be present in the advertising data.
271 */
272
273QT_END_NAMESPACE
274

source code of qtconnectivity/src/bluetooth/qlowenergyadvertisingdata.cpp