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.*;
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();
62 input_thread = new Thread(this);
66 private class ConnectThread extends Thread {
67 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
69 public ConnectThread(BluetoothDevice device) {
70 BluetoothSocket tmp_socket = null;
73 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
74 } catch (IOException e) {
81 if (D) Log.i(TAG, "ConnectThread: BEGIN");
82 setName("ConnectThread");
84 // Always cancel discovery because it will slow down a connection
85 adapter.cancelDiscovery();
87 synchronized (AltosBluetooth.this) {
88 // Make a connection to the BluetoothSocket
90 // This is a blocking call and will only return on a
91 // successful connection or an exception
94 input = socket.getInputStream();
95 output = socket.getOutputStream();
96 } catch (IOException e) {
100 } catch (IOException e2) {
101 if (D) Log.e(TAG, "ConnectThread: Failed to close() socket after failed connection");
105 AltosBluetooth.this.notifyAll();
106 if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
110 // Configure the newly connected device for telemetry
114 // Reset the ConnectThread because we're done
115 connect_thread = null;
117 // Send the device name back to the Telemetry Service
118 name = device.getName();
119 Message msg = handler.obtainMessage(TelemetryService.MSG_CONNECTED);
120 Bundle b = new Bundle();
121 b.putString(TelemetryService.KEY_DEVNAME, name);
123 handler.sendMessage(msg);
125 // Notify other waiting threads, now that we're connected
126 AltosBluetooth.this.notifyAll();
128 if (D) Log.i(TAG, "ConnectThread: Connect completed");
132 public void cancel() {
136 } catch (IOException e) {
137 if (D) Log.e(TAG, "ConnectThread: close() of connect socket failed", e);
142 private synchronized void wait_connected() throws InterruptedException, IOException {
143 if (D) Log.i(TAG, "wait_connected(): begin");
145 if (D) Log.i(TAG, "wait_connected(): waiting");
147 if (D) Log.i(TAG, "wait_connected(): wait ended..");
148 if (input == null) throw new IOException();
152 private void connection_failed() {
153 if (D) Log.e(TAG, "Bluetooth Socket IO failed!");
156 public void print(String data) {
157 byte[] bytes = data.getBytes();
158 if (D) Log.i(TAG, "print(): begin");
162 if (D) Log.i(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
163 } catch (IOException e) {
165 } catch (InterruptedException e) {
170 public int getchar() {
171 if (D) Log.i(TAG, "getchar(): begin");
174 if (D) Log.i(TAG, "getchar(): proceeding");
176 } catch (IOException e) {
178 } catch (java.lang.InterruptedException e) {
181 return AltosLink.ERROR;
184 public void close() {
185 if (D) Log.i(TAG, "close(): begin");
187 if (D) Log.i(TAG, "close(): synched");
189 if (connect_thread != null) {
190 if (D) Log.i(TAG, "close(): stopping connect_thread");
191 connect_thread.cancel();
192 connect_thread = null;
194 if (D) Log.i(TAG, "close(): Closing socket");
197 } catch (IOException e) {
198 if (D) Log.e(TAG, "close(): unable to close() socket");
200 if (input_thread != null) {
201 if (D) Log.i(TAG, "close(): stopping input_thread");
203 if (D) Log.i(TAG, "close(): input_thread.interrupt().....");
204 input_thread.interrupt();
205 if (D) Log.i(TAG, "close(): input_thread.join().....");
207 } catch (Exception e) {}
217 //public void flush_output() { super.flush_output(); }
219 public boolean can_cancel_reply() { return false; }
220 public boolean show_reply_timeout() { return true; }
221 public void hide_reply_timeout() { }