+ private void connected() {
+ try {
+ synchronized(this) {
+ if (socket != null) {
+ input = socket.getInputStream();
+ output = socket.getOutputStream();
+
+ input_thread = new Thread(this);
+ input_thread.start();
+
+ // Configure the newly connected device for telemetry
+ print("~\nE 0\n");
+ set_monitor(false);
+ if (D) Log.d(TAG, "ConnectThread: connected");
+
+ /* Let TelemetryService know we're connected
+ */
+ handler.obtainMessage(TelemetryService.MSG_CONNECTED).sendToTarget();
+
+ /* Notify other waiting threads that we're connected now
+ */
+ notifyAll();
+ }
+ }
+ } catch (IOException io) {
+ connect_failed();
+ }
+ }
+
+ private void connect_failed() {
+ synchronized (this) {
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e2) {
+ if (D) Log.e(TAG, "ConnectThread: Failed to close() socket after failed connection");
+ }
+ socket = null;
+ }
+ input = null;
+ output = null;
+ handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED).sendToTarget();
+ if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
+ }
+ }
+
+ private Object closing_lock = new Object();
+ private boolean closing = false;
+
+ private void disconnected() {
+ synchronized(closing_lock) {
+ if (D) Log.e(TAG, String.format("Connection lost during I/O. Closing %b", closing));
+ if (!closing) {
+ if (D) Log.d(TAG, "Sending disconnected message");
+ handler.obtainMessage(TelemetryService.MSG_DISCONNECTED).sendToTarget();
+ }
+ }
+ }
+