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
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")
16public class 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 public 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
39 {
40 qtContext = context;
41 setName("QtSocketServerThread");
42 }
43
44 public 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 public void run()
53 {
54 BluetoothManager manager =
55 (BluetoothManager)qtContext.getSystemService(Context.BLUETOOTH_SERVICE);
56
57 if (manager == null) {
58 errorOccurred(qtObject, QT_NO_BLUETOOTH_SUPPORTED);
59 return;
60 }
61
62 BluetoothAdapter adapter = manager.getAdapter();
63 if (adapter == null) {
64 errorOccurred(qtObject, QT_NO_BLUETOOTH_SUPPORTED);
65 return;
66 }
67
68 try {
69 if (m_isSecure) {
70 m_serverSocket = adapter.listenUsingRfcommWithServiceRecord(m_serviceName, m_uuid);
71 if (logEnabled)
72 Log.d(TAG, "Using secure socket listener");
73 } else {
74 m_serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord(m_serviceName, m_uuid);
75 if (logEnabled)
76 Log.d(TAG, "Using insecure socket listener");
77 }
78 } catch (IOException ex) {
79 if (logEnabled)
80 Log.d(TAG, "Server socket listen() failed:" + ex.toString());
81 ex.printStackTrace();
82 errorOccurred(qtObject, QT_LISTEN_FAILED);
83 return;
84 }
85
86 if (isInterrupted()) // close() may have been called
87 return;
88
89 BluetoothSocket s;
90 if (m_serverSocket != null) {
91 try {
92 while (!isInterrupted()) {
93 //this blocks until we see incoming connection
94 //or close() is called
95 if (logEnabled)
96 Log.d(TAG, "Waiting for new incoming socket");
97 s = m_serverSocket.accept();
98
99 if (logEnabled)
100 Log.d(TAG, "New socket accepted");
101 newSocket(qtObject, s);
102 }
103 } catch (IOException ex) {
104 if (logEnabled)
105 Log.d(TAG, "Server socket accept() failed:" + ex.toString());
106 ex.printStackTrace();
107 errorOccurred(qtObject, QT_ACCEPT_FAILED);
108 }
109 }
110
111 Log.d(TAG, "Leaving server socket thread.");
112 }
113
114 // This function closes the socket server
115 //
116 // A note on threading behavior
117 // 1. This function is called from Qt thread which is different from the Java thread (run())
118 // 2. The caller of this function expects the Java thread to be finished upon return
119 //
120 // First we mark the Java thread as interrupted, then call close() on the
121 // listening socket if it had been created, and lastly wait for the thread to finish.
122 // The close() method of the socket is intended to be used to abort the accept() from
123 // another thread, as per the accept() documentation.
124 //
125 // If the Java thread was in the middle of creating a socket with the non-blocking
126 // listen* call, the run() will notice after the returning from the listen* that it has
127 // been interrupted and returns early from the run().
128 //
129 // If the Java thread was in the middle of the blocking accept() call, it will get
130 // interrupated by the close() call on the socket. After returning the run() will
131 // notice it has been interrupted and return from the run()
132 public void close()
133 {
134 if (!isAlive())
135 return;
136
137 try {
138 //ensure closing of thread if we are not currently blocking on accept()
139 interrupt();
140
141 //interrupts accept() call above
142 if (m_serverSocket != null)
143 m_serverSocket.close();
144 // Wait for the thread to finish
145 join(20); // Maximum wait in ms, typically takes < 1ms
146 } catch (Exception ex) {
147 Log.d(TAG, "Closing server socket close() failed:" + ex.toString());
148 ex.printStackTrace();
149 }
150 }
151
152 public static native void errorOccurred(long qtObject, int errorCode);
153 public static native void newSocket(long qtObject, BluetoothSocket socket);
154}
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")
static void * context
#define TAG(x)
GLdouble s
[6]
Definition qopenglext.h:235
QTcpSocket * socket
[1]
QNetworkAccessManager manager