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_5.*;
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 class ConnectThread extends Thread {
66 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
68 public ConnectThread(BluetoothDevice device) {
69 BluetoothSocket tmp_socket = null;
72 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
73 } catch (IOException e) {
80 if (D) Log.d(TAG, "ConnectThread: BEGIN");
81 setName("ConnectThread");
83 // Always cancel discovery because it will slow down a connection
84 adapter.cancelDiscovery();
86 synchronized (AltosBluetooth.this) {
87 // Make a connection to the BluetoothSocket
89 // This is a blocking call and will only return on a
90 // successful connection or an exception
93 input = socket.getInputStream();
94 output = socket.getOutputStream();
95 } catch (IOException e) {
99 } catch (IOException e2) {
100 if (D) Log.e(TAG, "ConnectThread: Failed to close() socket after failed connection");
104 AltosBluetooth.this.notifyAll();
105 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED).sendToTarget();
106 if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
110 input_thread = new Thread(AltosBluetooth.this);
111 input_thread.start();
113 // Configure the newly connected device for telemetry
117 // Let TelemetryService know we're connected
118 handler.obtainMessage(TelemetryService.MSG_CONNECTED).sendToTarget();
120 // Notify other waiting threads, now that we're connected
121 AltosBluetooth.this.notifyAll();
123 // Reset the ConnectThread because we're done
124 connect_thread = null;
126 if (D) Log.d(TAG, "ConnectThread: Connect completed");
130 public void cancel() {
134 } catch (IOException e) {
135 if (D) Log.e(TAG, "ConnectThread: close() of connect socket failed", e);
140 public double frequency() {
144 public int telemetry_rate() {
145 return telemetry_rate;
148 public void save_frequency() {
149 AltosPreferences.set_frequency(0, frequency);
152 public void save_telemetry_rate() {
153 AltosPreferences.set_telemetry_rate(0, telemetry_rate);
156 private synchronized void wait_connected() throws InterruptedException, IOException {
158 if (D) Log.d(TAG, "wait_connected...");
160 if (D) Log.d(TAG, "wait_connected done");
161 if (input == null) throw new IOException();
165 private void connection_lost() {
166 if (D) Log.e(TAG, "Connection lost during I/O");
167 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED).sendToTarget();
170 public void print(String data) {
171 byte[] bytes = data.getBytes();
172 if (D) Log.d(TAG, "print(): begin");
176 if (D) Log.d(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
177 } catch (IOException e) {
179 } catch (InterruptedException e) {
184 public void putchar(byte c) {
185 byte[] bytes = { c };
186 if (D) Log.d(TAG, "print(): begin");
190 if (D) Log.d(TAG, "print(): Wrote byte: '" + c + "'");
191 } catch (IOException e) {
193 } catch (InterruptedException e) {
198 private static final int buffer_size = 1024;
200 private byte[] buffer = new byte[buffer_size];
201 private int buffer_len = 0;
202 private int buffer_off = 0;
204 public int getchar() {
205 while (buffer_off == buffer_len) {
208 buffer_len = input.read(buffer);
210 } catch (IOException e) {
212 return AltosLink.ERROR;
213 } catch (java.lang.InterruptedException e) {
215 return AltosLink.ERROR;
218 return buffer[buffer_off++];
221 public void close() {
222 if (D) Log.d(TAG, "close(): begin");
224 if (D) Log.d(TAG, "close(): synched");
226 if (connect_thread != null) {
227 if (D) Log.d(TAG, "close(): stopping connect_thread");
228 connect_thread.cancel();
229 connect_thread = null;
231 if (D) Log.d(TAG, "close(): Closing socket");
234 } catch (IOException e) {
235 if (D) Log.e(TAG, "close(): unable to close() socket");
237 if (input_thread != null) {
238 if (D) Log.d(TAG, "close(): stopping input_thread");
240 if (D) Log.d(TAG, "close(): input_thread.interrupt().....");
241 input_thread.interrupt();
242 if (D) Log.d(TAG, "close(): input_thread.join().....");
244 } catch (Exception e) {}
254 // We override this method so that we can add some debugging. Not 100% elegant, but more useful
255 // than debugging one char at a time above in getchar()!
256 public void add_reply(AltosLine line) throws InterruptedException {
257 if (D) Log.d(TAG, String.format("Got REPLY: %s", line.line));
258 super.add_reply(line);
261 //public void flush_output() { super.flush_output(); }
263 // Stubs of required methods when extending AltosLink
264 public boolean can_cancel_reply() { return false; }
265 public boolean show_reply_timeout() { return true; }
266 public void hide_reply_timeout() { }