+ 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();
+ }
+ }
+ }
+
+ private void start_altos_bluetooth(DeviceAddress address, boolean pause) {
+ // Get the BLuetoothDevice object
+ BluetoothDevice device = bluetooth_adapter.getRemoteDevice(address.address);
+
+ stop_altos_bluetooth(false);
+ if (pause) {
+ try {
+ Thread.sleep(4000);
+ } catch (InterruptedException e) {
+ }
+ }
+ this.address = address;
+ if (D) Log.d(TAG, String.format("start_altos_bluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress()));
+ altos_bluetooth = new AltosBluetooth(device, handler);
+ telemetry_state.connect = TelemetryState.CONNECT_CONNECTING;
+ telemetry_state.address = address;
+ send_to_clients();
+ }
+
+ private void connected() throws InterruptedException {
+ if (D) Log.d(TAG, "connected top");
+ try {
+ if (altos_bluetooth == null)
+ throw new InterruptedException("no bluetooth");
+ telemetry_state.config = altos_bluetooth.config_data();
+ altos_bluetooth.set_radio_frequency(telemetry_state.frequency);
+ altos_bluetooth.set_telemetry_rate(telemetry_state.telemetry_rate);
+ } catch (TimeoutException e) {
+ // If this timed out, then we really want to retry it, but
+ // probably safer to just retry the connection from scratch.
+ if (D) Log.d(TAG, "connected timeout");
+ if (address != null) {
+ if (D) Log.d(TAG, "connected timeout, retrying");
+ start_altos_bluetooth(address, true);
+ } else {
+ handler.obtainMessage(MSG_CONNECT_FAILED).sendToTarget();
+ stop_altos_bluetooth(true);
+ }
+ return;
+ }
+
+ if (D) Log.d(TAG, "connected bluetooth configured");
+ telemetry_state.connect = TelemetryState.CONNECT_CONNECTED;
+ telemetry_state.address = address;
+
+ telemetry_reader = new TelemetryReader(altos_bluetooth, handler, telemetry_state.state);
+ telemetry_reader.start();
+
+ if (D) Log.d(TAG, "connected TelemetryReader started");
+
+ telemetry_logger = new TelemetryLogger(this, altos_bluetooth);
+
+ if (D) Log.d(TAG, "Notify UI of connection");
+
+ send_to_clients();
+ }
+
+