1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-19 11:30:44 +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";
public static final String 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 int NOTIFICATION_ID = 1;
private BluetoothAdapter mBtAdapter = null;
@ -53,6 +55,7 @@ public class BluetoothCommunicationService extends Service {
private BtSocketIoThread mBtSocketIoThread = null;
private boolean mStarted = false;
private String mBtDeviceAddress;
private void setReceiversEnableState(boolean enable) {
final Class[] receiverClasses = {
@ -119,16 +122,22 @@ public class BluetoothCommunicationService extends Service {
switch (cmdBundle.commandClass) {
case MUSIC_CONTROL:
Log.i(TAG, "Got command for MUSIC_CONTROL");
Intent musicintent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL);
musicintent.putExtra("command", cmdBundle.command.ordinal());
sendBroadcast(musicintent);
Intent musicIntent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL);
musicIntent.putExtra("command", cmdBundle.command.ordinal());
sendBroadcast(musicIntent);
break;
case CALL_CONTROL:
Log.i(TAG, "Got command for CALL_CONTROL");
Intent callintent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL);
callintent.putExtra("command", cmdBundle.command.ordinal());
sendBroadcast(callintent);
Intent callIntent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL);
callIntent.putExtra("command", cmdBundle.command.ordinal());
sendBroadcast(callIntent);
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:
break;
}
@ -167,11 +176,11 @@ public class BluetoothCommunicationService extends Service {
} else if (!mBtAdapter.isEnabled()) {
Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
} else {
String btDeviceAddress = intent.getStringExtra("device_address");
mBtDeviceAddress = intent.getStringExtra("device_address");
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
if (mBtSocketIoThread != null) {
mBtSocketIoThread.quit();
@ -181,7 +190,7 @@ public class BluetoothCommunicationService extends Service {
e.printStackTrace();
}
}
mBtSocketIoThread = new BtSocketIoThread(btDeviceAddress);
mBtSocketIoThread = new BtSocketIoThread(mBtDeviceAddress);
mBtSocketIoThread.start();
}
}
@ -220,6 +229,9 @@ public class BluetoothCommunicationService extends Service {
String track = intent.getStringExtra("music_track");
byte[] msg = PebbleProtocol.encodeSetMusicInfo(artist, album, track);
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)) {
startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running"));
mStarted = true;
@ -350,6 +362,7 @@ public class BluetoothCommunicationService extends Service {
if (length == 1 && endpoint == PebbleProtocol.ENDPOINT_PHONEVERSION) {
Log.i(TAG, "Pebble asked for Phone/App Version - repLYING!");
write(PebbleProtocol.encodePhoneVersion(PebbleProtocol.PHONEVERSION_REMOTE_OS_ANDROID));
write(PebbleProtocol.encodeFirmwareVersionReq());
} else if (endpoint != PebbleProtocol.ENDPOINT_DATALOG) {
GBCommandBundle cmdBundle = PebbleProtocol.decodeResponse(buffer);
if (cmdBundle == null) {

View File

@ -1,7 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge;
import android.app.Activity;
import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
@ -11,7 +10,6 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -31,6 +29,9 @@ public class ControlCenter extends Activity {
public static final String ACTION_QUIT
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit";
public static final String ACTION_SET_VERSION
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.set_version";
ListView deviceListView;
GBDeviceAdapter mGBDeviceAdapter;
final List<GBDevice> deviceList = new ArrayList<>();
@ -38,8 +39,20 @@ public class ControlCenter extends Activity {
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_QUIT)) {
String action = intent.getAction();
if (action.equals(ACTION_QUIT)) {
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();
if (btAdapter == null) {
@ -89,21 +105,13 @@ public class ControlCenter extends Activity {
Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(enableIntent);
}
Intent startIntent = new Intent(ControlCenter.this, BluetoothCommunicationService.class);
Intent startIntent = new Intent(this, BluetoothCommunicationService.class);
startIntent.setAction(BluetoothCommunicationService.ACTION_START);
startService(startIntent);
}
private void testNotification() {
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());
Intent versionInfoIntent = new Intent(this, BluetoothCommunicationService.class);
versionInfoIntent.setAction(BluetoothCommunicationService.ACTION_REQUEST_VERSIONINFO);
startService(versionInfoIntent);
}
@Override

View File

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

View File

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

View File

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

View File

@ -1,14 +1,20 @@
package nodomain.freeyourgadget.gadgetbridge;
public class GBDevice {
private boolean isConnected = false;
private final String name;
private final String address;
private String firmwareVersion;
public GBDevice(String address, String name) {
this.address = address;
this.name = name;
}
public void setFirmwareVersion(String firmwareVersion) {
this.firmwareVersion = firmwareVersion;
}
public String getName() {
return name;
}
@ -18,6 +24,10 @@ public class GBDevice {
}
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) {
// Calculate length first
int length = LENGTH_PREFIX + 1;
for (String s : parts) {
if (s == null || s.equals("")) {
length++; // encode null or empty strings as 0x00 later
continue;
if (parts != null) {
for (String s : parts) {
if (s == null || s.equals("")) {
length++; // encode null or empty strings as 0x00 later
continue;
}
length += (1 + s.getBytes().length);
}
length += (1 + s.getBytes().length);
}
if (endpoint == ENDPOINT_PHONECONTROL) {
length += 4; //for cookie;
}
@ -117,19 +118,20 @@ public class PebbleProtocol {
buf.putInt(cookie);
}
// Encode Pascal-Style Strings
for (String s : parts) {
if (s == null || s.equals("")) {
//buf.put((byte)0x01);
buf.put((byte) 0x00);
continue;
if (parts != null) {
for (String s : parts) {
if (s == null || s.equals("")) {
//buf.put((byte)0x01);
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();
}
@ -204,6 +206,10 @@ public class PebbleProtocol {
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) {
ByteBuffer buf = ByteBuffer.allocate(LENGTH_PHONEVERSION);
buf.order(ByteOrder.BIG_ENDIAN);
@ -270,6 +276,16 @@ public class PebbleProtocol {
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:
return null;
}