2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
3 * Copyright © 2012 Mike Beattie <mike@ethernal.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.AltosDroid;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.lang.reflect.Method;
25 import android.bluetooth.BluetoothAdapter;
26 import android.bluetooth.BluetoothDevice;
27 import android.bluetooth.BluetoothSocket;
28 import android.content.Context;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.Message;
32 import android.util.Log;
34 import org.altusmetrum.AltosLib.*;
36 public class AltosBluetooth extends AltosLink {
39 private static final String TAG = "AltosBluetooth";
40 private static final boolean D = true;
43 * This thread runs while attempting to make an outgoing connection
44 * with a device. It runs straight through; the connection either
48 private BluetoothAdapter adapter;
49 private ConnectThread connect_thread;
50 private BluetoothSocket socket;
51 private InputStream input;
52 private OutputStream output;
54 private class ConnectThread extends Thread {
55 private final BluetoothDevice mmDevice;
56 private String mSocketType;
57 BluetoothSocket tmp_socket;
59 public ConnectThread(BluetoothDevice device, boolean secure) {
61 mSocketType = secure ? "Secure" : "Insecure";
63 // Get a BluetoothSocket for a connection with the
64 // given BluetoothDevice
67 Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
68 tmp_socket = (BluetoothSocket) m.invoke(device, 2);
69 // tmp = device.createRfcommSocket(2);
71 Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
72 tmp_socket = (BluetoothSocket) m.invoke(device, 2);
73 // tmp = device.createInsecureRfcommSocket(2);
75 } catch (Exception e) {
76 Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
82 Log.i(TAG, "BEGIN connect_thread SocketType:" + mSocketType);
83 setName("ConnectThread" + mSocketType);
85 // Always cancel discovery because it will slow down a connection
86 adapter.cancelDiscovery();
88 // Make a connection to the BluetoothSocket
90 // This is a blocking call and will only return on a
91 // successful connection or an exception
93 } catch (IOException e) {
97 } catch (IOException e2) {
98 Log.e(TAG, "unable to close() " + mSocketType +
99 " socket during connection failure", e2);
106 synchronized (AltosBluetooth.this) {
107 input = tmp_socket.getInputStream();
108 output = tmp_socket.getOutputStream();
110 // Reset the ConnectThread because we're done
111 AltosBluetooth.this.notify();
112 connect_thread = null;
114 } catch (Exception e) {
115 Log.e(TAG, "Failed to finish connection", e);
120 public void cancel() {
122 if (tmp_socket != null)
124 } catch (IOException e) {
125 Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
130 private synchronized void wait_connected() throws InterruptedException {
136 private void connection_failed() {
139 public void print(String data) {
140 byte[] bytes = data.getBytes();
144 } catch (IOException e) {
146 } catch (InterruptedException e) {
151 public int getchar() {
155 } catch (IOException e) {
157 } catch (java.lang.InterruptedException e) {
160 return AltosLink.ERROR;
163 public void close() {
165 if (connect_thread != null) {
166 connect_thread.cancel();
167 connect_thread = null;
172 public void flush_output() {
173 super.flush_output();
174 /* any local work needed to flush bluetooth? */
177 public boolean can_cancel_reply() {
180 public boolean show_reply_timeout() {
184 public void hide_reply_timeout() {
187 public AltosBluetooth(BluetoothDevice device) {
188 adapter = BluetoothAdapter.getDefaultAdapter();
189 connect_thread = new ConnectThread(device, true);
190 connect_thread.start();