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 "qlowenergyconnectionparameters.h"
5
6QT_BEGIN_NAMESPACE
7
8QT_IMPL_METATYPE_EXTERN(QLowEnergyConnectionParameters)
9
10class QLowEnergyConnectionParametersPrivate : public QSharedData
11{
12public:
13 QLowEnergyConnectionParametersPrivate()
14 : minInterval(7.5)
15 , maxInterval(4000)
16 , latency(0)
17 , timeout(32000)
18 {
19 }
20
21 double minInterval;
22 double maxInterval;
23 int latency;
24 int timeout;
25};
26
27/*!
28 \since 5.7
29 \class QLowEnergyConnectionParameters
30 \brief The QLowEnergyConnectionParameters class is used when requesting or reporting
31 an update of the parameters of a Bluetooth LE connection.
32
33 The connection parameters influence how often a master and a slave device synchronize
34 with each other. In general, a lower connection interval and latency means faster communication,
35 but also higher power consumption. How these criteria should be weighed against each other
36 is highly dependent on the concrete use case.
37
38 Android only indirectly permits the adjustment of this parameter set.
39 The platform separates the connection parameters into three categories (hight, low & balanced
40 priority). Each category implies a predefined set of values for \l minimumInterval(),
41 \l maximumInterval() and \l latency(). Additionally, the value ranges of each category can vary
42 from one Android device to the next. Qt uses the \l minimumInterval() to determine the target
43 category as follows:
44
45 \table
46 \header
47 \li minimumInterval()
48 \li Android priority
49 \row
50 \li interval < 30
51 \li CONNECTION_PRIORITY_HIGH
52 \row
53 \li 30 <= interval <= 100
54 \li CONNECTION_PRIORITY_BALANCED
55 \row
56 \li interval > 100
57 \li CONNECTION_PRIORITY_LOW_POWER
58 \endtable
59
60 The \l supervisionTimeout() cannot be changed on Android and is therefore ignored.
61
62
63 \inmodule QtBluetooth
64 \ingroup shared
65
66 \sa QLowEnergyController::requestConnectionUpdate
67 \sa QLowEnergyController::connectionUpdated
68*/
69
70
71/*!
72 Constructs a new object of this class. All values are initialized to valid defaults.
73 */
74QLowEnergyConnectionParameters::QLowEnergyConnectionParameters()
75 : d(new QLowEnergyConnectionParametersPrivate)
76{
77}
78
79/*! Constructs a new object of this class that is a copy of \a other. */
80QLowEnergyConnectionParameters::QLowEnergyConnectionParameters(const QLowEnergyConnectionParameters &other)
81 : d(other.d)
82{
83}
84
85/*! Destroys this object. */
86QLowEnergyConnectionParameters::~QLowEnergyConnectionParameters()
87{
88}
89
90/*! Makes this object a copy of \a other and returns the new value of this object. */
91QLowEnergyConnectionParameters &QLowEnergyConnectionParameters::operator=(const QLowEnergyConnectionParameters &other)
92{
93 d = other.d;
94 return *this;
95}
96
97/*!
98 Sets the range in which the connection interval should be. The actual value will be decided by
99 the controller. Both \a minimum and \a maximum are given in milliseconds.
100 If \a maximum is smaller than \a minimum, it will be set to the value of \a minimum.
101 The smallest possible connection interval is 7.5 milliseconds, the largest one is
102 4000 milliseconds.
103 \sa minimumInterval(), maximumInterval()
104 */
105void QLowEnergyConnectionParameters::setIntervalRange(double minimum, double maximum)
106{
107 d->minInterval = minimum;
108 d->maxInterval = qMax(a: minimum, b: maximum);
109}
110
111/*!
112 Returns the minimum connection interval in milliseconds. The default is 7.5.
113 \note If this object was emitted via \l QLowEnergyController::connectionUpdated(), then
114 this value is the same as \l maximumInterval() and refers to the actual
115 connection interval.
116 \sa setIntervalRange()
117 */
118double QLowEnergyConnectionParameters::minimumInterval() const
119{
120 return d->minInterval;
121}
122
123/*!
124 Returns the maximum connection interval in milliseconds. The default is 4000.
125 \note If this object was emitted via \l QLowEnergyController::connectionUpdated(), then
126 this value is the same as \l minimumInterval() and refers to the actual
127 connection interval.
128 \sa setIntervalRange()
129 */
130double QLowEnergyConnectionParameters::maximumInterval() const
131{
132 return d->maxInterval;
133}
134
135/*!
136 Sets the slave latency of the connection (that is, the number of connection events that a slave
137 device is allowed to ignore) to \a latency. The minimum value is 0, the maximum is 499.
138 \sa latency()
139 */
140void QLowEnergyConnectionParameters::setLatency(int latency)
141{
142 d->latency = latency;
143}
144
145/*!
146 Returns the slave latency of the connection.
147 \sa setLatency()
148*/
149int QLowEnergyConnectionParameters::latency() const
150{
151 return d->latency;
152}
153
154/*!
155 Sets the link supervision timeout to \a timeout milliseconds.
156 There are several constraints on this value: It must be in the range [100,32000] and it must be
157 larger than (1 + \l latency()) * 2 * \l maximumInterval().
158
159 On Android, this timeout is not adjustable and therefore ignored.
160
161 \sa supervisionTimeout()
162 */
163void QLowEnergyConnectionParameters::setSupervisionTimeout(int timeout)
164{
165 d->timeout = timeout;
166}
167
168/*!
169 Returns the link supervision timeout of the connection in milliseconds.
170 \sa setSupervisionTimeout()
171*/
172int QLowEnergyConnectionParameters::supervisionTimeout() const
173{
174 return d->timeout;
175}
176
177/*!
178 \fn void QLowEnergyConnectionParameters::swap(QLowEnergyConnectionParameters &other)
179 Swaps this object with \a other.
180 */
181
182/*!
183 \brief Returns \a true if \a a and \a b are equal with respect to their public state,
184 otherwise returns false.
185 \internal
186 */
187bool QLowEnergyConnectionParameters::equals(const QLowEnergyConnectionParameters &a,
188 const QLowEnergyConnectionParameters &b)
189{
190 if (a.d == b.d)
191 return true;
192 return a.minimumInterval() == b.minimumInterval() && a.maximumInterval() == b.maximumInterval()
193 && a.latency() == b.latency() && a.supervisionTimeout() == b.supervisionTimeout();
194}
195
196/*!
197 \fn bool QLowEnergyConnectionParameters::operator!=(
198 const QLowEnergyConnectionParameters &p1,
199 const QLowEnergyConnectionParameters &p2)
200 \brief Returns \c true if \a p1 and \a p2 are not equal with respect to their public state,
201 otherwise returns \c false.
202 */
203
204/*!
205 \fn bool QLowEnergyConnectionParameters::operator==(
206 const QLowEnergyConnectionParameters &p1,
207 const QLowEnergyConnectionParameters &p2)
208 \brief Returns \c true if \a p1 and \a p2 are equal with respect to their public state,
209 otherwise returns \c false.
210 */
211
212QT_END_NAMESPACE
213

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