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 BluetoothSocket socket;
49 private InputStream input;
50 private OutputStream output;
53 public AltosBluetooth(BluetoothDevice device, Handler handler) {
55 adapter = BluetoothAdapter.getDefaultAdapter();
56 this.handler = handler;
58 create_socket(device);
59 connect_thread = new ConnectThread();
60 connect_thread.start();
63 private Object closed_lock = new Object();
64 private boolean closed = false;
66 private boolean closed() {
67 synchronized(closed_lock) {
72 private void connected() {
74 if (D) Log.d(TAG, "connected after closed");
81 input = socket.getInputStream();
82 output = socket.getOutputStream();
84 input_thread = new Thread(this);
87 // Configure the newly connected device for telemetry
90 if (D) Log.d(TAG, "ConnectThread: connected");
92 /* Let TelemetryService know we're connected
94 handler.obtainMessage(TelemetryService.MSG_CONNECTED, this).sendToTarget();
96 /* Notify other waiting threads that we're connected now
101 } catch (IOException io) {
106 private void connect_failed() {
108 if (D) Log.d(TAG, "connect_failed after closed");
115 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget();
116 if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
119 private void disconnected() {
121 if (D) Log.d(TAG, "disconnected after closed");
125 if (D) Log.d(TAG, "Sending disconnected message");
126 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
129 private void close_socket() {
130 BluetoothSocket tmp_socket;
137 if (tmp_socket != null) {
140 } catch (IOException e) {
141 if (D) Log.e(TAG, "close_socket failed");
146 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
148 private void create_socket(BluetoothDevice device) {
150 BluetoothSocket tmp_socket = null;
153 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
154 } catch (IOException e) {
157 if (socket != null) {
158 if (D) Log.d(TAG, String.format("Socket already allocated %s", socket.toString()));
161 synchronized (this) {
166 private class ConnectThread extends Thread {
169 if (D) Log.d(TAG, "ConnectThread: BEGIN");
170 setName("ConnectThread");
172 // Always cancel discovery because it will slow down a connection
174 adapter.cancelDiscovery();
175 } catch (Exception e) {
176 if (D) Log.d(TAG, String.format("cancelDiscovery exception %s", e.toString()));
179 BluetoothSocket local_socket = null;
181 synchronized (AltosBluetooth.this) {
183 local_socket = socket;
186 if (local_socket != null) {
188 // Make a connection to the BluetoothSocket
189 // This is a blocking call and will only return on a
190 // successful connection or an exception
191 local_socket.connect();
192 } catch (IOException e) {
193 if (D) Log.d(TAG, String.format("Connect exception %s", e.toString()));
198 if (local_socket != null) {
204 if (D) Log.d(TAG, "ConnectThread: completed");
208 public double frequency() {
212 public int telemetry_rate() {
213 return telemetry_rate;
216 public void save_frequency() {
217 AltosPreferences.set_frequency(0, frequency);
220 public void save_telemetry_rate() {
221 AltosPreferences.set_telemetry_rate(0, telemetry_rate);
224 private synchronized void wait_connected() throws InterruptedException, IOException {
225 if (input == null && socket != null) {
226 if (D) Log.d(TAG, "wait_connected...");
228 if (D) Log.d(TAG, "wait_connected done");
231 throw new IOException();
234 public void print(String data) {
235 byte[] bytes = data.getBytes();
236 if (D) Log.d(TAG, "print(): begin");
240 if (D) Log.d(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
241 } catch (IOException e) {
243 } catch (InterruptedException e) {
248 public void putchar(byte c) {
249 byte[] bytes = { c };
250 if (D) Log.d(TAG, "print(): begin");
254 if (D) Log.d(TAG, "print(): Wrote byte: '" + c + "'");
255 } catch (IOException e) {
257 } catch (InterruptedException e) {
262 private static final int buffer_size = 1024;
264 private byte[] buffer = new byte[buffer_size];
265 private int buffer_len = 0;
266 private int buffer_off = 0;
268 private byte[] debug_chars = new byte[buffer_size];
269 private int debug_off;
271 private void debug_input(byte b) {
273 Log.d(TAG, " " + new String(debug_chars, 0, debug_off));
276 if (debug_off < buffer_size)
277 debug_chars[debug_off++] = b;
281 public int getchar() {
282 while (buffer_off == buffer_len) {
285 buffer_len = input.read(buffer);
287 } catch (IOException e) {
288 if (D) Log.d(TAG, "getchar IOException");
290 return AltosLink.ERROR;
291 } catch (java.lang.InterruptedException e) {
292 if (D) Log.d(TAG, "getchar Interrupted");
294 return AltosLink.ERROR;
298 debug_input(buffer[buffer_off]);
299 return buffer[buffer_off++];
302 public void closing() {
303 synchronized(closed_lock) {
304 if (D) Log.d(TAG, "Marked closed true");
310 public void close() {
311 if (D) Log.d(TAG, "close(): begin");
319 if (input_thread != null) {
320 if (D) Log.d(TAG, "close(): stopping input_thread");
322 if (D) Log.d(TAG, "close(): input_thread.interrupt().....");
323 input_thread.interrupt();
324 if (D) Log.d(TAG, "close(): input_thread.join().....");
326 } catch (Exception e) {}
335 //public void flush_output() { super.flush_output(); }
337 // Stubs of required methods when extending AltosLink
338 public boolean can_cancel_reply() { return false; }
339 public boolean show_reply_timeout() { return true; }
340 public void hide_reply_timeout() { }