2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosDroid;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.lang.reflect.Method;
24 import android.bluetooth.BluetoothAdapter;
25 import android.bluetooth.BluetoothDevice;
26 import android.bluetooth.BluetoothSocket;
27 import android.content.Context;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.os.Message;
31 import android.util.Log;
33 import org.altusmetrum.AltosLib.*;
35 public class AltosBluetooth extends AltosLink {
38 private static final String TAG = "AltosBluetooth";
39 private static final boolean D = true;
42 * This thread runs while attempting to make an outgoing connection
43 * with a device. It runs straight through; the connection either
47 private BluetoothAdapter adapter;
48 private ConnectThread connect_thread;
49 private BluetoothSocket socket;
50 private InputStream input;
51 private OutputStream output;
53 private class ConnectThread extends Thread {
54 private final BluetoothDevice mmDevice;
55 private String mSocketType;
56 BluetoothSocket tmp_socket;
58 public ConnectThread(BluetoothDevice device, boolean secure) {
60 mSocketType = secure ? "Secure" : "Insecure";
62 // Get a BluetoothSocket for a connection with the
63 // given BluetoothDevice
66 Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
67 tmp_socket = (BluetoothSocket) m.invoke(device, 2);
68 // tmp = device.createRfcommSocket(2);
70 Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
71 tmp_socket = (BluetoothSocket) m.invoke(device, 2);
72 // tmp = device.createInsecureRfcommSocket(2);
74 } catch (Exception e) {
75 Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
81 Log.i(TAG, "BEGIN connect_thread SocketType:" + mSocketType);
82 setName("ConnectThread" + mSocketType);
84 // Always cancel discovery because it will slow down a connection
85 adapter.cancelDiscovery();
87 // Make a connection to the BluetoothSocket
89 // This is a blocking call and will only return on a
90 // successful connection or an exception
92 } catch (IOException e) {
96 } catch (IOException e2) {
97 Log.e(TAG, "unable to close() " + mSocketType +
98 " socket during connection failure", e2);
105 synchronized (AltosBluetooth.this) {
106 input = tmp_socket.getInputStream();
107 output = tmp_socket.getOutputStream();
109 // Reset the ConnectThread because we're done
110 AltosBluetooth.this.notify();
111 connect_thread = null;
113 } catch (Exception e) {
114 Log.e(TAG, "Failed to finish connection", e);
119 public void cancel() {
121 if (tmp_socket != null)
123 } catch (IOException e) {
124 Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
129 private synchronized void wait_connected() throws InterruptedException {
135 private void connection_failed() {
138 public void print(String data) {
139 byte[] bytes = data.getBytes();
143 } catch (IOException e) {
145 } catch (InterruptedException e) {
150 public int getchar() {
154 } catch (IOException e) {
156 } catch (java.lang.InterruptedException e) {
159 return AltosLink.ERROR;
162 public void close() {
164 if (connect_thread != null) {
165 connect_thread.cancel();
166 connect_thread = null;
171 public void flush_output() {
172 super.flush_output();
173 /* any local work needed to flush bluetooth? */
176 public boolean can_cancel_reply() {
179 public boolean show_reply_timeout() {
183 public void hide_reply_timeout() {
186 public AltosBluetooth(BluetoothDevice device) {
187 adapter = BluetoothAdapter.getDefaultAdapter();
188 connect_thread = new ConnectThread(device, true);
189 connect_thread.start();