X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosdroid%2Fsrc%2Forg%2Faltusmetrum%2FAltosDroid%2FAltosBluetooth.java;h=03ae5cb84a55281bb3fb5046d0809293e06a5f7f;hp=45438a6c9ba3cd3117e29d8f4c6ec93af329f560;hb=2509b664df6a13e6ae9e6753dc9fa0d696a4f6c7;hpb=b242f2756a8d9419a9bdba890b9e6b73560bdc19 diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index 45438a6c..03ae5cb8 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -1,5 +1,6 @@ /* * Copyright © 2011 Keith Packard + * Copyright © 2012 Mike Beattie * * 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 @@ -20,172 +21,203 @@ 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.Bundle; import android.os.Handler; -import android.os.Message; -import android.util.Log; +//import android.os.Message; -import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altoslib_7.*; -public class AltosBluetooth extends AltosLink { +public class AltosBluetooth extends AltosDroidLink { - // Debugging - private static final String TAG = "AltosBluetooth"; - private static final boolean D = true; + private ConnectThread connect_thread = null; - /** - * This thread runs while attempting to make an outgoing connection - * with a device. It runs straight through; the connection either - * succeeds or fails. - */ + private BluetoothDevice device; + private BluetoothSocket socket; + private InputStream input; + private OutputStream output; + private boolean pause; - private BluetoothAdapter adapter; - private ConnectThread connect_thread; - private BluetoothSocket socket; - private InputStream input; - private OutputStream output; + // Constructor + public AltosBluetooth(BluetoothDevice device, Handler handler, boolean pause) { + super(handler); + this.device = device; + this.handler = handler; + this.pause = pause; - private class ConnectThread extends Thread { - private final BluetoothDevice mmDevice; - private String mSocketType; - BluetoothSocket tmp_socket; + connect_thread = new ConnectThread(); + connect_thread.start(); + } - public ConnectThread(BluetoothDevice device, boolean secure) { - mmDevice = device; - mSocketType = secure ? "Secure" : "Insecure"; + void connected() { + if (closed()) { + AltosDebug.debug("connected after closed"); + return; + } - // 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); + AltosDebug.check_ui("connected\n"); + try { + synchronized(this) { + if (socket != null) { + input = socket.getInputStream(); + output = socket.getOutputStream(); + super.connected(); } - } catch (Exception e) { - Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e); - e.printStackTrace(); } + } catch (InterruptedException ie) { + connect_failed(); + } catch (IOException io) { + connect_failed(); } + } - public void run() { - Log.i(TAG, "BEGIN connect_thread SocketType:" + mSocketType); - setName("ConnectThread" + mSocketType); + private void connect_failed() { + if (closed()) { + AltosDebug.debug("connect_failed after closed"); + return; + } - // Always cancel discovery because it will slow down a connection - adapter.cancelDiscovery(); + close_device(); + input = null; + output = null; + handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget(); + AltosDebug.error("ConnectThread: Failed to establish connection"); + } + + void close_device() { + BluetoothSocket tmp_socket; + + synchronized(this) { + tmp_socket = socket; + socket = null; + } - // Make a connection to the BluetoothSocket + if (tmp_socket != null) { try { - // This is a blocking call and will only return on a - // successful connection or an exception - tmp_socket.connect(); + tmp_socket.close(); } catch (IOException e) { - // Close the socket + AltosDebug.error("close_socket failed"); + } + } + } + + public void close() { + super.close(); + input = null; + output = null; + } + + private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); + + private void create_socket(BluetoothDevice device) { + + 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) { + AltosDebug.debug("Socket already allocated %s", socket.toString()); + close_device(); + } + synchronized (this) { + socket = tmp_socket; + } + } + + private class ConnectThread extends Thread { + + public void run() { + AltosDebug.debug("ConnectThread: BEGIN (pause %b)", pause); + setName("ConnectThread"); + + if (pause) { try { - tmp_socket.close(); - } catch (IOException e2) { - Log.e(TAG, "unable to close() " + mSocketType + - " socket during connection failure", e2); + Thread.sleep(4000); + } catch (InterruptedException e) { } - connection_failed(); - return; } + create_socket(device); + // Always cancel discovery because it will slow down a connection try { - synchronized (AltosBluetooth.this) { - input = tmp_socket.getInputStream(); - output = tmp_socket.getOutputStream(); - socket = tmp_socket; - // Reset the ConnectThread because we're done - AltosBluetooth.this.notify(); - connect_thread = null; - } + BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); } catch (Exception e) { - Log.e(TAG, "Failed to finish connection", e); - e.printStackTrace(); + AltosDebug.debug("cancelDiscovery exception %s", e.toString()); } - } - public void cancel() { - try { - if (tmp_socket != null) - tmp_socket.close(); - } catch (IOException e) { - Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e); + BluetoothSocket local_socket = null; + + synchronized (AltosBluetooth.this) { + if (!closed()) + local_socket = socket; + } + + if (local_socket != null) { + try { + // Make a connection to the BluetoothSocket + // This is a blocking call and will only return on a + // successful connection or an exception + local_socket.connect(); + } 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; + } + } + + if (local_socket != null) { + connected(); + } else { + connect_failed(); } + + AltosDebug.debug("ConnectThread: completed"); } } - private synchronized void wait_connected() throws InterruptedException { - if (input == null) { + private synchronized void wait_connected() throws InterruptedException, IOException { + AltosDebug.check_ui("wait_connected\n"); + if (input == null && socket != null) { + AltosDebug.debug("wait_connected..."); wait(); + AltosDebug.debug("wait_connected done"); } + if (socket == null) + throw new IOException(); } - private void connection_failed() { - } - - public void print(String data) { - byte[] bytes = data.getBytes(); + int write(byte[] buffer, int len) { try { - wait_connected(); - output.write(bytes); - } catch (IOException e) { - connection_failed(); - } catch (InterruptedException e) { - connection_failed(); + output.write(buffer, 0, len); + } catch (IOException ie) { + return -1; } + return len; } - public int getchar() { + int read(byte[] buffer, int len) { try { - wait_connected(); - return input.read(); - } catch (IOException e) { - connection_failed(); - } catch (java.lang.InterruptedException e) { - connection_failed(); - } - return AltosLink.ERROR; - } - - public void close() { - synchronized(this) { - if (connect_thread != null) { - connect_thread.cancel(); - connect_thread = null; - } + return input.read(buffer, 0, len); + } catch (IOException ie) { + return -1; } } - 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() { - } + // Stubs of required methods when extending AltosLink + public boolean can_cancel_reply() { return false; } + public boolean show_reply_timeout() { return true; } + public void hide_reply_timeout() { } - public AltosBluetooth(BluetoothDevice device) { - adapter = BluetoothAdapter.getDefaultAdapter(); - connect_thread = new ConnectThread(device, true); - connect_thread.start(); - } -} \ No newline at end of file +}