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_7.*;
36 public class AltosBluetooth extends AltosDroidLink {
39 private static final String TAG = "AltosBluetooth";
40 private static final boolean D = true;
42 private ConnectThread connect_thread = null;
44 private BluetoothDevice device;
45 private BluetoothSocket socket;
46 private InputStream input;
47 private OutputStream output;
48 private boolean pause;
51 public AltosBluetooth(BluetoothDevice device, Handler handler, boolean pause) {
54 this.handler = handler;
57 connect_thread = new ConnectThread();
58 connect_thread.start();
63 if (D) Log.d(TAG, "connected after closed");
67 AltosDebug.check_ui("connected\n");
71 input = socket.getInputStream();
72 output = socket.getOutputStream();
76 } catch (InterruptedException ie) {
78 } catch (IOException io) {
83 private void connect_failed() {
85 if (D) Log.d(TAG, "connect_failed after closed");
92 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget();
93 if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
97 BluetoothSocket tmp_socket;
104 if (tmp_socket != null) {
107 } catch (IOException e) {
108 if (D) Log.e(TAG, "close_socket failed");
113 public void close() {
119 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
121 private void create_socket(BluetoothDevice device) {
123 BluetoothSocket tmp_socket = null;
126 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
127 } catch (IOException e) {
130 if (socket != null) {
131 if (D) Log.d(TAG, String.format("Socket already allocated %s", socket.toString()));
134 synchronized (this) {
139 private class ConnectThread extends Thread {
142 if (D) Log.d(TAG, "ConnectThread: BEGIN");
143 setName("ConnectThread");
148 } catch (InterruptedException e) {
152 create_socket(device);
153 // Always cancel discovery because it will slow down a connection
155 BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
156 } catch (Exception e) {
157 if (D) Log.d(TAG, String.format("cancelDiscovery exception %s", e.toString()));
160 BluetoothSocket local_socket = null;
162 synchronized (AltosBluetooth.this) {
164 local_socket = socket;
167 if (local_socket != null) {
169 // Make a connection to the BluetoothSocket
170 // This is a blocking call and will only return on a
171 // successful connection or an exception
172 local_socket.connect();
173 } catch (IOException e) {
174 if (D) Log.d(TAG, String.format("Connect exception %s", e.toString()));
179 if (local_socket != null) {
185 if (D) Log.d(TAG, "ConnectThread: completed");
189 private synchronized void wait_connected() throws InterruptedException, IOException {
190 if (input == null && socket != null) {
191 if (D) Log.d(TAG, "wait_connected...");
193 if (D) Log.d(TAG, "wait_connected done");
196 throw new IOException();
199 int write(byte[] buffer, int len) {
201 output.write(buffer, 0, len);
202 } catch (IOException ie) {
208 int read(byte[] buffer, int len) {
210 return input.read(buffer, 0, len);
211 } catch (IOException ie) {
216 // Stubs of required methods when extending AltosLink
217 public boolean can_cancel_reply() { return false; }
218 public boolean show_reply_timeout() { return true; }
219 public void hide_reply_timeout() { }