altosdroid: Run even without Bluetooth
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryService.java
index 80694ea74357385264f19c5f5cd0968a25d69b8c..926109e2b27642d0ec538daa0113354d350a49f4 100644 (file)
@@ -38,15 +38,11 @@ import android.os.Messenger;
 import android.os.RemoteException;
 import android.os.Looper;
 import android.widget.Toast;
-import android.location.Location;
-import android.location.LocationManager;
-import android.location.LocationListener;
 import android.location.Criteria;
 
-import org.altusmetrum.altoslib_7.*;
+import org.altusmetrum.altoslib_10.*;
 
-
-public class TelemetryService extends Service implements LocationListener {
+public class TelemetryService extends Service {
 
        static final int MSG_REGISTER_CLIENT   = 1;
        static final int MSG_UNREGISTER_CLIENT = 2;
@@ -61,6 +57,7 @@ public class TelemetryService extends Service implements LocationListener {
        static final int MSG_SETBAUD           = 11;
        static final int MSG_DISCONNECT        = 12;
        static final int MSG_DELETE_SERIAL     = 13;
+       static final int MSG_BLUETOOTH_ENABLED = 14;
 
        // Unique Identification Number for the Notification.
        // We use it on Notification start, and to cancel it.
@@ -90,6 +87,8 @@ public class TelemetryService extends Service implements LocationListener {
 
                @Override
                public void handleMessage(Message msg) {
+                       DeviceAddress address;
+
                        TelemetryService s = service.get();
                        AltosDroidLink bt = null;
                        if (s == null)
@@ -105,7 +104,7 @@ public class TelemetryService extends Service implements LocationListener {
                                break;
                        case MSG_CONNECT:
                                AltosDebug.debug("Connect command received");
-                               DeviceAddress address = (DeviceAddress) msg.obj;
+                               address = (DeviceAddress) msg.obj;
                                AltosDroidPreferences.set_active_device(address);
                                s.start_altos_bluetooth(address, false);
                                break;
@@ -200,15 +199,19 @@ public class TelemetryService extends Service implements LocationListener {
                                 * Messages from TelemetryReader
                                 */
                        case MSG_TELEMETRY:
-                               AltosDebug.debug("MSG_TELEMETRY");
                                s.telemetry((AltosTelemetry) msg.obj);
                                break;
                        case MSG_CRC_ERROR:
                                // forward crc error messages
                                s.telemetry_state.crc_errors = (Integer) msg.obj;
-                               AltosDebug.debug("MSG_CRC_ERROR");
                                s.send_to_clients();
                                break;
+                       case MSG_BLUETOOTH_ENABLED:
+                               AltosDebug.debug("TelemetryService notes that BT is now enabled");
+                               address = AltosDroidPreferences.active_device();
+                               if (address != null && !address.address.startsWith("USB"))
+                                       s.start_altos_bluetooth(address, false);
+                               break;
                        default:
                                super.handleMessage(msg);
                        }
@@ -227,7 +230,6 @@ public class TelemetryService extends Service implements LocationListener {
                telem.update_state(state);
                telemetry_state.states.put(telem.serial, state);
                if (state != null) {
-                       AltosDebug.debug("Save state %d", telem.serial);
                        AltosPreferences.set_state(telem.serial, state, null);
                }
                send_to_clients();
@@ -252,7 +254,7 @@ public class TelemetryService extends Service implements LocationListener {
 
                /* On connect, send the current state to the new client
                 */
-               send_to_client(client, message());
+               send_to_client(client);
 
                /* If we've got an address from a previous session, then
                 * go ahead and try to reconnect to the device
@@ -279,9 +281,9 @@ public class TelemetryService extends Service implements LocationListener {
                 }
        }
 
-       private void send_to_client(Messenger client, Message m) {
+       private void send_to_client(Messenger client) {
+               Message m = message();
                try {
-                       AltosDebug.debug("Send message to client %s", client.toString());
                        client.send(m);
                } catch (RemoteException e) {
                        AltosDebug.error("Client %s disappeared", client.toString());
@@ -290,10 +292,8 @@ public class TelemetryService extends Service implements LocationListener {
        }
 
        private void send_to_clients() {
-               Message m = message();
-               AltosDebug.debug("Send message to %d clients", clients.size());
                for (Messenger client : clients)
-                       send_to_client(client, m);
+                       send_to_client(client);
        }
 
        private void disconnect(boolean notify) {
@@ -305,6 +305,8 @@ public class TelemetryService extends Service implements LocationListener {
                if (altos_link != null)
                        altos_link.closing();
 
+               stop_receiver_voltage_timer();
+
                if (telemetry_reader != null) {
                        AltosDebug.debug("disconnect(): stopping TelemetryReader");
                        telemetry_reader.interrupt();
@@ -355,10 +357,14 @@ public class TelemetryService extends Service implements LocationListener {
        }
 
        private void start_altos_bluetooth(DeviceAddress address, boolean pause) {
-               // Get the BLuetoothDevice object
-               BluetoothDevice device = bluetooth_adapter.getRemoteDevice(address.address);
+               if (bluetooth_adapter == null || !bluetooth_adapter.isEnabled())
+                       return;
 
                disconnect(false);
+
+               // Get the BluetoothDevice object
+               BluetoothDevice device = bluetooth_adapter.getRemoteDevice(address.address);
+
                this.address = address;
                AltosDebug.debug("start_altos_bluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress());
                altos_link = new AltosBluetooth(device, handler, pause);
@@ -367,6 +373,34 @@ public class TelemetryService extends Service implements LocationListener {
                send_to_clients();
        }
 
+       // Timer for receiver battery voltage monitoring
+       Timer receiver_voltage_timer;
+
+       private void update_receiver_voltage() {
+               if (altos_link != null) {
+                       try {
+                               double  voltage = altos_link.monitor_battery();
+                               telemetry_state.receiver_battery = voltage;
+                       } catch (InterruptedException ie) {
+                       }
+               }
+       }
+
+       private void stop_receiver_voltage_timer() {
+               if (receiver_voltage_timer != null) {
+                       receiver_voltage_timer.cancel();
+                       receiver_voltage_timer.purge();
+                       receiver_voltage_timer = null;
+               }
+       }
+
+       private void start_receiver_voltage_timer() {
+               if (receiver_voltage_timer == null && altos_link.has_monitor_battery()) {
+                       receiver_voltage_timer = new Timer();
+                       receiver_voltage_timer.scheduleAtFixedRate(new TimerTask() { public void run() {update_receiver_voltage();}}, 1000L, 10000L);
+               }
+       }
+
        private void connected() throws InterruptedException {
                AltosDebug.debug("connected top");
                AltosDebug.check_ui("connected\n");
@@ -401,6 +435,8 @@ public class TelemetryService extends Service implements LocationListener {
 
                telemetry_logger = new TelemetryLogger(this, altos_link);
 
+               start_receiver_voltage_timer();
+
                AltosDebug.debug("Notify UI of connection");
 
                send_to_clients();
@@ -409,17 +445,15 @@ public class TelemetryService extends Service implements LocationListener {
 
        @Override
        public void onCreate() {
-               // Get local Bluetooth adapter
-               bluetooth_adapter = BluetoothAdapter.getDefaultAdapter();
 
-               // If the adapter is null, then Bluetooth is not supported
-               if (bluetooth_adapter == null) {
-                       Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
-               }
+               AltosDebug.init(this);
 
                // Initialise preferences
                AltosDroidPreferences.init(this);
 
+               // Get local Bluetooth adapter
+               bluetooth_adapter = BluetoothAdapter.getDefaultAdapter();
+
                telemetry_state = new TelemetryState();
 
                // Create a reference to the NotificationManager so that we can update our notifcation text later
@@ -455,11 +489,6 @@ public class TelemetryService extends Service implements LocationListener {
                                telemetry_state.states.put(serial, saved_state.state);
                        }
                }
-
-               // Listen for GPS and Network position updates
-               LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
-
-               locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
        }
 
        @Override
@@ -484,7 +513,11 @@ public class TelemetryService extends Service implements LocationListener {
                // Move us into the foreground.
                startForeground(NOTIFICATION, notification);
 
-               if (intent != null) {
+               /* Start bluetooth if we don't have a connection already */
+               if (intent != null &&
+                   (telemetry_state.connect == TelemetryState.CONNECT_NONE ||
+                    telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED))
+               {
                        String  action = intent.getAction();
 
                        if (action.equals(AltosDroid.ACTION_BLUETOOTH)) {
@@ -502,9 +535,6 @@ public class TelemetryService extends Service implements LocationListener {
        @Override
        public void onDestroy() {
 
-               // Stop listening for location updates
-               ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
-
                // Stop the bluetooth Comms threads
                disconnect(true);
 
@@ -519,21 +549,4 @@ public class TelemetryService extends Service implements LocationListener {
        public IBinder onBind(Intent intent) {
                return messenger.getBinder();
        }
-
-
-       public void onLocationChanged(Location location) {
-               telemetry_state.location = location;
-               AltosDebug.debug("location changed");
-               send_to_clients();
-       }
-
-       public void onStatusChanged(String provider, int status, Bundle extras) {
-       }
-
-       public void onProviderEnabled(String provider) {
-       }
-
-       public void onProviderDisabled(String provider) {
-       }
-
 }