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
qbluetoothserver.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 BlackBerry Limited. All rights reserved.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
9
11
12/*!
13 \class QBluetoothServer
14 \inmodule QtBluetooth
15 \brief The QBluetoothServer class uses the RFCOMM or L2cap protocol to communicate with
16 a Bluetooth device.
17
18 \since 5.2
19
20 QBluetoothServer is used to implement Bluetooth services over RFCOMM or L2cap.
21
22 Start listening for incoming connections with listen(). Wait till the newConnection() signal
23 is emitted when a new connection is established, and call nextPendingConnection() to get a QBluetoothSocket
24 for the new connection.
25
26 To enable other devices to find your service, create a QBluetoothServiceInfo with the
27 applicable attributes for your service and register it using QBluetoothServiceInfo::registerService().
28 Call serverPort() to get the channel number that is being used.
29
30 If the \l QBluetoothServiceInfo::Protocol is not supported by a platform, \l listen() will return \c false.
31 Android, HarmonyOS and WinRT only support RFCOMM for example.
32
33 On iOS, this class cannot be used because the platform does not expose
34 an API which may permit access to QBluetoothServer related features.
35
36 \sa QBluetoothServiceInfo, QBluetoothSocket
37*/
38
39/*!
40 \fn void QBluetoothServer::newConnection()
41
42 This signal is emitted when a new connection is available.
43
44 The connected slot should call nextPendingConnection() to get a QBluetoothSocket object to
45 send and receive data over the connection.
46
47 \sa nextPendingConnection(), hasPendingConnections()
48*/
49
50/*!
51 \fn void QBluetoothServer::errorOccurred(QBluetoothServer::Error error)
52
53 This signal is emitted when an \a error occurs.
54
55 \sa error(), QBluetoothServer::Error
56 \since 6.2
57*/
58
59/*!
60 \fn void QBluetoothServer::close()
61
62 Closes and resets the listening socket. Any already established \l QBluetoothSocket
63 continues to operate and must be separately \l {QBluetoothSocket::close()}{closed}.
64*/
65
66/*!
67 \enum QBluetoothServer::Error
68
69 This enum describes Bluetooth server error types.
70
71 \value NoError No error.
72 \value UnknownError An unknown error occurred.
73 \value PoweredOffError The Bluetooth adapter is powered off.
74 \value InputOutputError An input output error occurred.
75 \value ServiceAlreadyRegisteredError The service or port was already registered
76 \value UnsupportedProtocolError The \l {QBluetoothServiceInfo::Protocol}{Protocol} is not
77 supported on this platform.
78 \value [since 6.4] MissingPermissionsError The operating system requests
79 permissions which were not
80 granted by the user.
81*/
82
83/*!
84 \fn bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port)
85
86 Start listening for incoming connections to \a address on \a port. \a address
87 must be a local Bluetooth adapter address and \a port must be larger than zero
88 and not be taken already by another Bluetooth server object. It is recommended
89 to avoid setting a port number to enable the system to automatically choose
90 a port.
91
92 Returns \c true if the operation succeeded and the server is listening for
93 incoming connections, otherwise returns \c false.
94
95 If the server object is already listening for incoming connections this function
96 always returns \c false. \l close() should be called before calling this function.
97
98 \note On HarmonyOS the system service is registered by
99 \l QBluetoothServiceInfo::registerService(), which supplies the service
100 name and UUID this overload does not carry. Until then the server accepts
101 no connection, although \l isListening() already reports \c true. The
102 \l {QBluetoothServer::}{listen(const QBluetoothUuid &, const QString &)}
103 overload registers the service itself and is unaffected. \l serverAddress()
104 returns a null address, and \l serverPort() a key used only to identify
105 this server locally, because the platform exposes neither.
106
107 \sa isListening(), newConnection()
108*/
109
110/*!
111 \fn void QBluetoothServer::setMaxPendingConnections(int numConnections)
112
113 Sets the maximum number of pending connections to \a numConnections. If
114 the number of pending sockets exceeds this limit new sockets will be rejected.
115
116 \sa maxPendingConnections()
117*/
118
119/*!
120 \fn bool QBluetoothServer::hasPendingConnections() const
121 Returns true if a connection is pending, otherwise false.
122*/
123
124/*!
125 \fn QBluetoothSocket *QBluetoothServer::nextPendingConnection()
126
127 Returns a pointer to the QBluetoothSocket for the next pending connection. It is the callers
128 responsibility to delete the pointer.
129*/
130
131/*!
132 \fn QBluetoothAddress QBluetoothServer::serverAddress() const
133
134 Returns the server address.
135*/
136
137/*!
138 \fn quint16 QBluetoothServer::serverPort() const
139
140 Returns the server port number.
141*/
142
143/*!
144 Constructs a bluetooth server with \a parent and \a serverType.
145*/
146QBluetoothServer::QBluetoothServer(QBluetoothServiceInfo::Protocol serverType, QObject *parent)
147 : QObject(parent), d_ptr(new QBluetoothServerPrivate(serverType, this))
148{
149}
150
151/*!
152 Destroys the bluetooth server.
153*/
154QBluetoothServer::~QBluetoothServer()
155{
156 delete d_ptr;
157}
158
159/*!
160 \fn QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const QString &serviceName)
161
162 Convenience function for registering an SPP service with \a uuid and \a serviceName.
163 Because this function already registers the service, the QBluetoothServiceInfo object
164 which is returned can not be changed any more. To shutdown the server later on it is
165 required to call \l QBluetoothServiceInfo::unregisterService() and \l close() on this
166 server object.
167
168 Returns a registered QBluetoothServiceInfo instance if successful otherwise an
169 invalid QBluetoothServiceInfo. This function always assumes that the default Bluetooth adapter
170 should be used.
171
172 If the server object is already listening for incoming connections this function
173 returns an invalid \l QBluetoothServiceInfo.
174
175 For an RFCOMM server this function is equivalent to following code snippet.
176
177 \snippet qbluetoothserver.cpp listen
178 \snippet qbluetoothserver.cpp listen2
179 \snippet qbluetoothserver.cpp listen3
180
181 \sa isListening(), newConnection(), listen()
182*/
183QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const QString &serviceName)
184{
185 Q_D(const QBluetoothServer);
186 if (!listen())
187 return QBluetoothServiceInfo();
188//! [listen]
189 QBluetoothServiceInfo serviceInfo;
190 serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, serviceName);
191 QBluetoothServiceInfo::Sequence browseSequence;
192 browseSequence << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::PublicBrowseGroup));
193 serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
194 browseSequence);
195
196 QBluetoothServiceInfo::Sequence profileSequence;
197 QBluetoothServiceInfo::Sequence classId;
198 classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::SerialPort));
199 classId << QVariant::fromValue(quint16(0x100));
200 profileSequence.append(QVariant::fromValue(classId));
201 serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,
202 profileSequence);
203
204 classId.clear();
205 //Android requires custom uuid to be set as service class
206 classId << QVariant::fromValue(uuid);
207 classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::SerialPort));
208 serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
209 serviceInfo.setServiceUuid(uuid);
210
211 QBluetoothServiceInfo::Sequence protocolDescriptorList;
212 QBluetoothServiceInfo::Sequence protocol;
213 protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::L2cap));
214 if (d->serverType == QBluetoothServiceInfo::L2capProtocol)
215 protocol << QVariant::fromValue(serverPort());
216 protocolDescriptorList.append(QVariant::fromValue(protocol));
217 protocol.clear();
218//! [listen]
219 if (d->serverType == QBluetoothServiceInfo::RfcommProtocol) {
220//! [listen2]
221 protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::Rfcomm))
222 << QVariant::fromValue(quint8(serverPort()));
223 protocolDescriptorList.append(QVariant::fromValue(protocol));
224//! [listen2]
225 }
226//! [listen3]
227 serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
228 protocolDescriptorList);
229 bool result = serviceInfo.registerService();
230//! [listen3]
231 if (!result) {
232 close(); //close the still listening socket
233 return QBluetoothServiceInfo();
234 }
235 return serviceInfo;
236}
237
238/*!
239 Returns true if the server is listening for incoming connections, otherwise false.
240*/
241bool QBluetoothServer::isListening() const
242{
243 Q_D(const QBluetoothServer);
244
245#if defined(QT_ANDROID_BLUETOOTH) || defined(QT_WINRT_BLUETOOTH) || defined(QT_OSX_BLUETOOTH) || defined(QT_HARMONY_BLUETOOTH)
246 return d->isListening();
247#endif
248
249 return d->socket->state() == QBluetoothSocket::SocketState::ListeningState;
250}
251
252/*!
253 Returns the maximum number of pending connections.
254
255 \sa setMaxPendingConnections()
256*/
257int QBluetoothServer::maxPendingConnections() const
258{
259 Q_D(const QBluetoothServer);
260
261 return d->maxPendingConnections;
262}
263
264/*!
265 \fn QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security)
266 Sets the Bluetooth security flags to \a security. This function must be called
267 before calling listen(). The Bluetooth link will always be encrypted when using
268 Bluetooth 2.1 devices as encryption is mandatory.
269
270//! [bluetooth_security_bluez]
271 On the Linux BlueZ backend, the flags are mapped to kernel security levels:
272 \list
273 \li \l QBluetooth::Security::Authorization maps to \c BT_SECURITY_LOW;
274 \li \l QBluetooth::Security::Encryption maps to \c BT_SECURITY_MEDIUM;
275 \li \l QBluetooth::Security::Secure maps to \c BT_SECURITY_HIGH;
276 \li \l QBluetooth::Security::Authentication is not used by the kernel
277 and \b {is ignored}.
278 \endlist
279 The default value is \l QBluetooth::Security::Authorization.
280//! [bluetooth_security_bluez]
281
282 Android only supports two levels of security (secure and non-secure). If this flag
283 is set to \l QBluetooth::Security::NoSecurity the server object will not employ
284 any authentication or encryption. Any other security flag combination will
285 trigger a secure Bluetooth connection.
286
287 On \macos and Windows, security flags are not supported and will be ignored.
288*/
289
290/*!
291 \fn QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const
292 Returns the Bluetooth security flags.
293*/
294
295/*!
296 \fn QBluetoothSocket::ServerType QBluetoothServer::serverType() const
297 Returns the type of the QBluetoothServer.
298*/
299QBluetoothServiceInfo::Protocol QBluetoothServer::serverType() const
300{
301 Q_D(const QBluetoothServer);
302 return d->serverType;
303}
304
305/*!
306 \fn QBluetoothServer::Error QBluetoothServer::error() const
307 Returns the last error of the QBluetoothServer.
308*/
309QBluetoothServer::Error QBluetoothServer::error() const
310{
311 Q_D(const QBluetoothServer);
312 return d->m_lastError;
313}
314
315QT_END_NAMESPACE
316
317#include "moc_qbluetoothserver.cpp"
Combined button and popup list for selecting options.