altosdroid: Start adding an AltosLink subclass for android
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosBluetooth.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.AltosDroid;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.lang.reflect.Method;
24 import android.bluetooth.BluetoothAdapter;
25 import android.bluetooth.BluetoothDevice;
26 import android.bluetooth.BluetoothSocket;
27 import android.content.Context;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.os.Message;
31 import android.util.Log;
32
33 import org.altusmetrum.AltosLib.*;
34
35 public class AltosBluetooth extends AltosLink {
36
37         // Debugging
38         private static final String TAG = "AltosBluetooth";
39         private static final boolean D = true;
40
41         /**
42          * This thread runs while attempting to make an outgoing connection
43          * with a device. It runs straight through; the connection either
44          * succeeds or fails.
45          */
46
47         private BluetoothAdapter        adapter;
48         private ConnectThread           connect_thread;
49         private BluetoothSocket         socket;
50         private InputStream             input;
51         private OutputStream            output;
52
53         private class ConnectThread extends Thread {
54                 private final BluetoothDevice mmDevice;
55                 private String mSocketType;
56                 BluetoothSocket tmp_socket;
57
58                 public ConnectThread(BluetoothDevice device, boolean secure) {
59                         mmDevice = device;
60                         mSocketType = secure ? "Secure" : "Insecure";
61
62                         // Get a BluetoothSocket for a connection with the
63                         // given BluetoothDevice
64                         try {
65                                 if (secure) {
66                                         Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
67                                         tmp_socket = (BluetoothSocket) m.invoke(device, 2);
68                                         // tmp = device.createRfcommSocket(2);
69                                 } else {
70                                         Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
71                                         tmp_socket = (BluetoothSocket) m.invoke(device, 2);
72                                         // tmp = device.createInsecureRfcommSocket(2);
73                                 }
74                         } catch (Exception e) {
75                                 Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
76                                 e.printStackTrace();
77                         }
78                 }
79
80                 public void run() {
81                         Log.i(TAG, "BEGIN connect_thread SocketType:" + mSocketType);
82                         setName("ConnectThread" + mSocketType);
83
84                         // Always cancel discovery because it will slow down a connection
85                         adapter.cancelDiscovery();
86
87                         // Make a connection to the BluetoothSocket
88                         try {
89                                 // This is a blocking call and will only return on a
90                                 // successful connection or an exception
91                                 tmp_socket.connect();
92                         } catch (IOException e) {
93                                 // Close the socket
94                                 try {
95                                         tmp_socket.close();
96                                 } catch (IOException e2) {
97                                         Log.e(TAG, "unable to close() " + mSocketType +
98                                               " socket during connection failure", e2);
99                                 }
100                                 connection_failed();
101                                 return;
102                         }
103
104                         try {
105                                 synchronized (AltosBluetooth.this) {
106                                         input = tmp_socket.getInputStream();
107                                         output = tmp_socket.getOutputStream();
108                                         socket = tmp_socket;
109                                         // Reset the ConnectThread because we're done
110                                         AltosBluetooth.this.notify();
111                                         connect_thread = null;
112                                 }
113                         } catch (Exception e) {
114                                 Log.e(TAG, "Failed to finish connection", e);
115                                 e.printStackTrace();
116                         }
117                 }
118
119                 public void cancel() {
120                         try {
121                                 if (tmp_socket != null)
122                                         tmp_socket.close();
123                         } catch (IOException e) {
124                                 Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
125                         }
126                 }
127         }
128
129         private synchronized void wait_connected() throws InterruptedException {
130                 if (input == null) {
131                         wait();
132                 }
133         }
134
135         private void connection_failed() {
136         }
137         
138         public void print(String data) {
139                 byte[] bytes = data.getBytes();
140                 try {
141                         wait_connected();
142                         output.write(bytes);
143                 } catch (IOException e) {
144                         connection_failed();
145                 } catch (InterruptedException e) {
146                         connection_failed();
147                 }
148         }
149
150         public int getchar() {
151                 try {
152                         wait_connected();
153                         return input.read();
154                 } catch (IOException e) {
155                         connection_failed();
156                 } catch (java.lang.InterruptedException e) {
157                         connection_failed();
158                 }
159                 return AltosLink.ERROR;
160         }
161                         
162         public void close() {
163                 synchronized(this) {
164                         if (connect_thread != null) {
165                                 connect_thread.cancel();
166                                 connect_thread = null;
167                         }
168                 }
169         }
170
171         public void flush_output() {
172                 super.flush_output();
173                 /* any local work needed to flush bluetooth? */
174         }
175
176         public boolean can_cancel_reply() {
177                 return false;
178         }
179         public boolean show_reply_timeout() {
180                 return true;
181         }
182                 
183         public void hide_reply_timeout() {
184         }
185
186         public AltosBluetooth(BluetoothDevice device) {
187                 adapter = BluetoothAdapter.getDefaultAdapter();
188                 connect_thread = new ConnectThread(device, true);
189                 connect_thread.start();
190         }
191 }