altosdroid: Need access to handler inside AltosBluetooth
[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.util.UUID;
25
26 import android.bluetooth.BluetoothAdapter;
27 import android.bluetooth.BluetoothDevice;
28 import android.bluetooth.BluetoothSocket;
29 import android.os.Handler;
30 import android.util.Log;
31
32 import org.altusmetrum.AltosLib.*;
33
34 public class AltosBluetooth extends AltosLink {
35
36         // Debugging
37         private static final String TAG = "AltosBluetooth";
38         private static final boolean D = true;
39
40         private ConnectThread    connect_thread = null;
41         private Thread           input_thread   = null;
42
43         private Handler          handler;
44
45         private BluetoothAdapter adapter;
46         private BluetoothDevice  device;
47         private BluetoothSocket  socket;
48         private InputStream      input;
49         private OutputStream     output;
50
51         // Constructor
52         public AltosBluetooth(BluetoothDevice in_device, Handler in_handler) {
53                 adapter = BluetoothAdapter.getDefaultAdapter();
54                 device = in_device;
55                 handler = in_handler;
56
57                 connect_thread = new ConnectThread(device);
58                 connect_thread.start();
59
60                 input_thread = new Thread(this);
61                 input_thread.start();
62         }
63
64         private class ConnectThread extends Thread {
65                 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
66
67                 public ConnectThread(BluetoothDevice device) {
68                         BluetoothSocket tmp_socket = null;
69
70                         try {
71                                 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
72                         } catch (IOException e) {
73                                 e.printStackTrace();
74                         }
75                         socket = tmp_socket;
76                 }
77
78                 public void run() {
79                         if (D) Log.i(TAG, "BEGIN ConnectThread");
80                         setName("ConnectThread");
81
82                         // Always cancel discovery because it will slow down a connection
83                         adapter.cancelDiscovery();
84
85                         // Make a connection to the BluetoothSocket
86                         try {
87                                 // This is a blocking call and will only return on a
88                                 // successful connection or an exception
89                                 socket.connect();
90                         } catch (IOException e) {
91                                 // Close the socket
92                                 try {
93                                         socket.close();
94                                 } catch (IOException e2) {
95                                         if (D) Log.e(TAG, "unable to close() socket during connection failure", e2);
96                                 }
97                                 connection_failed();
98                                 return;
99                         }
100
101                         try {
102                                 synchronized (AltosBluetooth.this) {
103                                         input = socket.getInputStream();
104                                         output = socket.getOutputStream();
105
106                                         // Configure the newly connected device for telemetry
107                                         print("~\nE 0\n");
108                                         set_monitor(false);
109
110                                         // Reset the ConnectThread because we're done
111                                         AltosBluetooth.this.notify();
112                                         connect_thread = null;
113                                         if (D) Log.i(TAG, "Completed connect");
114                                 }
115                         } catch (Exception e) {
116                                 if (D) Log.e(TAG, "Failed to finish connection", e);
117                                 e.printStackTrace();
118                         }
119                 }
120
121                 public void cancel() {
122                         try {
123                                 if (socket != null)
124                                         socket.close();
125                         } catch (IOException e) {
126                                 if (D) Log.e(TAG, "close() of connect socket failed", e);
127                         }
128                 }
129         }
130
131         private synchronized void wait_connected() throws InterruptedException {
132                 if (input == null) {
133                         wait();
134                 }
135         }
136
137         private void connection_failed() {
138                 if (D) Log.i(TAG, "Bluetooth Connection failed!");
139         }
140         
141         public void print(String data) {
142                 byte[] bytes = data.getBytes();
143                 try {
144                         if (D) Log.i(TAG, "Entering print();");
145                         wait_connected();
146                         output.write(bytes);
147                         if (D) Log.i(TAG, "Writing bytes: '" + data + "'");
148                 } catch (IOException e) {
149                         connection_failed();
150                 } catch (InterruptedException e) {
151                         connection_failed();
152                 }
153         }
154
155         public int getchar() {
156                 try {
157                         wait_connected();
158                         return input.read();
159                 } catch (IOException e) {
160                         connection_failed();
161                 } catch (java.lang.InterruptedException e) {
162                         connection_failed();
163                 }
164                 return AltosLink.ERROR;
165         }
166                         
167         public void close() {
168                 synchronized(this) {
169                         if (connect_thread != null) {
170                                 connect_thread.cancel();
171                                 connect_thread = null;
172                         }
173                         if (input_thread != null) {
174                                 try {
175                                         input_thread.interrupt();
176                                         input_thread.join();
177                                 } catch (Exception e) {}
178                                 input_thread = null;
179                         }
180                 }
181         }
182
183
184         //public void flush_output() { super.flush_output(); }
185
186         public boolean can_cancel_reply()   { return false; }
187         public boolean show_reply_timeout() { return true; }
188         public void hide_reply_timeout()    { }
189
190 }