+ DeviceAddress address = (DeviceAddress) msg.obj;
+ AltosDroidPreferences.set_active_device(address);
+ s.start_altos_bluetooth(address, false);
+ 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;
+ if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
+ try {
+ s.altos_bluetooth.set_radio_frequency(s.telemetry_state.frequency);
+ s.altos_bluetooth.save_frequency();
+ } catch (InterruptedException e) {
+ } catch (TimeoutException e) {
+ }
+ }
+ s.send_to_clients();
+ break;
+ case MSG_SETBAUD:
+ if (D) Log.d(TAG, "MSG_SETBAUD");
+ s.telemetry_state.telemetry_rate = (Integer) msg.obj;
+ if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
+ s.altos_bluetooth.set_telemetry_rate(s.telemetry_state.telemetry_rate);
+ s.altos_bluetooth.save_telemetry_rate();
+ }
+ s.send_to_clients();
+ break;
+
+ /*
+ *Messages from AltosBluetooth
+ */
+ case MSG_CONNECTED:
+ Log.d(TAG, "MSG_CONNECTED");
+ bt = (AltosBluetooth) msg.obj;
+
+ if (bt != s.altos_bluetooth) {
+ if (D) Log.d(TAG, "Stale message");
+ break;
+ }
+ if (D) Log.d(TAG, "Connected to device");
+ try {
+ s.connected();
+ } catch (InterruptedException ie) {
+ }
+ break;
+ case MSG_CONNECT_FAILED:
+ Log.d(TAG, "MSG_CONNECT_FAILED");
+ bt = (AltosBluetooth) msg.obj;
+
+ if (bt != s.altos_bluetooth) {
+ if (D) Log.d(TAG, "Stale message");
+ break;
+ }
+ if (s.address != null) {
+ if (D) Log.d(TAG, "Connection failed... retrying");
+ s.start_altos_bluetooth(s.address, true);
+ } else {
+ s.stop_altos_bluetooth(true);
+ }
+ break;
+ case MSG_DISCONNECTED:
+ Log.d(TAG, "MSG_DISCONNECTED");
+ bt = (AltosBluetooth) msg.obj;
+
+ if (bt != s.altos_bluetooth) {
+ if (D) Log.d(TAG, "Stale message");
+ break;
+ }
+ if (s.address != null) {
+ if (D) Log.d(TAG, "Connection lost... retrying");
+ s.start_altos_bluetooth(s.address, true);
+ } else {
+ s.stop_altos_bluetooth(true);
+ }
+ break;
+
+ /*
+ * Messages from TelemetryReader
+ */
+ case MSG_TELEMETRY:
+ s.telemetry_state.state = (AltosState) msg.obj;
+ if (s.telemetry_state.state != null) {
+ if (D) Log.d(TAG, "Save state");
+ AltosPreferences.set_state(0, s.telemetry_state.state, null);
+ }
+ if (D) Log.d(TAG, "MSG_TELEMETRY");
+ s.send_to_clients();
+ break;
+ case MSG_CRC_ERROR:
+ // forward crc error messages
+ s.telemetry_state.crc_errors = (Integer) msg.obj;
+ if (D) Log.d(TAG, "MSG_CRC_ERROR");
+ s.send_to_clients();