X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=altosdroid%2Fsrc%2Forg%2Faltusmetrum%2FAltosDroid%2FAltosBluetooth.java;h=359b58321ed11995d805cc5a583aa539fd9d890c;hb=a61217f0a6d0ef48b6471f632c4600255867e831;hp=d506ff59cc4702e2d2fa8561330af5289edb2b8e;hpb=bca342577740a9d04b8419ecadcff582e77f1e61;p=fw%2Faltos diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index d506ff59..359b5832 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -4,7 +4,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -29,16 +30,11 @@ import android.bluetooth.BluetoothSocket; //import android.os.Bundle; import android.os.Handler; //import android.os.Message; -import android.util.Log; -import org.altusmetrum.altoslib_7.*; +import org.altusmetrum.altoslib_12.*; public class AltosBluetooth extends AltosDroidLink { - // Debugging - private static final String TAG = "AltosBluetooth"; - private static final boolean D = true; - private ConnectThread connect_thread = null; private BluetoothDevice device; @@ -60,7 +56,7 @@ public class AltosBluetooth extends AltosDroidLink { void connected() { if (closed()) { - if (D) Log.d(TAG, "connected after closed"); + AltosDebug.debug("connected after closed"); return; } @@ -82,7 +78,7 @@ public class AltosBluetooth extends AltosDroidLink { private void connect_failed() { if (closed()) { - if (D) Log.d(TAG, "connect_failed after closed"); + AltosDebug.debug("connect_failed after closed"); return; } @@ -90,7 +86,7 @@ public class AltosBluetooth extends AltosDroidLink { input = null; output = null; handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget(); - if (D) Log.e(TAG, "ConnectThread: Failed to establish connection"); + AltosDebug.error("ConnectThread: Failed to establish connection"); } void close_device() { @@ -105,7 +101,7 @@ public class AltosBluetooth extends AltosDroidLink { try { tmp_socket.close(); } catch (IOException e) { - if (D) Log.e(TAG, "close_socket failed"); + AltosDebug.error("close_socket failed"); } } } @@ -122,13 +118,14 @@ public class AltosBluetooth extends AltosDroidLink { BluetoothSocket tmp_socket = null; + AltosDebug.check_ui("create_socket\n"); try { tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID); } catch (IOException e) { e.printStackTrace(); } if (socket != null) { - if (D) Log.d(TAG, String.format("Socket already allocated %s", socket.toString())); + AltosDebug.debug("Socket already allocated %s", socket.toString()); close_device(); } synchronized (this) { @@ -139,7 +136,7 @@ public class AltosBluetooth extends AltosDroidLink { private class ConnectThread extends Thread { public void run() { - if (D) Log.d(TAG, "ConnectThread: BEGIN"); + AltosDebug.debug("ConnectThread: BEGIN (pause %b)", pause); setName("ConnectThread"); if (pause) { @@ -154,7 +151,7 @@ public class AltosBluetooth extends AltosDroidLink { try { BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); } catch (Exception e) { - if (D) Log.d(TAG, String.format("cancelDiscovery exception %s", e.toString())); + AltosDebug.debug("cancelDiscovery exception %s", e.toString()); } BluetoothSocket local_socket = null; @@ -170,8 +167,13 @@ public class AltosBluetooth extends AltosDroidLink { // This is a blocking call and will only return on a // successful connection or an exception local_socket.connect(); - } catch (IOException e) { - if (D) Log.d(TAG, String.format("Connect exception %s", e.toString())); + } catch (Exception e) { + AltosDebug.debug("Connect exception %s", e.toString()); + try { + local_socket.close(); + } catch (Exception ce) { + AltosDebug.debug("Close exception %s", ce.toString()); + } local_socket = null; } } @@ -182,21 +184,24 @@ public class AltosBluetooth extends AltosDroidLink { connect_failed(); } - if (D) Log.d(TAG, "ConnectThread: completed"); + AltosDebug.debug("ConnectThread: completed"); } } private synchronized void wait_connected() throws InterruptedException, IOException { + AltosDebug.check_ui("wait_connected\n"); if (input == null && socket != null) { - if (D) Log.d(TAG, "wait_connected..."); + AltosDebug.debug("wait_connected..."); wait(); - if (D) Log.d(TAG, "wait_connected done"); + AltosDebug.debug("wait_connected done"); } if (socket == null) throw new IOException(); } int write(byte[] buffer, int len) { + if (output == null) + return -1; try { output.write(buffer, 0, len); } catch (IOException ie) { @@ -206,6 +211,8 @@ public class AltosBluetooth extends AltosDroidLink { } int read(byte[] buffer, int len) { + if (input == null) + return -1; try { return input.read(buffer, 0, len); } catch (IOException ie) {