From 2e7f45433a5745451531e27d73d20dcccf11d0e9 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sun, 22 Mar 2015 23:38:51 +0100 Subject: [PATCH] display connection status in the device list --- CHANGELOG.md | 2 +- README.md | 2 +- .../BluetoothCommunicationService.java | 51 ++++++++++++++----- .../gadgetbridge/ControlCenter.java | 13 +++-- .../freeyourgadget/gadgetbridge/GBDevice.java | 41 +++++++++++++-- .../gadgetbridge/adapter/GBDeviceAdapter.java | 2 +- app/src/main/res/menu/menu_debug.xml | 7 --- app/src/main/res/menu/menu_main.xml | 2 + app/src/main/res/values/strings.xml | 1 + 9 files changed, 90 insertions(+), 31 deletions(-) delete mode 100644 app/src/main/res/menu/menu_debug.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 620505499..f4c01cc55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ####Version 0.1.3 * Remove the connect button, list all suported devices and connect on tap instead -* Display firmware version of connected devices +* Display connection status and firmware of connected devices in the device list * Remove quit button from the service notification, put a quit item in the context menu instead ####Version 0.1.2 diff --git a/README.md b/README.md index 8d848e8de..207a6c901 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Features: How to use: 1. Pair your Pebble though the Android Bluetooth Settings -2. Start Gadgetbridge, press "connect" +2. Start Gadgetbridge, tap on the device you want to connect to 3. To test, chose "Debug" from the menu and play around Known Issues: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index 88f7f23a9..ba99aecd6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -55,7 +55,8 @@ public class BluetoothCommunicationService extends Service { private BtSocketIoThread mBtSocketIoThread = null; private boolean mStarted = false; - private String mBtDeviceAddress; + + private GBDevice gbdevice = null; private void setReceiversEnableState(boolean enable) { final Class[] receiverClasses = { @@ -129,14 +130,22 @@ public class BluetoothCommunicationService extends Service { break; case VERSION_INFO: Log.i(TAG, "Got command for VERSION INFO"); - Intent versionIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST); - versionIntent.putExtra("device_address", mBtDeviceAddress); - versionIntent.putExtra("firmware_version", cmdBundle.info); - sendBroadcast(versionIntent); + if (gbdevice == null) { + return; + } + gbdevice.setFirmwareVersion(cmdBundle.info); + sendDeviceUpdateIntent(); default: break; } + } + private void sendDeviceUpdateIntent() { + Intent deviceUpdateIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST); + deviceUpdateIntent.putExtra("device_address", gbdevice.getAddress()); + deviceUpdateIntent.putExtra("device_state", gbdevice.getState().ordinal()); + deviceUpdateIntent.putExtra("firmware_version", gbdevice.getFirmwareVersion()); + sendBroadcast(deviceUpdateIntent); } @Override @@ -171,11 +180,11 @@ public class BluetoothCommunicationService extends Service { } else if (!mBtAdapter.isEnabled()) { Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show(); } else { - mBtDeviceAddress = intent.getStringExtra("device_address"); + String btDeviceAddress = intent.getStringExtra("device_address"); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - sharedPrefs.edit().putString("last_device_address", mBtDeviceAddress).commit(); + sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit(); - if (mBtDeviceAddress != null && (mBtSocket == null || !mBtSocket.isConnected())) { + if (btDeviceAddress != null && (mBtSocket == null || !mBtSocket.isConnected())) { // currently only one thread allowed if (mBtSocketIoThread != null) { mBtSocketIoThread.quit(); @@ -184,9 +193,17 @@ public class BluetoothCommunicationService extends Service { } catch (InterruptedException e) { e.printStackTrace(); } + + } + BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress); + if (btDevice != null) { + gbdevice = new GBDevice(btDeviceAddress, btDevice.getName()); + gbdevice.setState(GBDevice.State.CONNECTING); + sendDeviceUpdateIntent(); + + mBtSocketIoThread = new BtSocketIoThread(btDeviceAddress); + mBtSocketIoThread.start(); } - mBtSocketIoThread = new BtSocketIoThread(mBtDeviceAddress); - mBtSocketIoThread.start(); } } } else if (action.equals(ACTION_NOTIFICATION_GENERIC)) { @@ -225,8 +242,12 @@ public class BluetoothCommunicationService extends Service { byte[] msg = PebbleProtocol.encodeSetMusicInfo(artist, album, track); mBtSocketIoThread.write(msg); } else if (action.equals(ACTION_REQUEST_VERSIONINFO)) { - byte[] msg = PebbleProtocol.encodeFirmwareVersionReq(); - mBtSocketIoThread.write(msg); + if (gbdevice != null && gbdevice.getFirmwareVersion() == null) { + byte[] msg = PebbleProtocol.encodeFirmwareVersionReq(); + mBtSocketIoThread.write(msg); + } else { + sendDeviceUpdateIntent(); + } } else if (action.equals(ACTION_START)) { startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running")); mStarted = true; @@ -312,6 +333,8 @@ public class BluetoothCommunicationService extends Service { mBtSocket = null; return false; } + gbdevice.setState(GBDevice.State.CONNECTED); + sendDeviceUpdateIntent(); updateNotification("connected to " + btDevice.getName()); return true; } @@ -373,6 +396,8 @@ public class BluetoothCommunicationService extends Service { } } catch (IOException e) { if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right + gbdevice.setState(GBDevice.State.CONNECTING); + sendDeviceUpdateIntent(); updateNotification("connection lost, trying to reconnect"); while (mmConnectionAttempts++ < 10) { @@ -401,6 +426,8 @@ public class BluetoothCommunicationService extends Service { } mBtSocket = null; updateNotification("not connected"); + gbdevice.setState(GBDevice.State.NOT_CONNECTED); + sendDeviceUpdateIntent(); } synchronized public void write(byte[] bytes) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java index 0cced562b..71bb7f48a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java @@ -44,6 +44,7 @@ public class ControlCenter extends Activity { finish(); } else if (action.equals(ACTION_REFRESH_DEVICELIST)) { String deviceAddress = intent.getStringExtra("device_address"); + GBDevice.State state = GBDevice.State.values()[intent.getIntExtra("device_state", 0)]; String firmwareVersion = intent.getStringExtra("firmware_version"); if (deviceList.isEmpty()) { refreshPairedDevices(); @@ -53,6 +54,7 @@ public class ControlCenter extends Activity { for (GBDevice device : deviceList) { if (device.getAddress().equals(deviceAddress)) { device.setFirmwareVersion(firmwareVersion); + device.setState(state); mGBDeviceAdapter.notifyDataSetChanged(); break; } @@ -124,18 +126,21 @@ public class ControlCenter extends Activity { Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); return true; - } - else if (id == R.id.action_debug) { + } else if (id == R.id.action_debug) { Intent intent = new Intent(this, DebugActivity.class); startActivity(intent); return true; - } - else if (id == R.id.action_quit) { + } else if (id == R.id.action_quit) { Intent stopIntent = new Intent(this, BluetoothCommunicationService.class); stopService(stopIntent); Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT); sendBroadcast(quitIntent); + } else if (id == R.id.action_refresh) { + if (deviceList.isEmpty()) { + refreshPairedDevices(); + mGBDeviceAdapter.notifyDataSetChanged(); + } } return super.onOptionsItemSelected(item); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java index 3c526bf76..dbe3bcc57 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java @@ -1,10 +1,21 @@ package nodomain.freeyourgadget.gadgetbridge; public class GBDevice { - private boolean isConnected = false; private final String name; private final String address; - private String firmwareVersion; + private String firmwareVersion = null; + private State state = State.NOT_CONNECTED; + + public void setState(State state) { + this.state = state; + } + + public enum State { + NOT_CONNECTED, + CONNECTING, + CONNECTED + } + public GBDevice(String address, String name) { this.address = address; @@ -23,11 +34,31 @@ public class GBDevice { return address; } - public String getStatus() { + public String getFirmwareVersion() { + return firmwareVersion; + } + + public State getState() { + return state; + } + + String getStateString() { + switch (state) { + case NOT_CONNECTED: + return "not connected"; // TODO: do not hardcode + case CONNECTING: + return "connecting"; + case CONNECTED: + return "connected"; + } + return "unknown state"; + } + + public String getInfoString() { if (firmwareVersion != null) { - return "Firmware Version: " + firmwareVersion; + return getStateString() + " (FW: " + firmwareVersion + ")"; } else { - return null; + return getStateString(); } } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapter.java index f8afb38e8..4c55695a6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/GBDeviceAdapter.java @@ -36,7 +36,7 @@ public class GBDeviceAdapter extends ArrayAdapter { } TextView deviceStatusLabel = (TextView) view.findViewById(R.id.device_status); TextView deviceNameLabel = (TextView) view.findViewById(R.id.device_name); - deviceStatusLabel.setText(device.getStatus()); + deviceStatusLabel.setText(device.getInfoString()); deviceNameLabel.setText(device.getName()); return view; diff --git a/app/src/main/res/menu/menu_debug.xml b/app/src/main/res/menu/menu_debug.xml deleted file mode 100644 index 85b36d51a..000000000 --- a/app/src/main/res/menu/menu_debug.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index cf9153fd8..1f43dfd67 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -2,6 +2,8 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="nodomain.freeyourgadget.gadgetbridge.ControlCenter"> + Debug Quit Debug + Refresh