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 BluetoothAdapter adapter;
45 private BluetoothSocket socket;
46 private InputStream input;
47 private OutputStream output;
50 public AltosBluetooth(BluetoothDevice device, Handler handler) {
53 adapter = BluetoothAdapter.getDefaultAdapter();
54 this.handler = handler;
56 create_socket(device);
57 connect_thread = new ConnectThread();
58 connect_thread.start();
63 if (D) Log.d(TAG, "connected after closed");
70 input = socket.getInputStream();
71 output = socket.getOutputStream();
75 } catch (InterruptedException ie) {
77 } catch (IOException io) {
82 private void connect_failed() {
84 if (D) Log.d(TAG, "connect_failed after closed");
91 handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget();
92 if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
96 BluetoothSocket tmp_socket;
103 if (tmp_socket != null) {
106 } catch (IOException e) {
107 if (D) Log.e(TAG, "close_socket failed");
112 public void close() {
118 private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
120 private void create_socket(BluetoothDevice device) {
122 BluetoothSocket tmp_socket = null;
125 tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
126 } catch (IOException e) {
129 if (socket != null) {
130 if (D) Log.d(TAG, String.format("Socket already allocated %s", socket.toString()));
133 synchronized (this) {
138 private class ConnectThread extends Thread {
141 if (D) Log.d(TAG, "ConnectThread: BEGIN");
142 setName("ConnectThread");
144 // Always cancel discovery because it will slow down a connection
146 adapter.cancelDiscovery();
147 } catch (Exception e) {
148 if (D) Log.d(TAG, String.format("cancelDiscovery exception %s", e.toString()));
151 BluetoothSocket local_socket = null;
153 synchronized (AltosBluetooth.this) {
155 local_socket = socket;
158 if (local_socket != null) {
160 // Make a connection to the BluetoothSocket
161 // This is a blocking call and will only return on a
162 // successful connection or an exception
163 local_socket.connect();
164 } catch (IOException e) {
165 if (D) Log.d(TAG, String.format("Connect exception %s", e.toString()));
170 if (local_socket != null) {
176 if (D) Log.d(TAG, "ConnectThread: completed");
180 private synchronized void wait_connected() throws InterruptedException, IOException {
181 if (input == null && socket != null) {
182 if (D) Log.d(TAG, "wait_connected...");
184 if (D) Log.d(TAG, "wait_connected done");
187 throw new IOException();
190 int write(byte[] buffer, int len) {
192 output.write(buffer, 0, len);
193 } catch (IOException ie) {
199 int read(byte[] buffer, int len) {
201 return input.read(buffer, 0, len);
202 } catch (IOException ie) {
207 // Stubs of required methods when extending AltosLink
208 public boolean can_cancel_reply() { return false; }
209 public boolean show_reply_timeout() { return true; }
210 public void hide_reply_timeout() { }