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;
33 import org.altusmetrum.altoslib_9.*;
35 public class AltosBluetooth extends AltosDroidLink {
37 private ConnectThread connect_thread = null;
39 private BluetoothDevice device;
40 private BluetoothSocket socket;
41 private InputStream input;
42 private OutputStream output;
43 private boolean pause;
46 public AltosBluetooth(BluetoothDevice device, Handler handler, boolean pause) {
49 this.handler = handler;
52 connect_thread = new ConnectThread();
53 connect_thread.start();
58 AltosDebug.debug("connected after closed");
62 AltosDebug.check_ui("connected\n");
66 input = socket.getInputStream();
67 output = socket.getOutputStream();
71 } catch (InterruptedException ie) {
73 } catch (IOException io) {
78 private void connect_failed() {
80 AltosDebug.debug("connect_failed after closed");
87 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget();
88 AltosDebug.error("ConnectThread: Failed to establish connection");
92 BluetoothSocket tmp_socket;
99 if (tmp_socket != null) {
102 } catch (IOException e) {
103 AltosDebug.error("close_socket failed");
108 public void close() {
114 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
116 private void create_socket(BluetoothDevice device) {
118 BluetoothSocket tmp_socket = null;
120 AltosDebug.check_ui("create_socket\n");
122 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
123 } catch (IOException e) {
126 if (socket != null) {
127 AltosDebug.debug("Socket already allocated %s", socket.toString());
130 synchronized (this) {
135 private class ConnectThread extends Thread {
138 AltosDebug.debug("ConnectThread: BEGIN (pause %b)", pause);
139 setName("ConnectThread");
144 } catch (InterruptedException e) {
148 create_socket(device);
149 // Always cancel discovery because it will slow down a connection
151 BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
152 } catch (Exception e) {
153 AltosDebug.debug("cancelDiscovery exception %s", e.toString());
156 BluetoothSocket local_socket = null;
158 synchronized (AltosBluetooth.this) {
160 local_socket = socket;
163 if (local_socket != null) {
165 // Make a connection to the BluetoothSocket
166 // This is a blocking call and will only return on a
167 // successful connection or an exception
168 local_socket.connect();
169 } catch (Exception e) {
170 AltosDebug.debug("Connect exception %s", e.toString());
172 local_socket.close();
173 } catch (Exception ce) {
174 AltosDebug.debug("Close exception %s", ce.toString());
180 if (local_socket != null) {
186 AltosDebug.debug("ConnectThread: completed");
190 private synchronized void wait_connected() throws InterruptedException, IOException {
191 AltosDebug.check_ui("wait_connected\n");
192 if (input == null && socket != null) {
193 AltosDebug.debug("wait_connected...");
195 AltosDebug.debug("wait_connected done");
198 throw new IOException();
201 int write(byte[] buffer, int len) {
203 output.write(buffer, 0, len);
204 } catch (IOException ie) {
210 int read(byte[] buffer, int len) {
212 return input.read(buffer, 0, len);
213 } catch (IOException ie) {
218 // Stubs of required methods when extending AltosLink
219 public boolean can_cancel_reply() { return false; }
220 public boolean show_reply_timeout() { return true; }
221 public void hide_reply_timeout() { }