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.Handler;
30 import android.util.Log;
32 import org.altusmetrum.AltosLib.*;
34 public class AltosBluetooth extends AltosLink {
37 private static final String TAG = "AltosBluetooth";
38 private static final boolean D = true;
40 private ConnectThread connect_thread = null;
41 private Thread input_thread = null;
43 private Handler handler;
45 private BluetoothAdapter adapter;
46 private BluetoothDevice device;
47 private BluetoothSocket socket;
48 private InputStream input;
49 private OutputStream output;
52 public AltosBluetooth(BluetoothDevice in_device, Handler in_handler) {
53 adapter = BluetoothAdapter.getDefaultAdapter();
57 connect_thread = new ConnectThread(device);
58 connect_thread.start();
60 input_thread = new Thread(this);
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.i(TAG, "ConnectThread: BEGIN");
80 setName("ConnectThread");
82 // Always cancel discovery because it will slow down a connection
83 adapter.cancelDiscovery();
85 // Make a connection to the BluetoothSocket
87 // This is a blocking call and will only return on a
88 // successful connection or an exception
90 } catch (IOException e) {
94 } catch (IOException e2) {
95 if (D) Log.e(TAG, "unable to close() socket during connection failure", e2);
102 synchronized (AltosBluetooth.this) {
103 input = socket.getInputStream();
104 output = socket.getOutputStream();
106 // Configure the newly connected device for telemetry
110 // Reset the ConnectThread because we're done
111 AltosBluetooth.this.notify();
112 connect_thread = null;
113 if (D) Log.i(TAG, "Completed connect");
115 } catch (Exception e) {
116 if (D) Log.e(TAG, "Failed to finish connection", e);
121 public void cancel() {
125 } catch (IOException e) {
126 if (D) Log.e(TAG, "ConnectThread: close() of connect socket failed", e);
131 private synchronized void wait_connected() throws InterruptedException {
132 if (D) Log.i(TAG, "wait_connected(): begin");
134 if (D) Log.i(TAG, "wait_connected(): waiting");
136 if (D) Log.i(TAG, "wait_connected(): wait ended..");
140 private void connection_failed() {
141 if (D) Log.e(TAG, "Bluetooth Socket IO failed!");
144 public void print(String data) {
145 byte[] bytes = data.getBytes();
146 if (D) Log.i(TAG, "print(): begin");
150 if (D) Log.i(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
151 } catch (IOException e) {
153 } catch (InterruptedException e) {
158 public int getchar() {
159 if (D) Log.i(TAG, "getchar(): begin");
162 if (D) Log.i(TAG, "getchar(): proceeding");
164 } catch (IOException e) {
166 } catch (java.lang.InterruptedException e) {
169 return AltosLink.ERROR;
172 public void close() {
173 if (D) Log.i(TAG, "close(): begin");
175 if (D) Log.i(TAG, "close(): synched");
177 if (connect_thread != null) {
178 if (D) Log.i(TAG, "close(): stopping connect_thread");
179 connect_thread.cancel();
180 connect_thread = null;
182 if (input_thread != null) {
183 if (D) Log.i(TAG, "close(): stopping input_thread");
185 if (D) Log.i(TAG, "close(): input_thread.interrupt().....");
186 input_thread.interrupt();
187 if (D) Log.i(TAG, "close(): input_thread.join().....");
189 } catch (Exception e) {}
196 //public void flush_output() { super.flush_output(); }
198 public boolean can_cancel_reply() { return false; }
199 public boolean show_reply_timeout() { return true; }
200 public void hide_reply_timeout() { }