+ private BluetoothDevice device = null;
+ private AltosBluetooth mAltosBluetooth = null;
+ private AltosConfigData mConfigData = null;
+ private TelemetryReader mTelemetryReader = null;
+ private TelemetryLogger mTelemetryLogger = null;
+
+ // internally track state of bluetooth connection
+ private int state = STATE_NONE;
+
+ // Last data seen; send to UI when it starts
+
+ private AltosState last_state;
+ private Location last_location;
+ private int last_crc_errors;
+
+ // Handler of incoming messages from clients.
+ static class IncomingHandler extends Handler {
+ private final WeakReference<TelemetryService> service;
+ IncomingHandler(TelemetryService s) { service = new WeakReference<TelemetryService>(s); }
+
+ @Override
+ public void handleMessage(Message msg) {
+ TelemetryService s = service.get();
+ switch (msg.what) {
+ case MSG_REGISTER_CLIENT:
+ s.mClients.add(msg.replyTo);
+ try {
+ // Now we try to send the freshly connected UI any relavant information about what
+ // we're talking to - Basically state and Config Data.
+ msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, s.state, -1, s.mConfigData));
+ // We also send any recent telemetry or location data that's cached
+ if (s.last_state != null) msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_TELEMETRY, s.last_state ));
+ if (s.last_location != null) msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_LOCATION , s.last_location ));
+ if (s.last_crc_errors != 0 ) msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_CRC_ERROR, s.last_crc_errors));
+ } catch (RemoteException e) {
+ s.mClients.remove(msg.replyTo);
+ }
+ if (D) Log.d(TAG, "Client bound to service");
+ break;
+ case MSG_UNREGISTER_CLIENT:
+ s.mClients.remove(msg.replyTo);
+ if (D) Log.d(TAG, "Client unbound from service");
+ break;
+ case MSG_CONNECT:
+ if (D) Log.d(TAG, "Connect command received");
+ s.device = (BluetoothDevice) msg.obj;
+ s.startAltosBluetooth();
+ break;
+ case MSG_CONNECTED:
+ if (D) Log.d(TAG, "Connected to device");
+ s.connected();
+ break;
+ case MSG_CONNECT_FAILED:
+ if (D) Log.d(TAG, "Connection failed... retrying");
+ s.startAltosBluetooth();
+ break;
+ case MSG_DISCONNECTED:
+ // Only do the following if we haven't been shutdown elsewhere..
+ if (s.device != null) {
+ if (D) Log.d(TAG, "Disconnected from " + s.device.getName());
+ s.stopAltosBluetooth();
+ }
+ break;
+ case MSG_TELEMETRY:
+ // forward telemetry messages
+ s.last_state = (AltosState) msg.obj;
+ s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_TELEMETRY, msg.obj));
+ break;
+ case MSG_CRC_ERROR:
+ // forward crc error messages
+ s.last_crc_errors = (Integer) msg.obj;
+ s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_CRC_ERROR, msg.obj));
+ break;
+ case MSG_SETFREQUENCY:
+ if (s.state == STATE_CONNECTED) {
+ try {
+ s.mAltosBluetooth.set_radio_frequency((Double) msg.obj);
+ } catch (InterruptedException e) {
+ } catch (TimeoutException e) {
+ }
+ }
+ break;
+ default:
+ super.handleMessage(msg);
+ }
+ }
+ }
+
+ private void sendMessageToClients(Message m) {
+ for (int i=mClients.size()-1; i>=0; i--) {
+ try {
+ mClients.get(i).send(m);
+ } catch (RemoteException e) {
+ mClients.remove(i);
+ }
+ }
+ }
+
+ private void stopAltosBluetooth() {
+ if (D) Log.d(TAG, "stopAltosBluetooth(): begin");
+ setState(STATE_READY);
+ if (mTelemetryReader != null) {
+ if (D) Log.d(TAG, "stopAltosBluetooth(): stopping TelemetryReader");
+ mTelemetryReader.interrupt();
+ try {
+ mTelemetryReader.join();
+ } catch (InterruptedException e) {
+ }
+ mTelemetryReader = null;
+ }
+ if (mTelemetryLogger != null) {
+ if (D) Log.d(TAG, "stopAltosBluetooth(): stopping TelemetryLogger");
+ mTelemetryLogger.stop();
+ mTelemetryLogger = null;
+ }
+ if (mAltosBluetooth != null) {
+ if (D) Log.d(TAG, "stopAltosBluetooth(): stopping AltosBluetooth");
+ mAltosBluetooth.close();
+ mAltosBluetooth = null;
+ }
+ device = null;
+ mConfigData = null;
+ }
+
+ private void startAltosBluetooth() {
+ if (device == null) {
+ return;
+ }
+ 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 {
+ if (mAltosBluetooth == null)
+ throw new InterruptedException("no bluetooth");
+ 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);
+ }
+
+
+ private void onTimerTick() {
+ if (D) Log.d(TAG, "Timer wakeup");
+ try {
+ if (mClients.size() <= 0 && state != STATE_CONNECTED) {
+ stopSelf();
+ }
+ } catch (Throwable t) {
+ Log.e(TAG, "Timer failed: ", t);
+ }
+ }