altosdroid: Update copyrights
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosBluetooth.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
4  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.AltosDroid;
20
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;
33
34 import org.altusmetrum.AltosLib.*;
35
36 public class AltosBluetooth extends AltosLink {
37
38         // Debugging
39         private static final String TAG = "AltosBluetooth";
40         private static final boolean D = true;
41
42         /**
43          * This thread runs while attempting to make an outgoing connection
44          * with a device. It runs straight through; the connection either
45          * succeeds or fails.
46          */
47
48         private BluetoothAdapter        adapter;
49         private ConnectThread           connect_thread;
50         private BluetoothSocket         socket;
51         private InputStream             input;
52         private OutputStream            output;
53
54         private class ConnectThread extends Thread {
55                 private final BluetoothDevice mmDevice;
56                 private String mSocketType;
57                 BluetoothSocket tmp_socket;
58
59                 public ConnectThread(BluetoothDevice device, boolean secure) {
60                         mmDevice = device;
61                         mSocketType = secure ? "Secure" : "Insecure";
62
63                         // Get a BluetoothSocket for a connection with the
64                         // given BluetoothDevice
65                         try {
66                                 if (secure) {
67                                         Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
68                                         tmp_socket = (BluetoothSocket) m.invoke(device, 2);
69                                         // tmp = device.createRfcommSocket(2);
70                                 } else {
71                                         Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
72                                         tmp_socket = (BluetoothSocket) m.invoke(device, 2);
73                                         // tmp = device.createInsecureRfcommSocket(2);
74                                 }
75                         } catch (Exception e) {
76                                 Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
77                                 e.printStackTrace();
78                         }
79                 }
80
81                 public void run() {
82                         Log.i(TAG, "BEGIN connect_thread SocketType:" + mSocketType);
83                         setName("ConnectThread" + mSocketType);
84
85                         // Always cancel discovery because it will slow down a connection
86                         adapter.cancelDiscovery();
87
88                         // Make a connection to the BluetoothSocket
89                         try {
90                                 // This is a blocking call and will only return on a
91                                 // successful connection or an exception
92                                 tmp_socket.connect();
93                         } catch (IOException e) {
94                                 // Close the socket
95                                 try {
96                                         tmp_socket.close();
97                                 } catch (IOException e2) {
98                                         Log.e(TAG, "unable to close() " + mSocketType +
99                                               " socket during connection failure", e2);
100                                 }
101                                 connection_failed();
102                                 return;
103                         }
104
105                         try {
106                                 synchronized (AltosBluetooth.this) {
107                                         input = tmp_socket.getInputStream();
108                                         output = tmp_socket.getOutputStream();
109                                         socket = tmp_socket;
110                                         // Reset the ConnectThread because we're done
111                                         AltosBluetooth.this.notify();
112                                         connect_thread = null;
113                                 }
114                         } catch (Exception e) {
115                                 Log.e(TAG, "Failed to finish connection", e);
116                                 e.printStackTrace();
117                         }
118                 }
119
120                 public void cancel() {
121                         try {
122                                 if (tmp_socket != null)
123                                         tmp_socket.close();
124                         } catch (IOException e) {
125                                 Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
126                         }
127                 }
128         }
129
130         private synchronized void wait_connected() throws InterruptedException {
131                 if (input == null) {
132                         wait();
133                 }
134         }
135
136         private void connection_failed() {
137         }
138         
139         public void print(String data) {
140                 byte[] bytes = data.getBytes();
141                 try {
142                         wait_connected();
143                         output.write(bytes);
144                 } catch (IOException e) {
145                         connection_failed();
146                 } catch (InterruptedException e) {
147                         connection_failed();
148                 }
149         }
150
151         public int getchar() {
152                 try {
153                         wait_connected();
154                         return input.read();
155                 } catch (IOException e) {
156                         connection_failed();
157                 } catch (java.lang.InterruptedException e) {
158                         connection_failed();
159                 }
160                 return AltosLink.ERROR;
161         }
162                         
163         public void close() {
164                 synchronized(this) {
165                         if (connect_thread != null) {
166                                 connect_thread.cancel();
167                                 connect_thread = null;
168                         }
169                 }
170         }
171
172         public void flush_output() {
173                 super.flush_output();
174                 /* any local work needed to flush bluetooth? */
175         }
176
177         public boolean can_cancel_reply() {
178                 return false;
179         }
180         public boolean show_reply_timeout() {
181                 return true;
182         }
183                 
184         public void hide_reply_timeout() {
185         }
186
187         public AltosBluetooth(BluetoothDevice device) {
188                 adapter = BluetoothAdapter.getDefaultAdapter();
189                 connect_thread = new ConnectThread(device, true);
190                 connect_thread.start();
191         }
192 }