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_android.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
7#include "qbluetoothserver.h"
9#include "qbluetoothsocket.h"
12
13#include <QCoreApplication>
14#include <QtCore/QLoggingCategory>
15
17
18Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID)
19
21
23 QBluetoothServer *parent)
24 : serverType(sType), q_ptr(parent)
25{
26 thread = new ServerAcceptanceThread();
27 thread->setMaxPendingConnections(maxPendingConnections);
28}
29
31{
33 if (isListening())
34 q->close();
35
36 __fakeServerPorts.remove(this);
37
38 thread->deleteLater();
39 thread = nullptr;
40}
41
42bool QBluetoothServerPrivate::initiateActiveListening(
43 const QBluetoothUuid& uuid, const QString &serviceName)
44{
45 qCDebug(QT_BT_ANDROID) << "Initiate active listening" << uuid.toString() << serviceName;
46
47 if (uuid.isNull() || serviceName.isEmpty())
48 return false;
49
50 //no change of SP profile details -> do nothing
51 if (uuid == m_uuid && serviceName == m_serviceName && thread->isRunning())
52 return true;
53
54 m_uuid = uuid;
55 m_serviceName = serviceName;
56 thread->setServiceDetails(m_uuid, m_serviceName, securityFlags);
57
58 thread->run();
59 if (!thread->isRunning())
60 return false;
61
62 return true;
63}
64
65bool QBluetoothServerPrivate::deactivateActiveListening()
66{
67 if (isListening()) {
68 //suppress last error signal due to intended closure
69 thread->disconnect();
70 thread->stop();
71 }
72 return true;
73}
74
75bool QBluetoothServerPrivate::isListening() const
76{
77 return __fakeServerPorts.contains(const_cast<QBluetoothServerPrivate *>(this));
78}
79
81{
83
84 __fakeServerPorts.remove(d);
85 if (d->thread->isRunning()) {
86 //suppress last error signal due to intended closure
87 d->thread->disconnect();
88 d->thread->stop();
89 }
90}
91
93{
96 d->m_lastError = UnsupportedProtocolError;
97 emit errorOccurred(d->m_lastError);
98 return false;
99 }
100
102 qCWarning(QT_BT_ANDROID) << "Bluetooth server listen() failed due to missing permissions";
104 emit errorOccurred(d->m_lastError);
105 return false;
106 }
107
108 const QList<QBluetoothHostInfo> localDevices = QBluetoothLocalDevice::allDevices();
109 if (localDevices.isEmpty()) {
110 qCWarning(QT_BT_ANDROID) << "Device does not support Bluetooth";
111 d->m_lastError = QBluetoothServer::UnknownError;
112 emit errorOccurred(d->m_lastError);
113 return false; //no Bluetooth device
114 }
115
116 if (!localAdapter.isNull()) {
117 bool found = false;
118 for (const QBluetoothHostInfo &hostInfo : localDevices) {
119 if (hostInfo.address() == localAdapter) {
120 found = true;
121 break;
122 }
123 }
124
125 if (!found) {
126 qCWarning(QT_BT_ANDROID) << localAdapter.toString() << "is not a valid local Bt adapter";
127 return false;
128 }
129 }
130
131 if (d->isListening())
132 return false;
133
134 //check Bluetooth is available and online
136
137 if (!btAdapter.isValid()) {
138 qCWarning(QT_BT_ANDROID) << "Device does not support Bluetooth";
139 d->m_lastError = QBluetoothServer::UnknownError;
140 emit errorOccurred(d->m_lastError);
141 return false;
142 }
143
144 const int state = btAdapter.callMethod<jint>("getState");
145 if (state != 12 ) { //BluetoothAdapter.STATE_ON
147 emit errorOccurred(d->m_lastError);
148 qCWarning(QT_BT_ANDROID) << "Bluetooth device is powered off";
149 return false;
150 }
151
152 //We can not register an actual Rfcomm port, because the platform does not allow it
153 //but we need a way to associate a server with a service
154 if (port == 0) { //Try to assign a non taken port id
155 for (int i=1; ; i++){
156 if (__fakeServerPorts.key(i) == 0) {
157 port = i;
158 break;
159 }
160 }
161 }
162
163 if (__fakeServerPorts.key(port) == 0) {
165
166 qCDebug(QT_BT_ANDROID) << "Port" << port << "registered";
167 } else {
168 qCWarning(QT_BT_ANDROID) << "server with port" << port << "already registered or port invalid";
169 d->m_lastError = ServiceAlreadyRegisteredError;
170 emit errorOccurred(d->m_lastError);
171 return false;
172 }
173
174 connect(d->thread, SIGNAL(newConnection()),
175 this, SIGNAL(newConnection()));
178
179 return true;
180}
181
183{
184 Q_D(QBluetoothServer);
185 d->maxPendingConnections = numConnections;
186 d->thread->setMaxPendingConnections(numConnections);
187}
188
190{
191 //Android only supports one local adapter
192 QList<QBluetoothHostInfo> hosts = QBluetoothLocalDevice::allDevices();
193 Q_ASSERT(hosts.size() <= 1);
194
195 if (hosts.isEmpty())
196 return QBluetoothAddress();
197 else
198 return hosts.at(0).address();
199}
200
202{
203 //We return the fake port
204 Q_D(const QBluetoothServer);
206}
207
209{
210 Q_D(const QBluetoothServer);
211
212 return d->thread->hasPendingConnections();
213}
214
216{
217 Q_D(const QBluetoothServer);
218
219 QJniObject socket = d->thread->nextPendingConnection();
220 if (!socket.isValid())
221 return 0;
222
223
224 QBluetoothSocket *newSocket = new QBluetoothSocket();
225 bool success = newSocket->d_ptr->setSocketDescriptor(socket, d->serverType);
226 if (!success) {
227 delete newSocket;
228 newSocket = nullptr;
229 }
230
231 return newSocket;
232}
233
234void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security)
235{
236 Q_D(QBluetoothServer);
237 d->securityFlags = security;
238}
239
240QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const
241{
242 Q_D(const QBluetoothServer);
243 return d->securityFlags;
244}
245
247
QJniObject getDefaultBluetoothAdapter()
QT_BEGIN_NAMESPACE bool ensureAndroidPermission(QBluetoothPermission::CommunicationModes modes)
bool isValid() const
Returns true if the socket is valid and ready for use; otherwise returns false.
\inmodule QtBluetooth
\inmodule QtBluetooth
static QList< QBluetoothHostInfo > allDevices()
Returns a list of all available local Bluetooth devices.
QBluetooth::SecurityFlags securityFlags
\inmodule QtBluetooth
bool hasPendingConnections() const
Returns true if a connection is pending, otherwise false.
void setSecurityFlags(QBluetooth::SecurityFlags security)
Sets the Bluetooth security flags to security.
Error
This enum describes Bluetooth server error types.
QBluetoothSocket * nextPendingConnection()
Returns a pointer to the QBluetoothSocket for the next pending connection.
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.
void errorOccurred(QBluetoothServer::Error error)
This signal is emitted when an error occurs.
quint16 serverPort() const
Returns the server port number.
QBluetoothAddress serverAddress() const
Returns the server address.
QBluetooth::SecurityFlags securityFlags() const
Returns the Bluetooth security flags.
void newConnection()
This signal is emitted when a new connection is available.
void setMaxPendingConnections(int numConnections)
Sets the maximum number of pending connections to numConnections.
QBluetoothServiceInfo::Protocol serverType() const
Returns the type of the QBluetoothServer.
\inmodule QtBluetooth
virtual bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState=QBluetoothSocket::SocketState::ConnectedState, QBluetoothSocket::OpenMode openMode=QBluetoothSocket::ReadWrite)=0
\inmodule QtBluetooth
QBluetoothSocketBasePrivate * d_ptr
\inmodule QtBluetooth
\inmodule QtCore
Definition qhash.h:820
\inmodule QtCore
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QThread * thread() const
Returns the thread in which the object lives.
Definition qobject.cpp:1598
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QString toString(StringFormat mode=WithBraces) const
Definition quuid.cpp:650
bool isNull() const noexcept
Returns true if this is the null UUID {00000000-0000-0000-0000-000000000000}; otherwise returns false...
Definition quuid.cpp:818
else opt state
[0]
Combined button and popup list for selecting options.
@ QueuedConnection
QT_BEGIN_NAMESPACE QHash< QBluetoothServerPrivate *, int > __fakeServerPorts
EGLOutputPortEXT port
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define emit
unsigned short quint16
Definition qtypes.h:48
QTcpSocket * socket
[1]