1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-30 08:24:11 +01:00

Merge branch 'android' into revo-mini

This commit is contained in:
James Cotton 2012-08-28 11:42:55 -05:00
commit 7b716bab08
3 changed files with 79 additions and 49 deletions

View File

@ -28,10 +28,6 @@ import java.io.IOException;
import java.util.Set;
import java.util.UUID;
import org.openpilot.uavtalk.UAVObjectManager;
import org.openpilot.uavtalk.UAVTalk;
import android.annotation.TargetApi;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
@ -43,11 +39,13 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
@TargetApi(10) public class BluetoothUAVTalk {
public class BluetoothUAVTalk extends TelemetryTask {
private final String TAG = "BluetoothUAVTalk";
public static int LOGLEVEL = 2;
public static boolean WARN = LOGLEVEL > 1;
public static boolean DEBUG = LOGLEVEL > 0;
public static final int LOGLEVEL = 4;
public static final boolean DEBUG = LOGLEVEL > 2;
public static final boolean WARN = LOGLEVEL > 1;
public static final boolean ERROR = LOGLEVEL > 0;
// Temporarily define fixed device name
private String device_name = "RN42-222D";
@ -56,30 +54,35 @@ import android.util.Log;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothSocket socket;
private BluetoothDevice device;
private UAVTalk uavTalk;
private boolean connected;
public BluetoothUAVTalk(Context caller) {
public BluetoothUAVTalk(OPTelemetryService caller) {
super(caller);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(caller);
@Override
boolean attemptConnection() {
if( getConnected() )
return true;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(telemService);
device_name = prefs.getString("bluetooth_mac","");
if (DEBUG) Log.d(TAG, "Trying to open UAVTalk with " + device_name);
connected = false;
device = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Log.e(TAG, "Device does not support Bluetooth");
return;
return false;
}
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() {
telemService.sendOrderedBroadcast(enableBtIntent, "android.permission.BLUETOOTH_ADMIN", new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG,"Received " + context + intent);
@ -90,60 +93,60 @@ import android.util.Log;
} 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;
@Override
public void disconnect() {
super.disconnect();
if(socket != null) {
try {
socket.close();
} catch (IOException e) {
if (ERROR) Log.e(TAG, "Unable to close BT socket");
}
socket = null;
}
}
public boolean getFoundDevice() {
return (device != null);
}
public UAVTalk getUavtalk() {
return uavTalk;
}
private void queryDevices() {
Log.d(TAG, "Searching for devices");
if (DEBUG) Log.d(TAG, "Searching for devices matching the selected preference");
Set<BluetoothDevice> 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.getAddress() + " compared to " + device_name);
if(device.getAddress().compareTo(device_name) == 0) {
Log.d(TAG, "Found device: " + device.getName());
if (DEBUG) Log.d(TAG, "Found selected device: " + device.getName());
this.device = device;
openTelemetryBluetooth();
return;
}
}
}
attemptedFailed();
}
private boolean openTelemetryBluetooth(UAVObjectManager objMngr) {
Log.d(TAG, "Opening connection to " + device.getName());
private boolean openTelemetryBluetooth() {
if (DEBUG) Log.d(TAG, "Opening connection to " + device.getName());
socket = null;
connected = false;
try {
socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG,"Unable to create Rfcomm socket");
if (ERROR) Log.e(TAG,"Unable to create Rfcomm socket");
return false;
//e.printStackTrace();
}
mBluetoothAdapter.cancelDiscovery();
@ -152,26 +155,40 @@ import android.util.Log;
socket.connect();
}
catch (IOException e) {
Log.e(TAG,"Unable to connect to requested device", e);
if (ERROR) 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);
if (ERROR) Log.e(TAG, "unable to close() socket during connection failure", e2);
}
attemptedFailed();
return false;
}
connected = true;
try {
uavTalk = new UAVTalk(socket.getInputStream(), socket.getOutputStream(), objMngr);
inStream = socket.getInputStream();
outStream = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG,"Error starting UAVTalk");
// TODO Auto-generated catch block
//e.printStackTrace();
try {
socket.close();
} catch (IOException e2) {
}
attemptedFailed();
return false;
}
telemService.toastMessage("Bluetooth device connected");
// Post message to call attempt succeeded on the parent class
handler.post(new Runnable() {
@Override
public void run() {
BluetoothUAVTalk.this.attemptSucceeded();
}
});
return true;
}

View File

@ -121,7 +121,8 @@ public class OPTelemetryService extends Service {
break;
case 2:
Toast.makeText(getApplicationContext(), "Attempting BT connection", Toast.LENGTH_SHORT).show();
//activeTelem = new BTTelemetryThread();
telemTask = new BluetoothUAVTalk(this);
activeTelem = new Thread(telemTask, "Bluetooth telemetry thread");
break;
case 3:
Toast.makeText(getApplicationContext(), "Attempting TCP connection", Toast.LENGTH_SHORT).show();
@ -226,7 +227,7 @@ public class OPTelemetryService extends Service {
public void onDestroy() {
if (telemTask != null) {
Log.d(TAG, "onDestory() shutting down telemetry task");
Log.d(TAG, "onDestroy() shutting down telemetry task");
telemTask.disconnect();
telemTask = null;

View File

@ -68,6 +68,9 @@ public abstract class TelemetryTask implements Runnable {
//! The telemetry monitor which takes care of high level connects / disconnects
private TelemetryMonitor mon;
//! Thread to process the input stream
Thread inputProcessThread;
//! Flag to indicate a shut down was requested. Derived classes should take care to respect this.
boolean shutdown;
@ -146,6 +149,14 @@ public abstract class TelemetryTask implements Runnable {
}
});
if (inputProcessThread != null) {
inputProcessThread.interrupt();
try {
inputProcessThread.join();
} catch (InterruptedException e) {
}
}
// TODO: Make sure the input and output stream is closed
// TODO: Make sure any threads for input and output are closed
@ -157,7 +168,8 @@ public abstract class TelemetryTask implements Runnable {
* to read from the input stream.
*/
private void startInputProcessing() {
new Thread(new processUavTalk(), "Process UAV talk").start();
inputProcessThread = new Thread(new processUavTalk(), "Process UAV talk");
inputProcessThread.start();
}
//! Runnable to process input stream