+ telemetry_state.config = null;
+ if (D) Log.d(TAG, "stopAltosBluetooth(): send message to clients");
+ sendMessageToClients();
+ }
+
+ private void startAltosBluetooth(String address) {
+ // Get the BLuetoothDevice object
+ BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
+
+ this.address = address;
+ if (mAltosBluetooth == null) {
+ if (D) Log.d(TAG, String.format("startAltosBluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress()));
+ mAltosBluetooth = new AltosBluetooth(device, mHandler);
+ telemetry_state.connect = TelemetryState.CONNECT_CONNECTING;
+ sendMessageToClients();
+ } else {
+ // This is a bit of a hack - if it appears we're still connected, we treat this as a restart.
+ // So, to give a suitable delay to teardown/bringup, we just schedule a resend of a message
+ // to ourselves in a few seconds time that will ultimately call this method again.
+ // ... then we tear down the existing connection.
+ // We do it this way around so that we don't lose a reference to the device when this method
+ // is called on reception of MSG_CONNECT_FAILED in the handler above.
+ mHandler.sendMessageDelayed(Message.obtain(null, MSG_CONNECT, address), 3000);
+ stopAltosBluetooth();
+ }
+ }
+
+ private void connected() throws InterruptedException {
+ if (D) Log.d(TAG, "connected top");
+ try {
+ if (mAltosBluetooth == null)
+ throw new InterruptedException("no bluetooth");
+ telemetry_state.config = mAltosBluetooth.config_data();
+ mAltosBluetooth.set_radio_frequency(telemetry_state.frequency);
+ mAltosBluetooth.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.
+ mHandler.obtainMessage(MSG_CONNECT_FAILED).sendToTarget();
+ return;
+ }
+
+ if (D) Log.d(TAG, "connected bluetooth configured");
+ telemetry_state.connect = TelemetryState.CONNECT_CONNECTED;
+
+ mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler, telemetry_state.state);
+ mTelemetryReader.start();
+
+ if (D) Log.d(TAG, "connected TelemetryReader started");
+
+ mTelemetryLogger = new TelemetryLogger(this, mAltosBluetooth);
+
+ if (D) Log.d(TAG, "Notify UI of connection");
+
+ sendMessageToClients();