2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
3 * Copyright © 2012 Mike Beattie <mike@ethernal.org>
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.
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.
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.
19 package org.altusmetrum.AltosDroid;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.UUID;
26 import android.bluetooth.BluetoothAdapter;
27 import android.bluetooth.BluetoothDevice;
28 import android.bluetooth.BluetoothSocket;
29 //import android.os.Bundle;
30 import android.os.Handler;
31 //import android.os.Message;
32 import android.util.Log;
34 import org.altusmetrum.altoslib_6.*;
36 public class AltosBluetooth extends AltosLink {
39 private static final String TAG = "AltosBluetooth";
40 private static final boolean D = true;
42 private ConnectThread connect_thread = null;
43 private Thread input_thread = null;
45 private Handler handler;
47 private BluetoothAdapter adapter;
48 private BluetoothDevice device;
49 private BluetoothSocket socket;
50 private InputStream input;
51 private OutputStream output;
54 public AltosBluetooth(BluetoothDevice in_device, Handler in_handler) {
56 adapter = BluetoothAdapter.getDefaultAdapter();
60 connect_thread = new ConnectThread(device);
61 connect_thread.start();
65 private void connected() {
69 input = socket.getInputStream();
70 output = socket.getOutputStream();
72 input_thread = new Thread(this);
75 // Configure the newly connected device for telemetry
78 if (D) Log.d(TAG, "ConnectThread: connected");
80 /* Let TelemetryService know we're connected
82 handler.obtainMessage(TelemetryService.MSG_CONNECTED).sendToTarget();
84 /* Notify other waiting threads that we're connected now
89 } catch (IOException io) {
94 private void connect_failed() {
99 } catch (IOException e2) {
100 if (D) Log.e(TAG, "ConnectThread: Failed to close() socket after failed connection");
106 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED).sendToTarget();
107 if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
111 private Object closing_lock = new Object();
112 private boolean closing = false;
114 private void disconnected() {
115 synchronized(closing_lock) {
116 if (D) Log.e(TAG, String.format("Connection lost during I/O. Closing %b", closing));
118 if (D) Log.d(TAG, "Sending disconnected message");
119 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED).sendToTarget();
124 private class ConnectThread extends Thread {
125 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
127 public ConnectThread(BluetoothDevice device) {
128 BluetoothSocket tmp_socket = null;
131 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
132 } catch (IOException e) {
139 if (D) Log.d(TAG, "ConnectThread: BEGIN");
140 setName("ConnectThread");
142 // Always cancel discovery because it will slow down a connection
143 adapter.cancelDiscovery();
145 BluetoothSocket local_socket;
148 synchronized (AltosBluetooth.this) {
149 local_socket = socket;
152 if (local_socket != null) {
153 // Make a connection to the BluetoothSocket
154 // This is a blocking call and will only return on a
155 // successful connection or an exception
156 local_socket.connect();
161 } catch (IOException e) {
165 synchronized (AltosBluetooth.this) {
166 /* Reset the ConnectThread because we're done
168 connect_thread = null;
170 if (D) Log.d(TAG, "ConnectThread: Connect completed");
173 public void cancel() {
175 BluetoothSocket local_socket;
176 synchronized(AltosBluetooth.this) {
177 local_socket = socket;
180 if (local_socket != null)
181 local_socket.close();
183 } catch (IOException e) {
184 if (D) Log.e(TAG, "ConnectThread: close() of connect socket failed", e);
189 public double frequency() {
193 public int telemetry_rate() {
194 return telemetry_rate;
197 public void save_frequency() {
198 AltosPreferences.set_frequency(0, frequency);
201 public void save_telemetry_rate() {
202 AltosPreferences.set_telemetry_rate(0, telemetry_rate);
205 private synchronized void wait_connected() throws InterruptedException, IOException {
206 if (input == null && socket != null) {
207 if (D) Log.d(TAG, "wait_connected...");
209 if (D) Log.d(TAG, "wait_connected done");
212 throw new IOException();
215 public void print(String data) {
216 byte[] bytes = data.getBytes();
217 if (D) Log.d(TAG, "print(): begin");
221 if (D) Log.d(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
222 } catch (IOException e) {
224 } catch (InterruptedException e) {
229 public void putchar(byte c) {
230 byte[] bytes = { c };
231 if (D) Log.d(TAG, "print(): begin");
235 if (D) Log.d(TAG, "print(): Wrote byte: '" + c + "'");
236 } catch (IOException e) {
238 } catch (InterruptedException e) {
243 private static final int buffer_size = 1024;
245 private byte[] buffer = new byte[buffer_size];
246 private int buffer_len = 0;
247 private int buffer_off = 0;
249 public int getchar() {
250 while (buffer_off == buffer_len) {
253 buffer_len = input.read(buffer);
255 } catch (IOException e) {
256 if (D) Log.d(TAG, "getchar IOException");
258 return AltosLink.ERROR;
259 } catch (java.lang.InterruptedException e) {
260 if (D) Log.d(TAG, "getchar Interrupted");
262 return AltosLink.ERROR;
265 return buffer[buffer_off++];
268 public void closing() {
269 synchronized(closing_lock) {
270 if (D) Log.d(TAG, "Marked closing true");
276 public void close() {
277 if (D) Log.d(TAG, "close(): begin");
282 if (D) Log.d(TAG, "close(): synched");
284 if (socket != null) {
285 if (D) Log.d(TAG, "close(): Closing socket");
288 } catch (IOException e) {
289 if (D) Log.e(TAG, "close(): unable to close() socket");
293 connect_thread = null;
294 if (input_thread != null) {
295 if (D) Log.d(TAG, "close(): stopping input_thread");
297 if (D) Log.d(TAG, "close(): input_thread.interrupt().....");
298 input_thread.interrupt();
299 if (D) Log.d(TAG, "close(): input_thread.join().....");
301 } catch (Exception e) {}
311 // We override this method so that we can add some debugging. Not 100% elegant, but more useful
312 // than debugging one char at a time above in getchar()!
313 public void add_reply(AltosLine line) throws InterruptedException {
314 if (D) Log.d(TAG, String.format("Got REPLY: %s", line.line));
315 super.add_reply(line);
318 //public void flush_output() { super.flush_output(); }
320 // Stubs of required methods when extending AltosLink
321 public boolean can_cancel_reply() { return false; }
322 public boolean show_reply_timeout() { return true; }
323 public void hide_reply_timeout() { }