/*
* Copyright © 2011 Keith Packard <keithp@keithp.com>
+ * Copyright © 2012 Mike Beattie <mike@ethernal.org>
*
* 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
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.*;
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();
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;
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);
}
}
}
}
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) {
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() { }
+
+}