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")
20 @SuppressWarnings({
"WeakerAccess",
"CanBeFinal"})
22 @SuppressWarnings({
"WeakerAccess",
"CanBeFinal"})
23 public boolean logEnabled =
false;
24 @SuppressWarnings(
"WeakerAccess")
25 static Context qtContext = null;
27 private static final String
TAG = "QtBluetooth";
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");
46 m_uuid = UUID.fromString(uuid);
47 m_serviceName = serviceName;
48 m_isSecure = isSecure;
55 (BluetoothManager)qtContext.getSystemService(Context.BLUETOOTH_SERVICE);
58 errorOccurred(qtObject, QT_NO_BLUETOOTH_SUPPORTED);
63 if (adapter ==
null) {
64 errorOccurred(qtObject, QT_NO_BLUETOOTH_SUPPORTED);
70 m_serverSocket = adapter.listenUsingRfcommWithServiceRecord(m_serviceName, m_uuid);
72 Log.d(
TAG,
"Using secure socket listener");
74 m_serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord(m_serviceName, m_uuid);
76 Log.d(
TAG,
"Using insecure socket listener");
78 }
catch (IOException ex) {
80 Log.d(
TAG,
"Server socket listen() failed:" + ex.toString());
82 errorOccurred(qtObject, QT_LISTEN_FAILED);
90 if (m_serverSocket !=
null) {
92 while (!isInterrupted()) {
96 Log.d(
TAG,
"Waiting for new incoming socket");
97 s = m_serverSocket.accept();
100 Log.d(
TAG,
"New socket accepted");
101 newSocket(qtObject,
s);
103 }
catch (IOException ex) {
105 Log.d(
TAG,
"Server socket accept() failed:" + ex.toString());
106 ex.printStackTrace();
107 errorOccurred(qtObject, QT_ACCEPT_FAILED);
111 Log.d(
TAG,
"Leaving server socket thread.");
142 if (m_serverSocket !=
null)
143 m_serverSocket.close();
146 }
catch (Exception ex) {
147 Log.d(
TAG,
"Closing server socket close() failed:" + ex.toString());
148 ex.printStackTrace();
void setServiceDetails(String uuid, String serviceName, boolean isSecure)
static native void newSocket(long qtObject, BluetoothSocket socket)
static native void errorOccurred(long qtObject, int errorCode)
employee setName("Richard Schmit")
QNetworkAccessManager manager