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