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.Handler;
32 public class AltosBluetooth extends AltosDroidLink {
34 private ConnectThread connect_thread = null;
36 private BluetoothDevice device;
37 private BluetoothSocket socket;
38 private InputStream input;
39 private OutputStream output;
40 private boolean pause;
43 public AltosBluetooth(BluetoothDevice device, Handler handler, boolean pause) {
46 this.handler = handler;
49 connect_thread = new ConnectThread();
50 connect_thread.start();
55 AltosDebug.debug("connected after closed");
59 AltosDebug.check_ui("connected\n");
63 input = socket.getInputStream();
64 output = socket.getOutputStream();
68 } catch (InterruptedException ie) {
70 } catch (IOException io) {
75 private void connect_failed() {
77 AltosDebug.debug("connect_failed after closed");
84 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget();
85 AltosDebug.error("ConnectThread: Failed to establish connection");
89 BluetoothSocket tmp_socket;
96 if (tmp_socket != null) {
99 } catch (IOException e) {
100 AltosDebug.error("close_socket failed");
105 public void close() {
111 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
113 private void create_socket(BluetoothDevice device) {
115 BluetoothSocket tmp_socket = null;
117 AltosDebug.check_ui("create_socket\n");
119 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
120 } catch (IOException e) {
123 if (socket != null) {
124 AltosDebug.debug("Socket already allocated %s", socket.toString());
127 synchronized (this) {
132 private class ConnectThread extends Thread {
135 AltosDebug.debug("ConnectThread: BEGIN (pause %b)", pause);
136 setName("ConnectThread");
141 } catch (InterruptedException e) {
145 create_socket(device);
146 // Always cancel discovery because it will slow down a connection
148 BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
149 } catch (Exception e) {
150 AltosDebug.debug("cancelDiscovery exception %s", e.toString());
153 BluetoothSocket local_socket = null;
155 synchronized (AltosBluetooth.this) {
157 local_socket = socket;
160 if (local_socket != null) {
162 // Make a connection to the BluetoothSocket
163 // This is a blocking call and will only return on a
164 // successful connection or an exception
165 local_socket.connect();
166 } catch (Exception e) {
167 AltosDebug.debug("Connect exception %s", e.toString());
169 local_socket.close();
170 } catch (Exception ce) {
171 AltosDebug.debug("Close exception %s", ce.toString());
177 if (local_socket != null) {
183 AltosDebug.debug("ConnectThread: completed");
187 private synchronized void wait_connected() throws InterruptedException, IOException {
188 AltosDebug.check_ui("wait_connected\n");
189 if (input == null && socket != null) {
190 AltosDebug.debug("wait_connected...");
192 AltosDebug.debug("wait_connected done");
195 throw new IOException();
198 int write(byte[] buffer, int len) {
202 output.write(buffer, 0, len);
203 } catch (IOException ie) {
209 int read(byte[] buffer, int len) {
213 return input.read(buffer, 0, len);
214 } catch (IOException ie) {
219 // Stubs of required methods when extending AltosLink
220 public boolean can_cancel_reply() { return false; }
221 public boolean show_reply_timeout() { return true; }
222 public void hide_reply_timeout() { }