display connection status in the device list

This commit is contained in:
Andreas Shimokawa
2015-03-22 23:38:51 +01:00
parent ecb7a9f3b5
commit 2e7f45433a
9 changed files with 90 additions and 31 deletions

View File

@@ -2,7 +2,7 @@
####Version 0.1.3 ####Version 0.1.3
* Remove the connect button, list all suported devices and connect on tap instead * 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 * Remove quit button from the service notification, put a quit item in the context menu instead
####Version 0.1.2 ####Version 0.1.2

View File

@@ -21,7 +21,7 @@ Features:
How to use: How to use:
1. Pair your Pebble though the Android Bluetooth Settings 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 3. To test, chose "Debug" from the menu and play around
Known Issues: Known Issues:

View File

@@ -55,7 +55,8 @@ public class BluetoothCommunicationService extends Service {
private BtSocketIoThread mBtSocketIoThread = null; private BtSocketIoThread mBtSocketIoThread = null;
private boolean mStarted = false; private boolean mStarted = false;
private String mBtDeviceAddress;
private GBDevice gbdevice = null;
private void setReceiversEnableState(boolean enable) { private void setReceiversEnableState(boolean enable) {
final Class[] receiverClasses = { final Class[] receiverClasses = {
@@ -129,14 +130,22 @@ public class BluetoothCommunicationService extends Service {
break; break;
case VERSION_INFO: case VERSION_INFO:
Log.i(TAG, "Got command for VERSION INFO"); Log.i(TAG, "Got command for VERSION INFO");
Intent versionIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST); if (gbdevice == null) {
versionIntent.putExtra("device_address", mBtDeviceAddress); return;
versionIntent.putExtra("firmware_version", cmdBundle.info); }
sendBroadcast(versionIntent); gbdevice.setFirmwareVersion(cmdBundle.info);
sendDeviceUpdateIntent();
default: default:
break; 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 @Override
@@ -171,11 +180,11 @@ public class BluetoothCommunicationService extends Service {
} else if (!mBtAdapter.isEnabled()) { } else if (!mBtAdapter.isEnabled()) {
Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
} else { } else {
mBtDeviceAddress = intent.getStringExtra("device_address"); String btDeviceAddress = intent.getStringExtra("device_address");
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); 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 // currently only one thread allowed
if (mBtSocketIoThread != null) { if (mBtSocketIoThread != null) {
mBtSocketIoThread.quit(); mBtSocketIoThread.quit();
@@ -184,9 +193,17 @@ public class BluetoothCommunicationService extends Service {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); 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)) { } else if (action.equals(ACTION_NOTIFICATION_GENERIC)) {
@@ -225,8 +242,12 @@ public class BluetoothCommunicationService extends Service {
byte[] msg = PebbleProtocol.encodeSetMusicInfo(artist, album, track); byte[] msg = PebbleProtocol.encodeSetMusicInfo(artist, album, track);
mBtSocketIoThread.write(msg); mBtSocketIoThread.write(msg);
} else if (action.equals(ACTION_REQUEST_VERSIONINFO)) { } else if (action.equals(ACTION_REQUEST_VERSIONINFO)) {
byte[] msg = PebbleProtocol.encodeFirmwareVersionReq(); if (gbdevice != null && gbdevice.getFirmwareVersion() == null) {
mBtSocketIoThread.write(msg); byte[] msg = PebbleProtocol.encodeFirmwareVersionReq();
mBtSocketIoThread.write(msg);
} else {
sendDeviceUpdateIntent();
}
} else if (action.equals(ACTION_START)) { } else if (action.equals(ACTION_START)) {
startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running")); startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running"));
mStarted = true; mStarted = true;
@@ -312,6 +333,8 @@ public class BluetoothCommunicationService extends Service {
mBtSocket = null; mBtSocket = null;
return false; return false;
} }
gbdevice.setState(GBDevice.State.CONNECTED);
sendDeviceUpdateIntent();
updateNotification("connected to " + btDevice.getName()); updateNotification("connected to " + btDevice.getName());
return true; return true;
} }
@@ -373,6 +396,8 @@ public class BluetoothCommunicationService extends Service {
} }
} catch (IOException e) { } catch (IOException e) {
if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right
gbdevice.setState(GBDevice.State.CONNECTING);
sendDeviceUpdateIntent();
updateNotification("connection lost, trying to reconnect"); updateNotification("connection lost, trying to reconnect");
while (mmConnectionAttempts++ < 10) { while (mmConnectionAttempts++ < 10) {
@@ -401,6 +426,8 @@ public class BluetoothCommunicationService extends Service {
} }
mBtSocket = null; mBtSocket = null;
updateNotification("not connected"); updateNotification("not connected");
gbdevice.setState(GBDevice.State.NOT_CONNECTED);
sendDeviceUpdateIntent();
} }
synchronized public void write(byte[] bytes) { synchronized public void write(byte[] bytes) {

View File

@@ -44,6 +44,7 @@ public class ControlCenter extends Activity {
finish(); finish();
} else if (action.equals(ACTION_REFRESH_DEVICELIST)) { } else if (action.equals(ACTION_REFRESH_DEVICELIST)) {
String deviceAddress = intent.getStringExtra("device_address"); String deviceAddress = intent.getStringExtra("device_address");
GBDevice.State state = GBDevice.State.values()[intent.getIntExtra("device_state", 0)];
String firmwareVersion = intent.getStringExtra("firmware_version"); String firmwareVersion = intent.getStringExtra("firmware_version");
if (deviceList.isEmpty()) { if (deviceList.isEmpty()) {
refreshPairedDevices(); refreshPairedDevices();
@@ -53,6 +54,7 @@ public class ControlCenter extends Activity {
for (GBDevice device : deviceList) { for (GBDevice device : deviceList) {
if (device.getAddress().equals(deviceAddress)) { if (device.getAddress().equals(deviceAddress)) {
device.setFirmwareVersion(firmwareVersion); device.setFirmwareVersion(firmwareVersion);
device.setState(state);
mGBDeviceAdapter.notifyDataSetChanged(); mGBDeviceAdapter.notifyDataSetChanged();
break; break;
} }
@@ -124,18 +126,21 @@ public class ControlCenter extends Activity {
Intent intent = new Intent(this, SettingsActivity.class); Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent); startActivity(intent);
return true; return true;
} } else if (id == R.id.action_debug) {
else if (id == R.id.action_debug) {
Intent intent = new Intent(this, DebugActivity.class); Intent intent = new Intent(this, DebugActivity.class);
startActivity(intent); startActivity(intent);
return true; return true;
} } else if (id == R.id.action_quit) {
else if (id == R.id.action_quit) {
Intent stopIntent = new Intent(this, BluetoothCommunicationService.class); Intent stopIntent = new Intent(this, BluetoothCommunicationService.class);
stopService(stopIntent); stopService(stopIntent);
Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT); Intent quitIntent = new Intent(ControlCenter.ACTION_QUIT);
sendBroadcast(quitIntent); sendBroadcast(quitIntent);
} else if (id == R.id.action_refresh) {
if (deviceList.isEmpty()) {
refreshPairedDevices();
mGBDeviceAdapter.notifyDataSetChanged();
}
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);

View File

@@ -1,10 +1,21 @@
package nodomain.freeyourgadget.gadgetbridge; package nodomain.freeyourgadget.gadgetbridge;
public class GBDevice { public class GBDevice {
private boolean isConnected = false;
private final String name; private final String name;
private final String address; 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) { public GBDevice(String address, String name) {
this.address = address; this.address = address;
@@ -23,11 +34,31 @@ public class GBDevice {
return address; 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) { if (firmwareVersion != null) {
return "Firmware Version: " + firmwareVersion; return getStateString() + " (FW: " + firmwareVersion + ")";
} else { } else {
return null; return getStateString();
} }
} }
} }

View File

@@ -36,7 +36,7 @@ public class GBDeviceAdapter extends ArrayAdapter<GBDevice> {
} }
TextView deviceStatusLabel = (TextView) view.findViewById(R.id.device_status); TextView deviceStatusLabel = (TextView) view.findViewById(R.id.device_status);
TextView deviceNameLabel = (TextView) view.findViewById(R.id.device_name); TextView deviceNameLabel = (TextView) view.findViewById(R.id.device_name);
deviceStatusLabel.setText(device.getStatus()); deviceStatusLabel.setText(device.getInfoString());
deviceNameLabel.setText(device.getName()); deviceNameLabel.setText(device.getName());
return view; return view;

View File

@@ -1,7 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="nodomain.freeyourgadget.gadgetbridge.DebugActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>

View File

@@ -2,6 +2,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:context="nodomain.freeyourgadget.gadgetbridge.ControlCenter"> tools:context="nodomain.freeyourgadget.gadgetbridge.ControlCenter">
<item android:id="@+id/action_refresh" android:title="@string/action_refresh"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_settings" android:title="@string/action_settings" <item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" /> android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_debug" android:title="@string/action_debug" <item android:id="@+id/action_debug" android:title="@string/action_debug"

View File

@@ -6,4 +6,5 @@
<string name="action_debug">Debug</string> <string name="action_debug">Debug</string>
<string name="action_quit">Quit</string> <string name="action_quit">Quit</string>
<string name="title_activity_debug">Debug</string> <string name="title_activity_debug">Debug</string>
<string name="action_refresh" >Refresh</string>
</resources> </resources>