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:
commit
7b716bab08
@ -28,10 +28,6 @@ import java.io.IOException;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.openpilot.uavtalk.UAVObjectManager;
|
|
||||||
import org.openpilot.uavtalk.UAVTalk;
|
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
@ -43,11 +39,13 @@ import android.content.SharedPreferences;
|
|||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@TargetApi(10) public class BluetoothUAVTalk {
|
public class BluetoothUAVTalk extends TelemetryTask {
|
||||||
|
|
||||||
private final String TAG = "BluetoothUAVTalk";
|
private final String TAG = "BluetoothUAVTalk";
|
||||||
public static int LOGLEVEL = 2;
|
public static final int LOGLEVEL = 4;
|
||||||
public static boolean WARN = LOGLEVEL > 1;
|
public static final boolean DEBUG = LOGLEVEL > 2;
|
||||||
public static boolean DEBUG = LOGLEVEL > 0;
|
public static final boolean WARN = LOGLEVEL > 1;
|
||||||
|
public static final boolean ERROR = LOGLEVEL > 0;
|
||||||
|
|
||||||
// Temporarily define fixed device name
|
// Temporarily define fixed device name
|
||||||
private String device_name = "RN42-222D";
|
private String device_name = "RN42-222D";
|
||||||
@ -56,30 +54,35 @@ import android.util.Log;
|
|||||||
private BluetoothAdapter mBluetoothAdapter;
|
private BluetoothAdapter mBluetoothAdapter;
|
||||||
private BluetoothSocket socket;
|
private BluetoothSocket socket;
|
||||||
private BluetoothDevice device;
|
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","");
|
device_name = prefs.getString("bluetooth_mac","");
|
||||||
|
|
||||||
if (DEBUG) Log.d(TAG, "Trying to open UAVTalk with " + device_name);
|
if (DEBUG) Log.d(TAG, "Trying to open UAVTalk with " + device_name);
|
||||||
|
|
||||||
connected = false;
|
|
||||||
device = null;
|
device = null;
|
||||||
|
|
||||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (mBluetoothAdapter == null) {
|
if (mBluetoothAdapter == null) {
|
||||||
// Device does not support Bluetooth
|
// Device does not support Bluetooth
|
||||||
Log.e(TAG, "Device does not support Bluetooth");
|
Log.e(TAG, "Device does not support Bluetooth");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mBluetoothAdapter.isEnabled()) {
|
if (!mBluetoothAdapter.isEnabled()) {
|
||||||
// Enable bluetooth if it isn't already
|
// Enable bluetooth if it isn't already
|
||||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
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
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
Log.e(TAG,"Received " + context + intent);
|
Log.e(TAG,"Received " + context + intent);
|
||||||
@ -90,60 +93,60 @@ import android.util.Log;
|
|||||||
} else {
|
} else {
|
||||||
queryDevices();
|
queryDevices();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public boolean connect(UAVObjectManager objMngr) {
|
|
||||||
if( getConnected() )
|
|
||||||
return true;
|
|
||||||
if( !getFoundDevice() )
|
|
||||||
return false;
|
|
||||||
if( !openTelemetryBluetooth(objMngr) )
|
|
||||||
return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getConnected() {
|
@Override
|
||||||
return connected;
|
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() {
|
public boolean getFoundDevice() {
|
||||||
return (device != null);
|
return (device != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public UAVTalk getUavtalk() {
|
|
||||||
return uavTalk;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void queryDevices() {
|
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();
|
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
|
||||||
// If there are paired devices
|
// If there are paired devices
|
||||||
if (pairedDevices.size() > 0) {
|
if (pairedDevices.size() > 0) {
|
||||||
// Loop through paired devices
|
// Loop through paired devices
|
||||||
for (BluetoothDevice device : pairedDevices) {
|
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) {
|
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;
|
this.device = device;
|
||||||
|
|
||||||
|
openTelemetryBluetooth();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attemptedFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean openTelemetryBluetooth(UAVObjectManager objMngr) {
|
private boolean openTelemetryBluetooth() {
|
||||||
Log.d(TAG, "Opening connection to " + device.getName());
|
if (DEBUG) Log.d(TAG, "Opening connection to " + device.getName());
|
||||||
|
|
||||||
socket = null;
|
socket = null;
|
||||||
connected = false;
|
|
||||||
try {
|
try {
|
||||||
socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
|
socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG,"Unable to create Rfcomm socket");
|
if (ERROR) Log.e(TAG,"Unable to create Rfcomm socket");
|
||||||
return false;
|
return false;
|
||||||
//e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mBluetoothAdapter.cancelDiscovery();
|
mBluetoothAdapter.cancelDiscovery();
|
||||||
@ -152,26 +155,40 @@ import android.util.Log;
|
|||||||
socket.connect();
|
socket.connect();
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
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 {
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
} catch (IOException e2) {
|
} 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);
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connected = true;
|
attemptedFailed();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
uavTalk = new UAVTalk(socket.getInputStream(), socket.getOutputStream(), objMngr);
|
inStream = socket.getInputStream();
|
||||||
|
outStream = socket.getOutputStream();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG,"Error starting UAVTalk");
|
try {
|
||||||
// TODO Auto-generated catch block
|
socket.close();
|
||||||
//e.printStackTrace();
|
} catch (IOException e2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
attemptedFailed();
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +121,8 @@ public class OPTelemetryService extends Service {
|
|||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
Toast.makeText(getApplicationContext(), "Attempting BT connection", Toast.LENGTH_SHORT).show();
|
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;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
Toast.makeText(getApplicationContext(), "Attempting TCP connection", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getApplicationContext(), "Attempting TCP connection", Toast.LENGTH_SHORT).show();
|
||||||
@ -226,7 +227,7 @@ public class OPTelemetryService extends Service {
|
|||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
|
|
||||||
if (telemTask != null) {
|
if (telemTask != null) {
|
||||||
Log.d(TAG, "onDestory() shutting down telemetry task");
|
Log.d(TAG, "onDestroy() shutting down telemetry task");
|
||||||
telemTask.disconnect();
|
telemTask.disconnect();
|
||||||
telemTask = null;
|
telemTask = null;
|
||||||
|
|
||||||
|
@ -68,6 +68,9 @@ public abstract class TelemetryTask implements Runnable {
|
|||||||
//! The telemetry monitor which takes care of high level connects / disconnects
|
//! The telemetry monitor which takes care of high level connects / disconnects
|
||||||
private TelemetryMonitor mon;
|
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.
|
//! Flag to indicate a shut down was requested. Derived classes should take care to respect this.
|
||||||
boolean shutdown;
|
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 the input and output stream is closed
|
||||||
|
|
||||||
// TODO: Make sure any threads for input and output are 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.
|
* to read from the input stream.
|
||||||
*/
|
*/
|
||||||
private void startInputProcessing() {
|
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
|
//! Runnable to process input stream
|
||||||
|
Loading…
Reference in New Issue
Block a user