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; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 package org.altusmetrum.AltosDroid;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.UUID;
27 import android.bluetooth.BluetoothAdapter;
28 import android.bluetooth.BluetoothDevice;
29 import android.bluetooth.BluetoothSocket;
30 //import android.os.Bundle;
31 import android.os.Handler;
32 //import android.os.Message;
34 import org.altusmetrum.altoslib_13.*;
36 public class AltosBluetooth extends AltosDroidLink {
38 private ConnectThread connect_thread = null;
40 private BluetoothDevice device;
41 private BluetoothSocket socket;
42 private InputStream input;
43 private OutputStream output;
44 private boolean pause;
47 public AltosBluetooth(BluetoothDevice device, Handler handler, boolean pause) {
50 this.handler = handler;
53 connect_thread = new ConnectThread();
54 connect_thread.start();
59 AltosDebug.debug("connected after closed");
63 AltosDebug.check_ui("connected\n");
67 input = socket.getInputStream();
68 output = socket.getOutputStream();
72 } catch (InterruptedException ie) {
74 } catch (IOException io) {
79 private void connect_failed() {
81 AltosDebug.debug("connect_failed after closed");
88 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget();
89 AltosDebug.error("ConnectThread: Failed to establish connection");
93 BluetoothSocket tmp_socket;
100 if (tmp_socket != null) {
103 } catch (IOException e) {
104 AltosDebug.error("close_socket failed");
109 public void close() {
115 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
117 private void create_socket(BluetoothDevice device) {
119 BluetoothSocket tmp_socket = null;
121 AltosDebug.check_ui("create_socket\n");
123 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
124 } catch (IOException e) {
127 if (socket != null) {
128 AltosDebug.debug("Socket already allocated %s", socket.toString());
131 synchronized (this) {
136 private class ConnectThread extends Thread {
139 AltosDebug.debug("ConnectThread: BEGIN (pause %b)", pause);
140 setName("ConnectThread");
145 } catch (InterruptedException e) {
149 create_socket(device);
150 // Always cancel discovery because it will slow down a connection
152 BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
153 } catch (Exception e) {
154 AltosDebug.debug("cancelDiscovery exception %s", e.toString());
157 BluetoothSocket local_socket = null;
159 synchronized (AltosBluetooth.this) {
161 local_socket = socket;
164 if (local_socket != null) {
166 // Make a connection to the BluetoothSocket
167 // This is a blocking call and will only return on a
168 // successful connection or an exception
169 local_socket.connect();
170 } catch (Exception e) {
171 AltosDebug.debug("Connect exception %s", e.toString());
173 local_socket.close();
174 } catch (Exception ce) {
175 AltosDebug.debug("Close exception %s", ce.toString());
181 if (local_socket != null) {
187 AltosDebug.debug("ConnectThread: completed");
191 private synchronized void wait_connected() throws InterruptedException, IOException {
192 AltosDebug.check_ui("wait_connected\n");
193 if (input == null && socket != null) {
194 AltosDebug.debug("wait_connected...");
196 AltosDebug.debug("wait_connected done");
199 throw new IOException();
202 int write(byte[] buffer, int len) {
206 output.write(buffer, 0, len);
207 } catch (IOException ie) {
213 int read(byte[] buffer, int len) {
217 return input.read(buffer, 0, len);
218 } catch (IOException ie) {
223 // Stubs of required methods when extending AltosLink
224 public boolean can_cancel_reply() { return false; }
225 public boolean show_reply_timeout() { return true; }
226 public void hide_reply_timeout() { }