X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosdroid%2Fsrc%2Forg%2Faltusmetrum%2FAltosDroid%2FAltosBluetooth.java;h=3bfa3488c8a25337de736e0b8a4b022125531468;hp=390dccdb563e77ebc4da635c0e20bb4ae241c2a6;hb=a9ec3c96288b7ea4e40586321a0a98edf0c8fee5;hpb=0f3597389977f86a8c1bdff1b7f46107c43ef306 diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index 390dccdb..3bfa3488 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -21,14 +21,12 @@ package org.altusmetrum.AltosDroid; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.Method; +import java.util.UUID; + import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; -import android.content.Context; -import android.os.Bundle; import android.os.Handler; -import android.os.Message; import android.util.Log; import org.altusmetrum.AltosLib.*; @@ -39,48 +37,47 @@ public class AltosBluetooth extends AltosLink { private static final String TAG = "AltosBluetooth"; private static final boolean D = true; - /** - * This thread runs while attempting to make an outgoing connection - * with a device. It runs straight through; the connection either - * succeeds or fails. - */ + private ConnectThread connect_thread = null; + private Thread input_thread = null; + + private Handler handler; + + private BluetoothAdapter adapter; + private BluetoothDevice device; + private BluetoothSocket socket; + private InputStream input; + private OutputStream output; + + // Constructor + public AltosBluetooth(BluetoothDevice in_device, Handler in_handler) { + adapter = BluetoothAdapter.getDefaultAdapter(); + device = in_device; + handler = in_handler; + + connect_thread = new ConnectThread(device); + connect_thread.start(); - private BluetoothAdapter adapter; - private ConnectThread connect_thread; - private BluetoothSocket socket; - private InputStream input; - private OutputStream output; + input_thread = new Thread(this); + input_thread.start(); + } private class ConnectThread extends Thread { - private final BluetoothDevice mmDevice; - private String mSocketType; - BluetoothSocket tmp_socket; + private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); - public ConnectThread(BluetoothDevice device, boolean secure) { - mmDevice = device; - mSocketType = secure ? "Secure" : "Insecure"; + public ConnectThread(BluetoothDevice device) { + BluetoothSocket tmp_socket = null; - // Get a BluetoothSocket for a connection with the - // given BluetoothDevice try { - if (secure) { - Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); - tmp_socket = (BluetoothSocket) m.invoke(device, 2); - // tmp = device.createRfcommSocket(2); - } else { - Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); - tmp_socket = (BluetoothSocket) m.invoke(device, 2); - // tmp = device.createInsecureRfcommSocket(2); - } - } catch (Exception e) { - Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e); + tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID); + } catch (IOException e) { e.printStackTrace(); } + socket = tmp_socket; } public void run() { - Log.i(TAG, "BEGIN connect_thread SocketType:" + mSocketType); - setName("ConnectThread" + mSocketType); + if (D) Log.i(TAG, "BEGIN ConnectThread"); + setName("ConnectThread"); // Always cancel discovery because it will slow down a connection adapter.cancelDiscovery(); @@ -89,14 +86,13 @@ public class AltosBluetooth extends AltosLink { try { // This is a blocking call and will only return on a // successful connection or an exception - tmp_socket.connect(); + socket.connect(); } catch (IOException e) { // Close the socket try { - tmp_socket.close(); + socket.close(); } catch (IOException e2) { - Log.e(TAG, "unable to close() " + mSocketType + - " socket during connection failure", e2); + if (D) Log.e(TAG, "unable to close() socket during connection failure", e2); } connection_failed(); return; @@ -104,25 +100,30 @@ public class AltosBluetooth extends AltosLink { try { synchronized (AltosBluetooth.this) { - input = tmp_socket.getInputStream(); - output = tmp_socket.getOutputStream(); - socket = tmp_socket; + input = socket.getInputStream(); + output = socket.getOutputStream(); + + // Configure the newly connected device for telemetry + print("~\nE 0\n"); + set_monitor(false); + // Reset the ConnectThread because we're done AltosBluetooth.this.notify(); connect_thread = null; + if (D) Log.i(TAG, "Completed connect"); } } catch (Exception e) { - Log.e(TAG, "Failed to finish connection", e); + if (D) Log.e(TAG, "Failed to finish connection", e); e.printStackTrace(); } } public void cancel() { try { - if (tmp_socket != null) - tmp_socket.close(); + if (socket != null) + socket.close(); } catch (IOException e) { - Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e); + if (D) Log.e(TAG, "close() of connect socket failed", e); } } } @@ -134,13 +135,16 @@ public class AltosBluetooth extends AltosLink { } private void connection_failed() { + if (D) Log.i(TAG, "Bluetooth Connection failed!"); } public void print(String data) { byte[] bytes = data.getBytes(); try { + if (D) Log.i(TAG, "Entering print();"); wait_connected(); output.write(bytes); + if (D) Log.i(TAG, "Writing bytes: '" + data + "'"); } catch (IOException e) { connection_failed(); } catch (InterruptedException e) { @@ -166,27 +170,21 @@ public class AltosBluetooth extends AltosLink { connect_thread.cancel(); connect_thread = null; } + if (input_thread != null) { + try { + input_thread.interrupt(); + input_thread.join(); + } catch (Exception e) {} + input_thread = null; + } } } - public void flush_output() { - super.flush_output(); - /* any local work needed to flush bluetooth? */ - } - public boolean can_cancel_reply() { - return false; - } - public boolean show_reply_timeout() { - return true; - } - - public void hide_reply_timeout() { - } + //public void flush_output() { super.flush_output(); } - public AltosBluetooth(BluetoothDevice device) { - adapter = BluetoothAdapter.getDefaultAdapter(); - connect_thread = new ConnectThread(device, true); - connect_thread.start(); - } -} \ No newline at end of file + public boolean can_cancel_reply() { return false; } + public boolean show_reply_timeout() { return true; } + public void hide_reply_timeout() { } + +}