altosdroid: Explicitly disconnect BT on termination or 'disconnect'
authorKeith Packard <keithp@keithp.com>
Tue, 17 Feb 2015 05:19:09 +0000 (21:19 -0800)
committerKeith Packard <keithp@keithp.com>
Tue, 17 Feb 2015 05:19:09 +0000 (21:19 -0800)
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 <keithp@keithp.com>
altosdroid/res/menu/option_menu.xml
altosdroid/res/values/strings.xml
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java

index 3bd5a54e2bb4b6d6dd0abcb81f5fb39d94e7954e..f005e88198a653deb54bb97189640181bc24b0d1 100644 (file)
@@ -17,6 +17,9 @@
     <item android:id="@+id/connect_scan"
           android:icon="@android:drawable/ic_menu_search"
           android:title="@string/connect_device" />
+    <item android:id="@+id/disconnect"
+         android:icon="@android:drawable/ic_notification_clear_all"
+         android:title="@string/disconnect_device" />
     <item android:id="@+id/quit"
           android:icon="@android:drawable/ic_menu_close_clear_cancel"
           android:title="@string/quit" />
index 0cc9934937ad07634cbf97a75c19b5d51020f75d..8a5b29b4e4447ea5faa84db6357bdea7ca46a21a 100644 (file)
@@ -27,6 +27,7 @@
 
        <!-- Options Menu -->
        <string name="connect_device">Connect a device</string>
+       <string name="disconnect_device">Disconnect device</string>
        <string name="quit">Quit</string>
        <string name="select_freq">Select radio frequency</string>
        <string name="select_rate">Select data rate</string>
index 10e735c8cbaa5f5b42748ffc8025579d03d5f93c..273688d8f553573fc4bc3ef43d200a1bd3294c57 100644 (file)
@@ -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:
index 5e34d610fa394404977d4a188ca9cc3b65d52991..5f1389725e8aa240e23b77d86f63790fd60d966c 100644 (file)
@@ -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 {