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
QtBluetoothSocketServer.java
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
4package org.qtproject.qt.android.bluetooth;
5
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;
13import java.util.UUID;
14
15@SuppressWarnings("WeakerAccess")
16class QtBluetoothSocketServer extends Thread
17{
18
19 /* Pointer to the Qt object that "owns" the Java object */
20 @SuppressWarnings({"WeakerAccess", "CanBeFinal"})
21 long qtObject = 0;
22 @SuppressWarnings({"WeakerAccess", "CanBeFinal"})
23 boolean logEnabled = false;
24 @SuppressWarnings("WeakerAccess")
25 static Context qtContext = null;
26
27 private static final String TAG = "QtBluetooth";
28 private boolean m_isSecure = false;
29 private UUID m_uuid;
30 private String m_serviceName;
31 private BluetoothServerSocket m_serverSocket = null;
32
33 //error codes
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;
37
38 QtBluetoothSocketServer(Context context)
39 {
40 qtContext = context;
41 setName("QtSocketServerThread");
42 }
43
44 void setServiceDetails(String uuid, String serviceName, boolean isSecure)
45 {
46 m_uuid = UUID.fromString(uuid);
47 m_serviceName = serviceName;
48 m_isSecure = isSecure;
49
50 }
51
52 @Override
53 public void run()
54 {
55 BluetoothManager manager =
56 (BluetoothManager)qtContext.getSystemService(Context.BLUETOOTH_SERVICE);
57
58 if (manager == null) {
59 errorOccurred(qtObject, QT_NO_BLUETOOTH_SUPPORTED);
60 return;
61 }
62
63 BluetoothAdapter adapter = manager.getAdapter();
64 if (adapter == null) {
65 errorOccurred(qtObject, QT_NO_BLUETOOTH_SUPPORTED);
66 return;
67 }
68
69 try {
70 if (m_isSecure) {
71 m_serverSocket = adapter.listenUsingRfcommWithServiceRecord(m_serviceName, m_uuid);
72 if (logEnabled)
73 Log.d(TAG, "Using secure socket listener");
74 } else {
75 m_serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord(m_serviceName, m_uuid);
76 if (logEnabled)
77 Log.d(TAG, "Using insecure socket listener");
78 }
79 } catch (IOException ex) {
80 if (logEnabled)
81 Log.d(TAG, "Server socket listen() failed:" + ex.toString());
82 ex.printStackTrace();
83 errorOccurred(qtObject, QT_LISTEN_FAILED);
84 return;
85 }
86
87 if (isInterrupted()) // close() may have been called
88 return;
89
90 BluetoothSocket s;
91 if (m_serverSocket != null) {
92 try {
93 while (!isInterrupted()) {
94 //this blocks until we see incoming connection
95 //or close() is called
96 if (logEnabled)
97 Log.d(TAG, "Waiting for new incoming socket");
98 s = m_serverSocket.accept();
99
100 if (logEnabled)
101 Log.d(TAG, "New socket accepted");
102 newSocket(qtObject, s);
103 }
104 } catch (IOException ex) {
105 if (logEnabled)
106 Log.d(TAG, "Server socket accept() failed:" + ex.toString());
107 ex.printStackTrace();
108 errorOccurred(qtObject, QT_ACCEPT_FAILED);
109 }
110 }
111
112 Log.d(TAG, "Leaving server socket thread.");
113 }
114
115 // This function closes the socket server
116 //
117 // A note on threading behavior
118 // 1. This function is called from Qt thread which is different from the Java thread (run())
119 // 2. The caller of this function expects the Java thread to be finished upon return
120 //
121 // First we mark the Java thread as interrupted, then call close() on the
122 // listening socket if it had been created, and lastly wait for the thread to finish.
123 // The close() method of the socket is intended to be used to abort the accept() from
124 // another thread, as per the accept() documentation.
125 //
126 // If the Java thread was in the middle of creating a socket with the non-blocking
127 // listen* call, the run() will notice after the returning from the listen* that it has
128 // been interrupted and returns early from the run().
129 //
130 // If the Java thread was in the middle of the blocking accept() call, it will get
131 // interrupated by the close() call on the socket. After returning the run() will
132 // notice it has been interrupted and return from the run()
133 void close()
134 {
135 if (!isAlive())
136 return;
137
138 try {
139 //ensure closing of thread if we are not currently blocking on accept()
140 interrupt();
141
142 //interrupts accept() call above
143 if (m_serverSocket != null)
144 m_serverSocket.close();
145 // Wait for the thread to finish
146 join(20); // Maximum wait in ms, typically takes < 1ms
147 } catch (Exception ex) {
148 Log.d(TAG, "Closing server socket close() failed:" + ex.toString());
149 ex.printStackTrace();
150 }
151 }
152
153 static native void errorOccurred(long qtObject, int errorCode);
154 static native void newSocket(long qtObject, BluetoothSocket socket);
155}
QPainter Context
static const QString context()
Definition java.cpp:398
@ BluetoothAdapter
QTCONCURRENT_RUN_NODISCARD auto run(QThreadPool *pool, Function &&f, Args &&...args)
#define TAG(x)
GLdouble s
[6]
Definition qopenglext.h:235
QNetworkAccessManager manager
[0]
QSctpSocket * socket
[0]