Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5#include "qbluetoothserver.h"
7#include "qbluetoothsocket.h"
9
11
138 : QObject(parent), d_ptr(new QBluetoothServerPrivate(serverType, this))
139{
140}
141
149
175{
176 Q_D(const QBluetoothServer);
177 if (!listen())
178 return QBluetoothServiceInfo();
180 QBluetoothServiceInfo serviceInfo;
181 serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, serviceName);
182 QBluetoothServiceInfo::Sequence browseSequence;
184 serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
185 browseSequence);
186
187 QBluetoothServiceInfo::Sequence profileSequence;
190 classId << QVariant::fromValue(quint16(0x100));
191 profileSequence.append(QVariant::fromValue(classId));
193 profileSequence);
194
195 classId.clear();
196 //Android requires custom uuid to be set as service class
197 classId << QVariant::fromValue(uuid);
199 serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
200 serviceInfo.setServiceUuid(uuid);
201
202 QBluetoothServiceInfo::Sequence protocolDescriptorList;
205 if (d->serverType == QBluetoothServiceInfo::L2capProtocol)
206 protocol << QVariant::fromValue(serverPort());
207 protocolDescriptorList.append(QVariant::fromValue(protocol));
208 protocol.clear();
210 if (d->serverType == QBluetoothServiceInfo::RfcommProtocol) {
214 protocolDescriptorList.append(QVariant::fromValue(protocol));
216 }
218 serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
219 protocolDescriptorList);
220 bool result = serviceInfo.registerService();
222 if (!result) {
223 close(); //close the still listening socket
224 return QBluetoothServiceInfo();
225 }
226 return serviceInfo;
227}
228
233{
234 Q_D(const QBluetoothServer);
235
236#if defined(QT_ANDROID_BLUETOOTH) || defined(QT_WINRT_BLUETOOTH) || defined(QT_OSX_BLUETOOTH)
237 return d->isListening();
238#endif
239
240 return d->socket->state() == QBluetoothSocket::SocketState::ListeningState;
241}
242
249{
250 Q_D(const QBluetoothServer);
251
252 return d->maxPendingConnections;
253}
254
279{
280 Q_D(const QBluetoothServer);
281 return d->serverType;
282}
283
289{
290 Q_D(const QBluetoothServer);
291 return d->m_lastError;
292}
293
295
296#include "moc_qbluetoothserver.cpp"
\inmodule QtBluetooth
int maxPendingConnections() const
Returns the maximum number of pending connections.
QBluetoothServer(QBluetoothServiceInfo::Protocol serverType, QObject *parent=nullptr)
Constructs a bluetooth server with parent and serverType.
Error
This enum describes Bluetooth server error types.
QBluetoothServerPrivate * d_ptr
bool isListening() const
Returns true if the server is listening for incoming connections, otherwise false.
bool listen(const QBluetoothAddress &address=QBluetoothAddress(), quint16 port=0)
Start listening for incoming connections to address on port.
void close()
Closes and resets the listening socket.
Error error() const
Returns the last error of the QBluetoothServer.
quint16 serverPort() const
Returns the server port number.
~QBluetoothServer()
Destroys the bluetooth server.
QBluetoothServiceInfo::Protocol serverType() const
Returns the type of the QBluetoothServer.
\inmodule QtBluetooth
void setAttribute(quint16 attributeId, const QVariant &value)
Sets the attribute identified by attributeId to value.
Protocol
This enum describes the socket protocol used by the service.
\inmodule QtBluetooth
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qobject.h:103
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
GLuint64EXT * result
[6]
unsigned short quint16
Definition qtypes.h:48
unsigned char quint8
Definition qtypes.h:46
socketLayer listen()