From f49fd5d2be68de97ebe65fa4f6484746e91dd677 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 22 Apr 2016 18:53:32 -0400 Subject: [PATCH] altosdroid: Run even without Bluetooth This lets us view old state information on USB-only devices, or when the bluetooth device can't be enabled. Signed-off-by: Keith Packard --- .../altusmetrum/AltosDroid/AltosDroid.java | 41 +++++++++---------- .../AltosDroid/TelemetryService.java | 24 +++++++---- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 85513325..4f1dcb1a 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -564,22 +564,15 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener, mAgeOldColor = getResources().getColor(R.color.old_color); } - private boolean ensureBluetooth() { + private void ensureBluetooth() { // Get local Bluetooth adapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - // If the adapter is null, then Bluetooth is not supported - if (mBluetoothAdapter == null) { - Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); - return false; - } - - if (!mBluetoothAdapter.isEnabled()) { + /* if there is a BT adapter and it isn't turned on, then turn it on */ + if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, AltosDroid.REQUEST_ENABLE_BT); } - - return true; } private boolean check_usb() { @@ -645,9 +638,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener, return; } AltosDebug.debug("Starting by looking for bluetooth devices"); - if (ensureBluetooth()) - return; - finish(); + ensureBluetooth(); } } @@ -738,12 +729,11 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener, if (resultCode == Activity.RESULT_OK) { // Bluetooth is now enabled, so set up a chat session //setupChat(); + AltosDebug.debug("BT enabled"); + bluetoothEnabled(data); } else { // User did not enable Bluetooth or an error occured - AltosDebug.error("BT not enabled"); - stopService(new Intent(AltosDroid.this, TelemetryService.class)); - Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show(); - finish(); + AltosDebug.debug("BT not enabled"); } break; case REQUEST_MAP_TYPE: @@ -767,6 +757,14 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener, } } + private void bluetoothEnabled(Intent data) { + try { + mService.send(Message.obtain(null, TelemetryService.MSG_BLUETOOTH_ENABLED, null)); + } catch (RemoteException e) { + AltosDebug.debug("send BT enabled message failed"); + } + } + private void connectDevice(Intent data) { // Attempt to connect to the device try { @@ -907,11 +905,10 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener, Intent serverIntent = null; switch (item.getItemId()) { case R.id.connect_scan: - if (ensureBluetooth()) { - // Launch the DeviceListActivity to see devices and do scan - serverIntent = new Intent(this, DeviceListActivity.class); - startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE); - } + ensureBluetooth(); + // Launch the DeviceListActivity to see devices and do scan + serverIntent = new Intent(this, DeviceListActivity.class); + startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE); return true; case R.id.disconnect: /* Disconnect the device diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 1834d55b..926109e2 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -57,6 +57,7 @@ public class TelemetryService extends Service { 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. @@ -86,6 +87,8 @@ public class TelemetryService extends Service { @Override public void handleMessage(Message msg) { + DeviceAddress address; + TelemetryService s = service.get(); AltosDroidLink bt = null; if (s == null) @@ -101,7 +104,7 @@ public class TelemetryService extends Service { 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; @@ -203,6 +206,12 @@ public class TelemetryService extends Service { s.telemetry_state.crc_errors = (Integer) msg.obj; 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); } @@ -348,10 +357,14 @@ public class TelemetryService extends Service { } 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); @@ -441,11 +454,6 @@ public class TelemetryService extends Service { // 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(); - } - telemetry_state = new TelemetryState(); // Create a reference to the NotificationManager so that we can update our notifcation text later -- 2.30.2