+ device = null;
+ mConfigData = null;
+ }
+
+ private void startAltosBluetooth() {
+ if (mAltosBluetooth == null) {
+ if (D) Log.d(TAG, String.format("startAltosBluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress()));
+ mAltosBluetooth = new AltosBluetooth(device, mHandler);
+ setState(STATE_CONNECTING);
+ } 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, device), 3000);
+ stopAltosBluetooth();
+ }
+ }
+
+ private synchronized void setState(int s) {
+ if (D) Log.d(TAG, "setState(): " + state + " -> " + s);
+ state = s;
+
+ // This shouldn't be required - mConfigData should be null for any non-connected
+ // state, but to be safe and to reduce message size
+ AltosConfigData acd = (state == STATE_CONNECTED) ? mConfigData : null;
+
+ sendMessageToClients(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, state, -1, acd));
+ }
+
+ private void connected() {
+ try {
+ mConfigData = mAltosBluetooth.config_data();
+ } catch (InterruptedException e) {
+ } 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;
+ }
+
+ setState(STATE_CONNECTED);
+
+ mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler);
+ mTelemetryReader.start();
+
+ mTelemetryLogger = new TelemetryLogger(this, mAltosBluetooth);