altosdroid: Send current state to client on connect
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryService.java
index 1903cc1de2d8c2084cebfebda7cdabad9a3590ac..7cd233ab646528b5805b0875084409300f4fada6 100644 (file)
@@ -81,6 +81,11 @@ public class TelemetryService extends Service {
                        switch (msg.what) {
                        case MSG_REGISTER_CLIENT:
                                s.mClients.add(msg.replyTo);
+                               try {
+                                       msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, s.state, -1));
+                               } catch (RemoteException e) {
+                                       s.mClients.remove(msg.replyTo);
+                               }
                                if (D) Log.d(TAG, "Client bound to service");
                                break;
                        case MSG_UNREGISTER_CLIENT:
@@ -102,9 +107,21 @@ public class TelemetryService extends Service {
                }
        }
 
+       private void sendMessageToClients(Message m) {
+               for (int i=mClients.size()-1; i>=0; i--) {
+                       try {
+                               mClients.get(i).send(m);
+                       } catch (RemoteException e) {
+                               mClients.remove(i);
+                       }
+               }
+       }
+
        private void stopAltosBluetooth() {
+               if (D) Log.i(TAG, "Stopping BT");
                setState(STATE_READY);
                if (mAltosBluetooth != null) {
+                       if (D) Log.i(TAG, "Closing AltosBluetooth");
                        mAltosBluetooth.close();
                        mAltosBluetooth = null;
                }
@@ -112,6 +129,7 @@ public class TelemetryService extends Service {
        }
 
        private void startAltosBluetooth(BluetoothDevice d) {
+                       if (D) Log.i(TAG, "Connecting to " + d.getName());
                        mAltosBluetooth = new AltosBluetooth(d, mHandler);
                        setState(STATE_CONNECTING);
        }