altosdroid: Move constructor for 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.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
41         private BluetoothAdapter adapter;
42         private BluetoothDevice  device;
43         private BluetoothSocket  socket;
44         private InputStream      input;
45         private OutputStream     output;
46
47         // Constructor
48         public AltosBluetooth(BluetoothDevice in_device) {
49                 adapter = BluetoothAdapter.getDefaultAdapter();
50
51
52                 connect_thread = new ConnectThread(device);
53                 connect_thread.start();
54         }
55
56         private class ConnectThread extends Thread {
57                 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
58
59                 public ConnectThread(BluetoothDevice device) {
60                         BluetoothSocket tmp_socket = null;
61
62                         try {
63                                 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
64                         } catch (IOException e) {
65                                 e.printStackTrace();
66                         }
67                         socket = tmp_socket;
68                 }
69
70                 public void run() {
71                         if (D) Log.i(TAG, "BEGIN ConnectThread");
72                         setName("ConnectThread");
73
74                         // Always cancel discovery because it will slow down a connection
75                         adapter.cancelDiscovery();
76
77                         // Make a connection to the BluetoothSocket
78                         try {
79                                 // This is a blocking call and will only return on a
80                                 // successful connection or an exception
81                                 socket.connect();
82                         } catch (IOException e) {
83                                 // Close the socket
84                                 try {
85                                         socket.close();
86                                 } catch (IOException e2) {
87                                         if (D) Log.e(TAG, "unable to close() socket during connection failure", e2);
88                                 }
89                                 connection_failed();
90                                 return;
91                         }
92
93                         try {
94                                 synchronized (AltosBluetooth.this) {
95                                         input = socket.getInputStream();
96                                         output = socket.getOutputStream();
97
98                                         // Reset the ConnectThread because we're done
99                                         AltosBluetooth.this.notify();
100                                         connect_thread = null;
101                                         if (D) Log.i(TAG, "Completed connect");
102                                 }
103                         } catch (Exception e) {
104                                 if (D) Log.e(TAG, "Failed to finish connection", e);
105                                 e.printStackTrace();
106                         }
107                 }
108
109                 public void cancel() {
110                         try {
111                                 if (socket != null)
112                                         socket.close();
113                         } catch (IOException e) {
114                                 if (D) Log.e(TAG, "close() of connect socket failed", e);
115                         }
116                 }
117         }
118
119         private synchronized void wait_connected() throws InterruptedException {
120                 if (input == null) {
121                         wait();
122                 }
123         }
124
125         private void connection_failed() {
126         }
127         
128         public void print(String data) {
129                 byte[] bytes = data.getBytes();
130                 try {
131                         wait_connected();
132                         output.write(bytes);
133                 } catch (IOException e) {
134                         connection_failed();
135                 } catch (InterruptedException e) {
136                         connection_failed();
137                 }
138         }
139
140         public int getchar() {
141                 try {
142                         wait_connected();
143                         return input.read();
144                 } catch (IOException e) {
145                         connection_failed();
146                 } catch (java.lang.InterruptedException e) {
147                         connection_failed();
148                 }
149                 return AltosLink.ERROR;
150         }
151                         
152         public void close() {
153                 synchronized(this) {
154                         if (connect_thread != null) {
155                                 connect_thread.cancel();
156                                 connect_thread = null;
157                         }
158                 }
159         }
160
161
162         //public void flush_output() { super.flush_output(); }
163
164         public boolean can_cancel_reply()   { return false; }
165         public boolean show_reply_timeout() { return true; }
166         public void hide_reply_timeout()    { }
167
168 }