1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-20 12:00:51 +02:00

Get and display Pebble firmware version when connected.

This commit is contained in:
Andreas Shimokawa 2015-03-22 00:34:54 +01:00
parent 4e465928e6
commit 05c07aa611
7 changed files with 96 additions and 45 deletions

View File

@ -46,6 +46,8 @@ public class BluetoothCommunicationService extends Service {
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.settime"; = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.settime";
public static final String ACTION_SETMUSICINFO public static final String ACTION_SETMUSICINFO
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.setmusicinfo"; = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.setmusicinfo";
public static final String ACTION_REQUEST_VERSIONINFO
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.request_versioninfo";
private static final String TAG = "BluetoothCommunicationService"; private static final String TAG = "BluetoothCommunicationService";
private static final int NOTIFICATION_ID = 1; private static final int NOTIFICATION_ID = 1;
private BluetoothAdapter mBtAdapter = null; private BluetoothAdapter mBtAdapter = null;
@ -53,6 +55,7 @@ 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 void setReceiversEnableState(boolean enable) { private void setReceiversEnableState(boolean enable) {
final Class[] receiverClasses = { final Class[] receiverClasses = {
@ -119,16 +122,22 @@ public class BluetoothCommunicationService extends Service {
switch (cmdBundle.commandClass) { switch (cmdBundle.commandClass) {
case MUSIC_CONTROL: case MUSIC_CONTROL:
Log.i(TAG, "Got command for MUSIC_CONTROL"); Log.i(TAG, "Got command for MUSIC_CONTROL");
Intent musicintent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL); Intent musicIntent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL);
musicintent.putExtra("command", cmdBundle.command.ordinal()); musicIntent.putExtra("command", cmdBundle.command.ordinal());
sendBroadcast(musicintent); sendBroadcast(musicIntent);
break; break;
case CALL_CONTROL: case CALL_CONTROL:
Log.i(TAG, "Got command for CALL_CONTROL"); Log.i(TAG, "Got command for CALL_CONTROL");
Intent callintent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL); Intent callIntent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL);
callintent.putExtra("command", cmdBundle.command.ordinal()); callIntent.putExtra("command", cmdBundle.command.ordinal());
sendBroadcast(callintent); sendBroadcast(callIntent);
break; break;
case VERSION_INFO:
Log.i(TAG, "Got command for VERSION INFO");
Intent versionIntent = new Intent(ControlCenter.ACTION_SET_VERSION);
versionIntent.putExtra("device_address", mBtDeviceAddress);
versionIntent.putExtra("firmware_version", cmdBundle.info);
sendBroadcast(versionIntent);
default: default:
break; break;
} }
@ -167,11 +176,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 {
String btDeviceAddress = intent.getStringExtra("device_address"); mBtDeviceAddress = intent.getStringExtra("device_address");
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit(); sharedPrefs.edit().putString("last_device_address", mBtDeviceAddress).commit();
if (btDeviceAddress != null && (mBtSocket == null || !mBtSocket.isConnected())) { if (mBtDeviceAddress != 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();
@ -181,7 +190,7 @@ public class BluetoothCommunicationService extends Service {
e.printStackTrace(); e.printStackTrace();
} }
} }
mBtSocketIoThread = new BtSocketIoThread(btDeviceAddress); mBtSocketIoThread = new BtSocketIoThread(mBtDeviceAddress);
mBtSocketIoThread.start(); mBtSocketIoThread.start();
} }
} }
@ -220,6 +229,9 @@ public class BluetoothCommunicationService extends Service {
String track = intent.getStringExtra("music_track"); String track = intent.getStringExtra("music_track");
byte[] msg = PebbleProtocol.encodeSetMusicInfo(artist, album, track); byte[] msg = PebbleProtocol.encodeSetMusicInfo(artist, album, track);
mBtSocketIoThread.write(msg); mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_REQUEST_VERSIONINFO)) {
byte[] msg = PebbleProtocol.encodeFirmwareVersionReq();
mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_START)) { } else if (intent.getAction().equals(ACTION_START)) {
startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running")); startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running"));
mStarted = true; mStarted = true;
@ -350,6 +362,7 @@ public class BluetoothCommunicationService extends Service {
if (length == 1 && endpoint == PebbleProtocol.ENDPOINT_PHONEVERSION) { if (length == 1 && endpoint == PebbleProtocol.ENDPOINT_PHONEVERSION) {
Log.i(TAG, "Pebble asked for Phone/App Version - repLYING!"); Log.i(TAG, "Pebble asked for Phone/App Version - repLYING!");
write(PebbleProtocol.encodePhoneVersion(PebbleProtocol.PHONEVERSION_REMOTE_OS_ANDROID)); write(PebbleProtocol.encodePhoneVersion(PebbleProtocol.PHONEVERSION_REMOTE_OS_ANDROID));
write(PebbleProtocol.encodeFirmwareVersionReq());
} else if (endpoint != PebbleProtocol.ENDPOINT_DATALOG) { } else if (endpoint != PebbleProtocol.ENDPOINT_DATALOG) {
GBCommandBundle cmdBundle = PebbleProtocol.decodeResponse(buffer); GBCommandBundle cmdBundle = PebbleProtocol.decodeResponse(buffer);
if (cmdBundle == null) { if (cmdBundle == null) {

View File

@ -1,7 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge; package nodomain.freeyourgadget.gadgetbridge;
import android.app.Activity; import android.app.Activity;
import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -11,7 +10,6 @@ import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -31,6 +29,9 @@ public class ControlCenter extends Activity {
public static final String ACTION_QUIT public static final String ACTION_QUIT
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit"; = "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit";
public static final String ACTION_SET_VERSION
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.set_version";
ListView deviceListView; ListView deviceListView;
GBDeviceAdapter mGBDeviceAdapter; GBDeviceAdapter mGBDeviceAdapter;
final List<GBDevice> deviceList = new ArrayList<>(); final List<GBDevice> deviceList = new ArrayList<>();
@ -38,8 +39,20 @@ public class ControlCenter extends Activity {
private BroadcastReceiver mReceiver = new BroadcastReceiver() { private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_QUIT)) { String action = intent.getAction();
if (action.equals(ACTION_QUIT)) {
finish(); finish();
} else if (action.equals(ACTION_SET_VERSION)) {
String deviceAddress = intent.getStringExtra("device_address");
String firmwareVersion = intent.getStringExtra("firmware_version");
for (GBDevice device : deviceList) {
if (device.getAddress().equals(deviceAddress)) {
device.setFirmwareVersion(firmwareVersion);
mGBDeviceAdapter.notifyDataSetChanged();
break;
}
}
} }
} }
}; };
@ -63,7 +76,10 @@ public class ControlCenter extends Activity {
} }
}); });
registerReceiver(mReceiver, new IntentFilter(ACTION_QUIT)); IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_QUIT);
filter.addAction(ACTION_SET_VERSION);
registerReceiver(mReceiver, filter);
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) { if (btAdapter == null) {
@ -89,21 +105,13 @@ public class ControlCenter extends Activity {
Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(enableIntent); startActivity(enableIntent);
} }
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class); Intent startIntent = new Intent(this, BluetoothCommunicationService.class);
startIntent.setAction(BluetoothCommunicationService.ACTION_START); startIntent.setAction(BluetoothCommunicationService.ACTION_START);
startService(startIntent); startService(startIntent);
} Intent versionInfoIntent = new Intent(this, BluetoothCommunicationService.class);
versionInfoIntent.setAction(BluetoothCommunicationService.ACTION_REQUEST_VERSIONINFO);
private void testNotification() { startService(versionInfoIntent);
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this);
ncomp.setContentTitle("Test Notification");
ncomp.setContentText("This is a Test Notification from Gadgetbridge");
ncomp.setTicker("This is a Test Notification from Gadgetbridge");
ncomp.setSmallIcon(R.drawable.ic_launcher);
ncomp.setAutoCancel(true);
nManager.notify((int) System.currentTimeMillis(), ncomp.build());
} }
@Override @Override

View File

@ -16,4 +16,6 @@ public enum GBCommand {
MUSIC_PLAYPAUSE, MUSIC_PLAYPAUSE,
MUSIC_NEXT, MUSIC_NEXT,
MUSIC_PREVIOUS, MUSIC_PREVIOUS,
VERSION_FIRMWARE
} }

View File

@ -4,4 +4,5 @@ package nodomain.freeyourgadget.gadgetbridge;
public class GBCommandBundle { public class GBCommandBundle {
public GBCommandClass commandClass; public GBCommandClass commandClass;
public GBCommand command; public GBCommand command;
public String info;
} }

View File

@ -2,5 +2,6 @@ package nodomain.freeyourgadget.gadgetbridge;
public enum GBCommandClass { public enum GBCommandClass {
MUSIC_CONTROL, MUSIC_CONTROL,
CALL_CONTROL CALL_CONTROL,
VERSION_INFO
} }

View File

@ -1,14 +1,20 @@
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;
public GBDevice(String address, String name) { public GBDevice(String address, String name) {
this.address = address; this.address = address;
this.name = name; this.name = name;
} }
public void setFirmwareVersion(String firmwareVersion) {
this.firmwareVersion = firmwareVersion;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -18,6 +24,10 @@ public class GBDevice {
} }
public String getStatus() { public String getStatus() {
return ""; if (firmwareVersion != null) {
return "Firmware Version: " + firmwareVersion;
} else {
return null;
}
} }
} }

View File

@ -94,14 +94,15 @@ public class PebbleProtocol {
static byte[] encodeMessage(short endpoint, byte type, int cookie, String[] parts) { static byte[] encodeMessage(short endpoint, byte type, int cookie, String[] parts) {
// Calculate length first // Calculate length first
int length = LENGTH_PREFIX + 1; int length = LENGTH_PREFIX + 1;
for (String s : parts) { if (parts != null) {
if (s == null || s.equals("")) { for (String s : parts) {
length++; // encode null or empty strings as 0x00 later if (s == null || s.equals("")) {
continue; length++; // encode null or empty strings as 0x00 later
continue;
}
length += (1 + s.getBytes().length);
} }
length += (1 + s.getBytes().length);
} }
if (endpoint == ENDPOINT_PHONECONTROL) { if (endpoint == ENDPOINT_PHONECONTROL) {
length += 4; //for cookie; length += 4; //for cookie;
} }
@ -117,19 +118,20 @@ public class PebbleProtocol {
buf.putInt(cookie); buf.putInt(cookie);
} }
// Encode Pascal-Style Strings // Encode Pascal-Style Strings
for (String s : parts) { if (parts != null) {
if (s == null || s.equals("")) { for (String s : parts) {
//buf.put((byte)0x01); if (s == null || s.equals("")) {
buf.put((byte) 0x00); //buf.put((byte)0x01);
continue; buf.put((byte) 0x00);
continue;
}
int partlength = s.getBytes().length;
if (partlength > 255) partlength = 255;
buf.put((byte) partlength);
buf.put(s.getBytes(), 0, partlength);
} }
int partlength = s.getBytes().length;
if (partlength > 255) partlength = 255;
buf.put((byte) partlength);
buf.put(s.getBytes(), 0, partlength);
} }
return buf.array(); return buf.array();
} }
@ -204,6 +206,10 @@ public class PebbleProtocol {
return encodeMessage(ENDPOINT_MUSICCONTROL, MUSICCONTROL_SETMUSICINFO, 0, parts); return encodeMessage(ENDPOINT_MUSICCONTROL, MUSICCONTROL_SETMUSICINFO, 0, parts);
} }
public static byte[] encodeFirmwareVersionReq() {
return encodeMessage(ENDPOINT_FIRMWAREVERSION, (byte) 0, 0, null);
}
public static byte[] encodePhoneVersion(byte os) { public static byte[] encodePhoneVersion(byte os) {
ByteBuffer buf = ByteBuffer.allocate(LENGTH_PHONEVERSION); ByteBuffer buf = ByteBuffer.allocate(LENGTH_PHONEVERSION);
buf.order(ByteOrder.BIG_ENDIAN); buf.order(ByteOrder.BIG_ENDIAN);
@ -270,6 +276,16 @@ public class PebbleProtocol {
break; break;
} }
break; break;
case ENDPOINT_FIRMWAREVERSION:
int version = buf.getInt();
byte[] versionString = new byte[32];
buf.get(versionString, 0, 32);
cmd.commandClass = GBCommandClass.VERSION_INFO;
cmd.command = GBCommand.VERSION_FIRMWARE;
cmd.info = new String(versionString).trim();
Log.i(TAG, "Got firmware version: " + cmd.info);
break;
default: default:
return null; return null;
} }