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

AndroidGCS Bluetooth: Make bluetooth use the new connection system.

This commit is contained in:
James Cotton 2012-08-28 11:09:50 -05:00
parent caff64ed7e
commit 6cd9a9b0af
2 changed files with 65 additions and 47 deletions

View File

@ -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;
} }

View File

@ -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();