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