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_4.*;
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) {
55 adapter = BluetoothAdapter.getDefaultAdapter();
59 connect_thread = new ConnectThread(device);
60 connect_thread.start();
64 private class ConnectThread extends Thread {
65 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
67 public ConnectThread(BluetoothDevice device) {
68 BluetoothSocket tmp_socket = null;
71 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
72 } catch (IOException e) {
79 if (D) Log.d(TAG, "ConnectThread: BEGIN");
80 setName("ConnectThread");
82 // Always cancel discovery because it will slow down a connection
83 adapter.cancelDiscovery();
85 synchronized (AltosBluetooth.this) {
86 // Make a connection to the BluetoothSocket
88 // This is a blocking call and will only return on a
89 // successful connection or an exception
92 input = socket.getInputStream();
93 output = socket.getOutputStream();
94 } catch (IOException e) {
98 } catch (IOException e2) {
99 if (D) Log.e(TAG, "ConnectThread: Failed to close() socket after failed connection");
103 AltosBluetooth.this.notifyAll();
104 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED).sendToTarget();
105 if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
109 input_thread = new Thread(AltosBluetooth.this);
110 input_thread.start();
112 // Configure the newly connected device for telemetry
116 // Let TelemetryService know we're connected
117 handler.obtainMessage(TelemetryService.MSG_CONNECTED).sendToTarget();
119 // Notify other waiting threads, now that we're connected
120 AltosBluetooth.this.notifyAll();
122 // Reset the ConnectThread because we're done
123 connect_thread = null;
125 if (D) Log.d(TAG, "ConnectThread: Connect completed");
129 public void cancel() {
133 } catch (IOException e) {
134 if (D) Log.e(TAG, "ConnectThread: close() of connect socket failed", e);
139 private synchronized void wait_connected() throws InterruptedException, IOException {
142 if (input == null) throw new IOException();
146 private void connection_lost() {
147 if (D) Log.e(TAG, "Connection lost during I/O");
148 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED).sendToTarget();
151 public void print(String data) {
152 byte[] bytes = data.getBytes();
153 if (D) Log.d(TAG, "print(): begin");
157 if (D) Log.d(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
158 } catch (IOException e) {
160 } catch (InterruptedException e) {
165 public void putchar(byte c) {
166 byte[] bytes = { c };
167 if (D) Log.d(TAG, "print(): begin");
171 if (D) Log.d(TAG, "print(): Wrote byte: '" + c + "'");
172 } catch (IOException e) {
174 } catch (InterruptedException e) {
179 public int getchar() {
183 } catch (IOException e) {
185 } catch (java.lang.InterruptedException e) {
188 return AltosLink.ERROR;
191 public void close() {
192 if (D) Log.d(TAG, "close(): begin");
194 if (D) Log.d(TAG, "close(): synched");
196 if (connect_thread != null) {
197 if (D) Log.d(TAG, "close(): stopping connect_thread");
198 connect_thread.cancel();
199 connect_thread = null;
201 if (D) Log.d(TAG, "close(): Closing socket");
204 } catch (IOException e) {
205 if (D) Log.e(TAG, "close(): unable to close() socket");
207 if (input_thread != null) {
208 if (D) Log.d(TAG, "close(): stopping input_thread");
210 if (D) Log.d(TAG, "close(): input_thread.interrupt().....");
211 input_thread.interrupt();
212 if (D) Log.d(TAG, "close(): input_thread.join().....");
214 } catch (Exception e) {}
224 // We override this method so that we can add some debugging. Not 100% elegant, but more useful
225 // than debugging one char at a time above in getchar()!
226 public void add_reply(AltosLine line) throws InterruptedException {
227 if (D) Log.d(TAG, String.format("Got REPLY: %s", line.line));
228 super.add_reply(line);
231 //public void flush_output() { super.flush_output(); }
233 // Stubs of required methods when extending AltosLink
234 public boolean can_cancel_reply() { return false; }
235 public boolean show_reply_timeout() { return true; }
236 public void hide_reply_timeout() { }