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_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
4#include "android/serveracceptancethread_p.h"
5#include "android/androidutils_p.h"
6#include "android/jni_android_p.h"
12
13#include <QCoreApplication>
14#include <QtCore/QLoggingCategory>
15
16QT_BEGIN_NAMESPACE
17
18Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID)
19
20QHash<QBluetoothServerPrivate*, int> __fakeServerPorts;
21
22QBluetoothServerPrivate::QBluetoothServerPrivate(QBluetoothServiceInfo::Protocol sType,
23 QBluetoothServer *parent)
24 : serverType(sType), q_ptr(parent)
25{
26 thread = new ServerAcceptanceThread();
27 thread->setMaxPendingConnections(maxPendingConnections);
28}
29
31{
32 Q_Q(QBluetoothServer);
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
80void QBluetoothServer::close()
81{
82 Q_D(QBluetoothServer);
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
92bool QBluetoothServer::listen(const QBluetoothAddress &localAdapter, quint16 port)
93{
94 Q_D(QBluetoothServer);
95 if (serverType() != QBluetoothServiceInfo::RfcommProtocol) {
96 d->m_lastError = UnsupportedProtocolError;
97 emit errorOccurred(d->m_lastError);
98 return false;
99 }
100
101 if (!ensureAndroidPermission(QBluetoothPermission::Access)) {
102 qCWarning(QT_BT_ANDROID) << "Bluetooth server listen() failed due to missing permissions";
103 d->m_lastError = QBluetoothServer::MissingPermissionsError;
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
135 QJniObject btAdapter = getDefaultBluetoothAdapter();
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
146 d->m_lastError = QBluetoothServer::PoweredOffError;
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) {
164 __fakeServerPorts[d] = port;
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()));
176 connect(d->thread, SIGNAL(errorOccurred(QBluetoothServer::Error)), this,
177 SIGNAL(errorOccurred(QBluetoothServer::Error)), Qt::QueuedConnection);
178
179 return true;
180}
181
182void QBluetoothServer::setMaxPendingConnections(int numConnections)
183{
184 Q_D(QBluetoothServer);
185 d->maxPendingConnections = numConnections;
186 d->thread->setMaxPendingConnections(numConnections);
187}
188
189QBluetoothAddress QBluetoothServer::serverAddress() const
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
201quint16 QBluetoothServer::serverPort() const
202{
203 //We return the fake port
204 Q_D(const QBluetoothServer);
205 return __fakeServerPorts.value((QBluetoothServerPrivate*)d, 0);
206}
207
208bool QBluetoothServer::hasPendingConnections() const
209{
210 Q_D(const QBluetoothServer);
211
212 return d->thread->hasPendingConnections();
213}
214
215QBluetoothSocket *QBluetoothServer::nextPendingConnection()
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
246QT_END_NAMESPACE