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.util.Log;
31 import org.altusmetrum.AltosLib.*;
33 public class AltosBluetooth extends AltosLink {
36 private static final String TAG = "AltosBluetooth";
37 private static final boolean D = true;
39 private ConnectThread connect_thread = null;
40 private Thread input_thread = null;
42 private BluetoothAdapter adapter;
43 private BluetoothDevice device;
44 private BluetoothSocket socket;
45 private InputStream input;
46 private OutputStream output;
49 public AltosBluetooth(BluetoothDevice in_device) {
50 adapter = BluetoothAdapter.getDefaultAdapter();
53 connect_thread = new ConnectThread(device);
54 connect_thread.start();
56 input_thread = new Thread(this);
60 private class ConnectThread extends Thread {
61 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
63 public ConnectThread(BluetoothDevice device) {
64 BluetoothSocket tmp_socket = null;
67 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
68 } catch (IOException e) {
75 if (D) Log.i(TAG, "BEGIN ConnectThread");
76 setName("ConnectThread");
78 // Always cancel discovery because it will slow down a connection
79 adapter.cancelDiscovery();
81 // Make a connection to the BluetoothSocket
83 // This is a blocking call and will only return on a
84 // successful connection or an exception
86 } catch (IOException e) {
90 } catch (IOException e2) {
91 if (D) Log.e(TAG, "unable to close() socket during connection failure", e2);
98 synchronized (AltosBluetooth.this) {
99 input = socket.getInputStream();
100 output = socket.getOutputStream();
102 // Reset the ConnectThread because we're done
103 AltosBluetooth.this.notify();
104 connect_thread = null;
105 if (D) Log.i(TAG, "Completed connect");
107 } catch (Exception e) {
108 if (D) Log.e(TAG, "Failed to finish connection", e);
113 public void cancel() {
117 } catch (IOException e) {
118 if (D) Log.e(TAG, "close() of connect socket failed", e);
123 private synchronized void wait_connected() throws InterruptedException {
129 private void connection_failed() {
132 public void print(String data) {
133 byte[] bytes = data.getBytes();
137 } catch (IOException e) {
139 } catch (InterruptedException e) {
144 public int getchar() {
148 } catch (IOException e) {
150 } catch (java.lang.InterruptedException e) {
153 return AltosLink.ERROR;
156 public void close() {
158 if (connect_thread != null) {
159 connect_thread.cancel();
160 connect_thread = null;
162 if (input_thread != null) {
164 input_thread.interrupt();
166 } catch (Exception e) {}
173 //public void flush_output() { super.flush_output(); }
175 public boolean can_cancel_reply() { return false; }
176 public boolean show_reply_timeout() { return true; }
177 public void hide_reply_timeout() { }