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;
40 * This thread runs while attempting to make an outgoing connection
41 * with a device. It runs straight through; the connection either
45 private BluetoothAdapter adapter;
46 private ConnectThread connect_thread;
47 private BluetoothSocket socket;
48 private InputStream input;
49 private OutputStream output;
51 private class ConnectThread extends Thread {
52 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
54 public ConnectThread(BluetoothDevice device) {
55 BluetoothSocket tmp_socket = null;
58 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
59 } catch (IOException e) {
66 if (D) Log.i(TAG, "BEGIN ConnectThread");
67 setName("ConnectThread");
69 // Always cancel discovery because it will slow down a connection
70 adapter.cancelDiscovery();
72 // Make a connection to the BluetoothSocket
74 // This is a blocking call and will only return on a
75 // successful connection or an exception
77 } catch (IOException e) {
81 } catch (IOException e2) {
82 if (D) Log.e(TAG, "unable to close() socket during connection failure", e2);
89 synchronized (AltosBluetooth.this) {
90 input = socket.getInputStream();
91 output = socket.getOutputStream();
93 // Reset the ConnectThread because we're done
94 AltosBluetooth.this.notify();
95 connect_thread = null;
96 if (D) Log.i(TAG, "Completed connect");
98 } catch (Exception e) {
99 if (D) Log.e(TAG, "Failed to finish connection", e);
104 public void cancel() {
108 } catch (IOException e) {
109 if (D) Log.e(TAG, "close() of connect socket failed", e);
114 private synchronized void wait_connected() throws InterruptedException {
120 private void connection_failed() {
123 public void print(String data) {
124 byte[] bytes = data.getBytes();
128 } catch (IOException e) {
130 } catch (InterruptedException e) {
135 public int getchar() {
139 } catch (IOException e) {
141 } catch (java.lang.InterruptedException e) {
144 return AltosLink.ERROR;
147 public void close() {
149 if (connect_thread != null) {
150 connect_thread.cancel();
151 connect_thread = null;
156 public void flush_output() {
157 super.flush_output();
158 /* any local work needed to flush bluetooth? */
161 public boolean can_cancel_reply() {
164 public boolean show_reply_timeout() {
168 public void hide_reply_timeout() {
171 public AltosBluetooth(BluetoothDevice device) {
172 adapter = BluetoothAdapter.getDefaultAdapter();
173 connect_thread = new ConnectThread(device, true);
174 connect_thread.start();