- private void stopAltosBluetooth() {
- if (D) Log.d(TAG, "stopAltosBluetooth(): begin");
- setState(STATE_READY);
- if (mTelemetryReader != null) {
- if (D) Log.d(TAG, "stopAltosBluetooth(): stopping TelemetryReader");
- mTelemetryReader.interrupt();
+ /* A client has disconnected, clean up
+ */
+ private void remove_client(Messenger client) {
+ clients.remove(client);
+ if (D) Log.d(TAG, "Client unbound from service");
+
+ /* When the list of clients is empty, stop the service if
+ * we have no current telemetry source
+ */
+
+ if (clients.isEmpty() && telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED) {
+ if (!D) Log.d(TAG, "No clients, no connection. Stopping\n");
+ stopSelf();
+ }
+ }
+
+ private void send_to_client(Messenger client, Message m) {
+ try {
+ if (D) Log.d(TAG, String.format("Send message to client %s", client.toString()));
+ client.send(m);
+ } catch (RemoteException e) {
+ if (D) Log.e(TAG, String.format("Client %s disappeared", client.toString()));
+ remove_client(client);
+ }
+ }
+
+ private void send_to_clients() {
+ Message m = message();
+ if (D) Log.d(TAG, String.format("Send message to %d clients", clients.size()));
+ for (Messenger client : clients)
+ send_to_client(client, m);
+ }
+
+ private void disconnect(boolean notify) {
+ if (D) Log.d(TAG, "disconnect(): begin");
+
+ telemetry_state.connect = TelemetryState.CONNECT_DISCONNECTED;
+ telemetry_state.address = null;
+
+ if (altos_link != null)
+ altos_link.closing();
+
+ if (telemetry_reader != null) {
+ if (D) Log.d(TAG, "disconnect(): stopping TelemetryReader");
+ telemetry_reader.interrupt();