MCPcopy Create free account
hub / github.com/XCSoar/XCSoar / BluetoothServerPort

Class BluetoothServerPort

android/src/BluetoothServerPort.java:40–129  ·  view source on GitHub ↗

A utility class which wraps the Java API into an easier API for the C++ code.

Source from the content-addressed store, hash-verified

38 * C++ code.
39 */
40final class BluetoothServerPort extends MultiPort
41 implements Runnable, InputListener {
42
43 private static final String TAG = "XCSoar";
44
45 private BluetoothServerSocket serverSocket;
46 private Collection<BluetoothSocket> sockets =
47 new LinkedList<BluetoothSocket>();
48
49 private final Thread thread = new Thread(this, "Bluetooth server");
50
51 BluetoothServerPort(BluetoothAdapter adapter, UUID uuid)
52 throws IOException {
53 serverSocket = adapter.listenUsingRfcommWithServiceRecord("XCSoar", uuid);
54
55 thread.start();
56 }
57
58 private synchronized BluetoothServerSocket stealServerSocket() {
59 BluetoothServerSocket s = serverSocket;
60 serverSocket = null;
61 return s;
62 }
63
64 private void closeServerSocket() {
65 BluetoothServerSocket s = stealServerSocket();
66 if (s == null)
67 return;
68
69 try {
70 s.close();
71 } catch (IOException e) {
72 Log.e(TAG, "Failed to close Bluetooth server socket", e);
73 }
74
75 stateChanged();
76 }
77
78 @Override public void run() {
79 while (true) {
80 try {
81 /* copy serverSocket to a local variable, so we can compare it
82 with null without risking a race condition with the thread
83 calling close() */
84 BluetoothServerSocket ss = serverSocket;
85 if (ss == null)
86 /* close() is being called by another thread, which now
87 waits for this thread to quit */
88 break;
89
90 BluetoothSocket socket = ss.accept();
91 Log.i(TAG, "Accepted Bluetooth connection from " +
92 BluetoothHelper.getDisplayString(socket));
93 BluetoothPort port = new BluetoothPort(socket);
94
95 /* make writes non-blocking and potentially lossy, to avoid
96 blocking when one of the peers doesn't receive quickly
97 enough */

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected