diff --git a/androidgcs/AndroidManifest.xml b/androidgcs/AndroidManifest.xml
index 8b3a9e4b3..6c89f2632 100644
--- a/androidgcs/AndroidManifest.xml
+++ b/androidgcs/AndroidManifest.xml
@@ -18,5 +18,6 @@
+
\ No newline at end of file
diff --git a/androidgcs/src/org/openpilot/androidgcs/BluetoothUAVTalk.java b/androidgcs/src/org/openpilot/androidgcs/BluetoothUAVTalk.java
new file mode 100644
index 000000000..b240f3adc
--- /dev/null
+++ b/androidgcs/src/org/openpilot/androidgcs/BluetoothUAVTalk.java
@@ -0,0 +1,146 @@
+package org.openpilot.androidgcs;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.UUID;
+
+import org.openpilot.uavtalk.UAVObjectManager;
+import org.openpilot.uavtalk.UAVTalk;
+
+import android.app.Activity;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothSocket;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+public class BluetoothUAVTalk {
+ private final String TAG = "BluetoothUAVTalk";
+ public static int LOGLEVEL = 2;
+ public static boolean WARN = LOGLEVEL > 1;
+ public static boolean DEBUG = LOGLEVEL > 0;
+
+ // Temporarily define fixed device name
+ public final static String DEVICE_NAME = "RN42-222D";
+ private final static UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
+
+ private BluetoothAdapter mBluetoothAdapter;
+ private BluetoothSocket socket;
+ private BluetoothDevice device;
+ private UAVTalk uavTalk;
+ private boolean connected;
+
+ public BluetoothUAVTalk(Context caller, String deviceName) {
+ if (DEBUG) Log.d(TAG, "Trying to open UAVTalk with " + deviceName);
+
+ connected = false;
+ device = null;
+
+ mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+ if (mBluetoothAdapter == null) {
+ // Device does not support Bluetooth
+ Log.e(TAG, "Device does not support Bluetooth");
+ return;
+ }
+
+ if (!mBluetoothAdapter.isEnabled()) {
+ // Enable bluetooth if it isn't already
+ Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
+ caller.sendOrderedBroadcast(enableBtIntent, "android.permission.BLUETOOTH_ADMIN", new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Log.e(TAG,"Received " + context + intent);
+ //TODO: some logic here to see if it worked
+ queryDevices();
+ }
+ }, null, Activity.RESULT_OK, null, null);
+ } else {
+ queryDevices();
+ }
+ }
+
+ public boolean connect(UAVObjectManager objMngr) {
+ if( getConnected() )
+ return true;
+ if( !getFoundDevice() )
+ return false;
+ if( !openTelemetryBluetooth(objMngr) )
+ return false;
+ return true;
+ }
+
+ public boolean getConnected() {
+ return connected;
+ }
+
+ public boolean getFoundDevice() {
+ return (device != null);
+ }
+
+ public UAVTalk getUavtalk() {
+ return uavTalk;
+ }
+
+ private void queryDevices() {
+ Log.d(TAG, "Searching for devices");
+ Set pairedDevices = mBluetoothAdapter.getBondedDevices();
+ // If there are paired devices
+ if (pairedDevices.size() > 0) {
+ // Loop through paired devices
+ for (BluetoothDevice device : pairedDevices) {
+ // Add the name and address to an array adapter to show in a ListView
+ //mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
+ Log.d(TAG, "Paired device: " + device.getName());
+ if(device.getName().compareTo(DEVICE_NAME) == 0) {
+ this.device = device;
+ return;
+ }
+ }
+ }
+
+ }
+
+ private boolean openTelemetryBluetooth(UAVObjectManager objMngr) {
+ Log.d(TAG, "Opening conncetion to " + device.getName());
+ socket = null;
+ connected = false;
+ try {
+ socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
+ } catch (IOException e) {
+ Log.e(TAG,"Unable to create Rfcomm socket");
+ return false;
+ //e.printStackTrace();
+ }
+
+ mBluetoothAdapter.cancelDiscovery();
+
+ try {
+ socket.connect();
+ }
+ catch (IOException e) {
+ Log.e(TAG,"Unable to connect to requested device", e);
+ try {
+ socket.close();
+ } catch (IOException e2) {
+ Log.e(TAG, "unable to close() socket during connection failure", e2);
+ }
+ return false;
+ }
+
+ connected = true;
+
+ try {
+ uavTalk = new UAVTalk(socket.getInputStream(), socket.getOutputStream(), objMngr);
+ } catch (IOException e) {
+ Log.e(TAG,"Error starting UAVTalk");
+ // TODO Auto-generated catch block
+ //e.printStackTrace();
+ return false;
+ }
+
+ return true;
+ }
+
+}
diff --git a/androidgcs/src/org/openpilot/androidgcs/OPTelemetryService.java b/androidgcs/src/org/openpilot/androidgcs/OPTelemetryService.java
new file mode 100644
index 000000000..2994be6d2
--- /dev/null
+++ b/androidgcs/src/org/openpilot/androidgcs/OPTelemetryService.java
@@ -0,0 +1,88 @@
+package org.openpilot.androidgcs;
+
+import org.openpilot.uavtalk.Telemetry;
+import org.openpilot.uavtalk.TelemetryMonitor;
+import org.openpilot.uavtalk.UAVObjectManager;
+import org.openpilot.uavtalk.UAVTalk;
+import org.openpilot.uavtalk.uavobjects.UAVObjectsInitialize;
+
+import android.app.Service;
+import android.content.Intent;
+import android.os.IBinder;
+import android.os.Looper;
+import android.util.Log;
+
+public class OPTelemetryService extends Service {
+ private final String TAG = "OPTElemetryService";
+ public static int LOGLEVEL = 2;
+ public static boolean WARN = LOGLEVEL > 1;
+ public static boolean DEBUG = LOGLEVEL > 0;
+
+ private UAVObjectManager objMngr;
+ private UAVTalk uavTalk;
+ private Telemetry tel;
+ private TelemetryMonitor mon;
+
+ @Override
+ public IBinder onBind(Intent arg0) {
+ return null;
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+
+ if (DEBUG) Log.d(TAG, "Telemetry Service started");
+
+ Thread telemetryThread = new Thread(telemetryRunnable);
+ telemetryThread.start();
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ }
+
+ private final Runnable telemetryRunnable = new Runnable() {
+
+ public void run() {
+ Looper.prepare();
+
+ if (DEBUG) Log.d(TAG, "Telemetry Thread started");
+
+ objMngr = new UAVObjectManager();
+ UAVObjectsInitialize.register(objMngr);
+
+ BluetoothUAVTalk bt = new BluetoothUAVTalk(OPTelemetryService.this, BluetoothUAVTalk.DEVICE_NAME);
+ while(true) {
+ if (DEBUG) Log.d(TAG, "Attempting Bluetooth Connection");
+ if(! bt.getConnected() )
+ bt.connect(objMngr);
+ else
+ break;
+ Looper.loop();
+ }
+
+ if (DEBUG) Log.d(TAG, "Connected via bluetooth");
+
+ uavTalk = bt.getUavtalk();
+ tel = new Telemetry(uavTalk, objMngr);
+ mon = new TelemetryMonitor(objMngr,tel);
+
+ if (DEBUG) Log.d(TAG, "Entering UAVTalk processing loop");
+ while(true) {
+ if( !uavTalk.processInputStream() )
+ break;
+ Looper.loop();
+ }
+ if (DEBUG) Log.d(TAG, "UAVTalk stream disconnected");
+ }
+
+ };
+
+ public UAVObjectManager getObjMngr() { return objMngr; };
+ public UAVTalk getUavTalk() { return uavTalk; };
+ public Telemetry getTelemetry() { return tel; };
+ public TelemetryMonitor getTelemetryMonitor() { return mon; };
+
+}
diff --git a/androidgcs/src/org/openpilot/androidgcs/ObjectBrowser.java b/androidgcs/src/org/openpilot/androidgcs/ObjectBrowser.java
index a8c58d58d..6bb217930 100644
--- a/androidgcs/src/org/openpilot/androidgcs/ObjectBrowser.java
+++ b/androidgcs/src/org/openpilot/androidgcs/ObjectBrowser.java
@@ -69,26 +69,12 @@ public class ObjectBrowser extends Activity {
Log.d(TAG, "Launching Object Browser");
- connected = false;
+ Log.d(TAG, "Start OP Telemetry Service");
+ startService( new Intent( this, OPTelemetryService.class ) );
objMngr = new UAVObjectManager();
UAVObjectsInitialize.register(objMngr);
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if (mBluetoothAdapter == null) {
- // Device does not support Bluetooth
- Log.e(TAG, "Device does not support Bluetooth");
- return;
- }
-
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- } else {
- queryDevices();
- }
-
-
UAVObject obj = objMngr.getObject("SystemStats");
if(obj != null)
obj.addUpdatedObserver(new Observer() {
@@ -116,84 +102,7 @@ public class ObjectBrowser extends Activity {
public void update(Observable observable, Object data) {
uavobjHandler.post(updateText);
}
- });
-
-
+ });
}
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK) {
- //Log.d(TAG, "Bluetooth started succesfully");
- queryDevices();
- }
- if(requestCode == REQUEST_ENABLE_BT && resultCode != RESULT_OK) {
- //Log.d(TAG, "Bluetooth could not be started");
- }
-
- }
-
- public void queryDevices() {
- Log.d(TAG, "Searching for devices");
- Set pairedDevices = mBluetoothAdapter.getBondedDevices();
- // If there are paired devices
- if (pairedDevices.size() > 0) {
- // Loop through paired devices
- for (BluetoothDevice device : pairedDevices) {
- // Add the name and address to an array adapter to show in a ListView
- //mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
- Log.d(TAG, "Paired device: " + device.getName());
- if(device.getName().compareTo(DEVICE_NAME) == 0) {
- openTelmetryBluetooth(device);
- openTelmetryBluetooth(device);
- }
- }
- }
-
- }
-
- private void openTelmetryBluetooth(BluetoothDevice device) {
- Log.d(TAG, "Opening conncetion to " + device.getName());
- socket = null;
- connected = false;
- try {
- socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) {
- Log.e(TAG,"Unable to create Rfcomm socket");
- //e.printStackTrace();
- }
-
- mBluetoothAdapter.cancelDiscovery();
-
- try {
- socket.connect();
- }
- catch (IOException e) {
- Log.e(TAG,"Unable to connect to requested device", e);
- try {
- socket.close();
- } catch (IOException e2) {
- //Log.e(TAG, "unable to close() socket during connection failure", e2);
- }
- return;
- }
-
- connected = true;
-
- try {
- uavTalk = new UAVTalk(socket.getInputStream(), socket.getOutputStream(), objMngr);
- } catch (IOException e) {
- Log.e(TAG,"Error starting UAVTalk");
- // TODO Auto-generated catch block
- //e.printStackTrace();
- return;
- }
-
- Thread inputStream = uavTalk.getInputProcessThread();
- inputStream.start();
-
- Telemetry tel = new Telemetry(uavTalk, objMngr);
- TelemetryMonitor mon = new TelemetryMonitor(objMngr,tel);
-
- }
}