An #AndroidPort implementation that initiates a Bluetooth RFCOMM connection.
| 32 | * connection. |
| 33 | */ |
| 34 | class BluetoothClientPort extends ProxyAndroidPort implements Runnable { |
| 35 | private static final String TAG = "XCSoar"; |
| 36 | |
| 37 | private BluetoothSocket socket; |
| 38 | |
| 39 | private Thread thread; |
| 40 | |
| 41 | BluetoothClientPort(BluetoothSocket _socket) |
| 42 | throws IOException { |
| 43 | socket = _socket; |
| 44 | |
| 45 | thread = new Thread(this, toString()); |
| 46 | thread.start(); |
| 47 | } |
| 48 | |
| 49 | @Override public String toString() { |
| 50 | BluetoothSocket socket = this.socket; |
| 51 | return socket != null |
| 52 | ? "Bluetooth " + BluetoothHelper.getDisplayString(socket) |
| 53 | : super.toString(); |
| 54 | } |
| 55 | |
| 56 | @Override public void close() { |
| 57 | BluetoothSocket socket = this.socket; |
| 58 | if (socket != null) { |
| 59 | try { |
| 60 | socket.close(); |
| 61 | } catch (IOException e) { |
| 62 | Log.w(TAG, "Failed to close BluetoothSocket", e); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* ensure that run() has finished before calling |
| 67 | ProxyAndroidPort.close() */ |
| 68 | Thread thread = this.thread; |
| 69 | if (thread != null) { |
| 70 | try { |
| 71 | thread.join(); |
| 72 | } catch (InterruptedException e) { |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | super.close(); |
| 77 | } |
| 78 | |
| 79 | @Override public int getState() { |
| 80 | return socket != null |
| 81 | ? STATE_LIMBO |
| 82 | : super.getState(); |
| 83 | } |
| 84 | |
| 85 | @Override public void run() { |
| 86 | try { |
| 87 | BluetoothSocket socket = this.socket; |
| 88 | if (socket != null) { |
| 89 | socket.connect(); |
| 90 | this.socket = null; |
| 91 | setPort(new BluetoothPort(socket)); |
nothing calls this directly
no outgoing calls
no test coverage detected