From 877609a60a9f2c61c1efad8285b2a3c22f59be28 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 16 Feb 2015 21:19:09 -0800 Subject: [PATCH] altosdroid: Explicitly disconnect BT on termination or 'disconnect' This adds an explicit message to the telemetry service telling it when to stop trying to talk to the bluetooth device. Until this message is received, the service will reconnect to the specified BT device. That message is sent when you 'quit' the application, or when you 'disconnect'. Signed-off-by: Keith Packard --- altosdroid/res/menu/option_menu.xml | 3 +++ altosdroid/res/values/strings.xml | 1 + .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 14 +++++++++++++- .../altusmetrum/AltosDroid/TelemetryService.java | 10 ++++++++-- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/altosdroid/res/menu/option_menu.xml b/altosdroid/res/menu/option_menu.xml index 3bd5a54e..f005e881 100644 --- a/altosdroid/res/menu/option_menu.xml +++ b/altosdroid/res/menu/option_menu.xml @@ -17,6 +17,9 @@ + diff --git a/altosdroid/res/values/strings.xml b/altosdroid/res/values/strings.xml index 0cc99349..8a5b29b4 100644 --- a/altosdroid/res/values/strings.xml +++ b/altosdroid/res/values/strings.xml @@ -27,6 +27,7 @@ Connect a device + Disconnect device Quit Select radio frequency Select data rate diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 10e735c8..273688d8 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -553,6 +553,13 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { } } + private void disconnectDevice() { + try { + mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, null)); + } catch (RemoteException e) { + } + } + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); @@ -610,9 +617,14 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { serverIntent = new Intent(this, DeviceListActivity.class); startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE); return true; + case R.id.disconnect: + /* Disconnect the bluetooth device + */ + disconnectDevice(); + return true; case R.id.quit: Log.d(TAG, "R.id.quit"); - stopService(new Intent(AltosDroid.this, TelemetryService.class)); + disconnectDevice(); finish(); return true; case R.id.select_freq: diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 5e34d610..5f138972 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -63,6 +63,7 @@ public class TelemetryService extends Service implements LocationListener { static final int MSG_SETFREQUENCY = 8; static final int MSG_CRC_ERROR = 9; static final int MSG_SETBAUD = 10; + static final int MSG_DISCONNECT = 11; // Unique Identification Number for the Notification. // We use it on Notification start, and to cancel it. @@ -110,6 +111,11 @@ public class TelemetryService extends Service implements LocationListener { AltosDroidPreferences.set_active_device(address); s.start_altos_bluetooth(address); break; + case MSG_DISCONNECT: + if (D) Log.d(TAG, "Disconnect command received"); + s.address = null; + s.stop_altos_bluetooth(true); + break; case MSG_SETFREQUENCY: if (D) Log.d(TAG, "MSG_SETFREQUENCY"); s.telemetry_state.frequency = (Double) msg.obj; @@ -144,7 +150,7 @@ public class TelemetryService extends Service implements LocationListener { } break; case MSG_CONNECT_FAILED: - if (s.address != null && !s.clients.isEmpty()) { + if (s.address != null) { if (D) Log.d(TAG, "Connection failed... retrying"); s.start_altos_bluetooth(s.address); } else { @@ -153,7 +159,7 @@ public class TelemetryService extends Service implements LocationListener { break; case MSG_DISCONNECTED: Log.d(TAG, "MSG_DISCONNECTED"); - if (s.address != null && !s.clients.isEmpty()) { + if (s.address != null) { if (D) Log.d(TAG, "Connection lost... retrying"); s.start_altos_bluetooth(s.address); } else { -- 2.30.2