4package org.qtproject.qt.android.bluetooth;
6import android.bluetooth.BluetoothAdapter;
7import android.bluetooth.BluetoothServerSocket;
8import android.bluetooth.BluetoothManager;
9import android.bluetooth.BluetoothSocket;
10import android.content.Context;
11import android.util.Log;
12import java.io.IOException;
15@SuppressWarnings(
"WeakerAccess")
16class QtBluetoothSocketServer extends Thread
20 @SuppressWarnings({
"WeakerAccess",
"CanBeFinal"})
22 @SuppressWarnings({
"WeakerAccess",
"CanBeFinal"})
23 boolean logEnabled =
false;
24 @SuppressWarnings(
"WeakerAccess")
25 static
Context qtContext = null;
28 private boolean m_isSecure = false;
31 private BluetoothServerSocket m_serverSocket = null;
34 private static final
int QT_NO_BLUETOOTH_SUPPORTED = 0;
35 private static final
int QT_LISTEN_FAILED = 1;
36 private static final
int QT_ACCEPT_FAILED = 2;
41 setName(
"QtSocketServerThread");
44 void setServiceDetails(
String uuid,
String serviceName,
boolean isSecure)
46 m_uuid = UUID.fromString(uuid);
47 m_serviceName = serviceName;
48 m_isSecure = isSecure;
56 (BluetoothManager)qtContext.getSystemService(
Context.BLUETOOTH_SERVICE);
59 errorOccurred(qtObject, QT_NO_BLUETOOTH_SUPPORTED);
64 if (adapter ==
null) {
65 errorOccurred(qtObject, QT_NO_BLUETOOTH_SUPPORTED);
71 m_serverSocket = adapter.listenUsingRfcommWithServiceRecord(m_serviceName, m_uuid);
73 Log.d(
TAG,
"Using secure socket listener");
75 m_serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord(m_serviceName, m_uuid);
77 Log.d(
TAG,
"Using insecure socket listener");
79 }
catch (IOException ex) {
81 Log.d(
TAG,
"Server socket listen() failed:" + ex.toString());
83 errorOccurred(qtObject, QT_LISTEN_FAILED);
91 if (m_serverSocket !=
null) {
93 while (!isInterrupted()) {
97 Log.d(
TAG,
"Waiting for new incoming socket");
98 s = m_serverSocket.accept();
101 Log.d(
TAG,
"New socket accepted");
102 newSocket(qtObject,
s);
104 }
catch (IOException ex) {
106 Log.d(
TAG,
"Server socket accept() failed:" + ex.toString());
107 ex.printStackTrace();
108 errorOccurred(qtObject, QT_ACCEPT_FAILED);
112 Log.d(
TAG,
"Leaving server socket thread.");
143 if (m_serverSocket !=
null)
144 m_serverSocket.close();
147 }
catch (Exception ex) {
148 Log.d(
TAG,
"Closing server socket close() failed:" + ex.toString());
149 ex.printStackTrace();
153 static native
void errorOccurred(
long qtObject,
int errorCode);
154 static native
void newSocket(
long qtObject, BluetoothSocket
socket);
static const QString context()
QTCONCURRENT_RUN_NODISCARD auto run(QThreadPool *pool, Function &&f, Args &&...args)
QNetworkAccessManager manager
[0]