- device = null;
- telem.clear();
- }
-
- private void startAltosBluetooth() {
- if (mAltosBluetooth == null) {
- if (D) Log.i(TAG, "Connecting to " + device.getName());
- mAltosBluetooth = new AltosBluetooth(device, mHandler);
- setState(STATE_CONNECTING);
- } else {
- stopAltosBluetooth();
- mHandler.sendMessageDelayed(Message.obtain(null, MSG_CONNECT, device), 1000);
+ }
+
+ 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 stop_altos_bluetooth(boolean notify) {
+ if (D) Log.d(TAG, "stop_altos_bluetooth(): begin");
+ telemetry_state.connect = TelemetryState.CONNECT_DISCONNECTED;
+ telemetry_state.address = null;
+
+ if (altos_bluetooth != null)
+ altos_bluetooth.closing();
+
+ if (telemetry_reader != null) {
+ if (D) Log.d(TAG, "stop_altos_bluetooth(): stopping TelemetryReader");
+ telemetry_reader.interrupt();
+ try {
+ telemetry_reader.join();
+ } catch (InterruptedException e) {
+ }
+ telemetry_reader = null;
+ }
+ if (telemetry_logger != null) {
+ if (D) Log.d(TAG, "stop_altos_bluetooth(): stopping TelemetryLogger");
+ telemetry_logger.stop();
+ telemetry_logger = null;
+ }
+ if (altos_bluetooth != null) {
+ if (D) Log.d(TAG, "stop_altos_bluetooth(): stopping AltosBluetooth");
+ altos_bluetooth.close();
+ altos_bluetooth = null;
+ }
+ telemetry_state.config = null;
+ if (notify) {
+ if (D) Log.d(TAG, "stop_altos_bluetooth(): send message to clients");
+ send_to_clients();
+ if (clients.isEmpty()) {
+ if (D) Log.d(TAG, "stop_altos_bluetooth(): no clients, terminating");
+ stopSelf();
+ }