1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-23 18:36:50 +01:00

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
* 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

View File

@ -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:

View File

@ -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) {

View File

@ -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);

View File

@ -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();
}
}
}

View File

@ -36,7 +36,7 @@ public class GBDeviceAdapter extends ArrayAdapter<GBDevice> {
}
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;

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:tools="http://schemas.android.com/tools"
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"
android:orderInCategory="100" app:showAsAction="never" />
<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_quit">Quit</string>
<string name="title_activity_debug">Debug</string>
<string name="action_refresh" >Refresh</string>
</resources>