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

refresh list of paried devices when bluetooth gets turned on

This commit is contained in:
Andreas Shimokawa 2015-03-22 12:46:28 +01:00
parent 05c07aa611
commit 79c7085264
3 changed files with 46 additions and 34 deletions

View File

@ -134,7 +134,7 @@ public class BluetoothCommunicationService extends Service {
break;
case VERSION_INFO:
Log.i(TAG, "Got command for VERSION INFO");
Intent versionIntent = new Intent(ControlCenter.ACTION_SET_VERSION);
Intent versionIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST);
versionIntent.putExtra("device_address", mBtDeviceAddress);
versionIntent.putExtra("firmware_version", cmdBundle.info);
sendBroadcast(versionIntent);
@ -168,7 +168,7 @@ public class BluetoothCommunicationService extends Service {
return START_STICKY;
}
if (intent.getAction().equals(ACTION_CONNECT)) {
if (action.equals(ACTION_CONNECT)) {
//Check the system status
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBtAdapter == null) {
@ -194,24 +194,24 @@ public class BluetoothCommunicationService extends Service {
mBtSocketIoThread.start();
}
}
} else if (intent.getAction().equals(ACTION_NOTIFICATION_GENERIC)) {
} else if (action.equals(ACTION_NOTIFICATION_GENERIC)) {
String title = intent.getStringExtra("notification_title");
String body = intent.getStringExtra("notification_body");
byte[] msg = PebbleProtocol.encodeSMS(title, body);
mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_NOTIFICATION_SMS)) {
} else if (action.equals(ACTION_NOTIFICATION_SMS)) {
String sender = intent.getStringExtra("notification_sender");
String body = intent.getStringExtra("notification_body");
String senderName = getContactDisplayNameByNumber(sender);
byte[] msg = PebbleProtocol.encodeSMS(senderName, body);
mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_NOTIFICATION_EMAIL)) {
} else if (action.equals(ACTION_NOTIFICATION_EMAIL)) {
String sender = intent.getStringExtra("notification_sender");
String subject = intent.getStringExtra("notification_subject");
String body = intent.getStringExtra("notification_body");
byte[] msg = PebbleProtocol.encodeEmail(sender, subject, body);
mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_CALLSTATE)) {
} else if (action.equals(ACTION_CALLSTATE)) {
GBCommand command = GBCommand.values()[intent.getIntExtra("call_command", 0)]; // UGLY
String phoneNumber = intent.getStringExtra("call_phonenumber");
String callerName = null;
@ -220,19 +220,19 @@ public class BluetoothCommunicationService extends Service {
}
byte[] msg = PebbleProtocol.encodeSetCallState(phoneNumber, callerName, command);
mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_SETTIME)) {
} else if (action.equals(ACTION_SETTIME)) {
byte[] msg = PebbleProtocol.encodeSetTime(-1);
mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_SETMUSICINFO)) {
} else if (action.equals(ACTION_SETMUSICINFO)) {
String artist = intent.getStringExtra("music_artist");
String album = intent.getStringExtra("music_album");
String track = intent.getStringExtra("music_track");
byte[] msg = PebbleProtocol.encodeSetMusicInfo(artist, album, track);
mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_REQUEST_VERSIONINFO)) {
} else if (action.equals(ACTION_REQUEST_VERSIONINFO)) {
byte[] msg = PebbleProtocol.encodeFirmwareVersionReq();
mBtSocketIoThread.write(msg);
} else if (intent.getAction().equals(ACTION_START)) {
} else if (action.equals(ACTION_START)) {
startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running"));
mStarted = true;
}

View File

@ -14,6 +14,10 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver {
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) {
Intent refreshIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST);
context.sendBroadcast(refreshIntent);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!sharedPrefs.getBoolean("general_autoconnectonbluetooth", false)) {
return;

View File

@ -29,7 +29,7 @@ public class ControlCenter extends Activity {
public static final String ACTION_QUIT
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.quit";
public static final String ACTION_SET_VERSION
public static final String ACTION_REFRESH_DEVICELIST
= "nodomain.freeyourgadget.gadgetbride.controlcenter.action.set_version";
ListView deviceListView;
@ -42,15 +42,20 @@ public class ControlCenter extends Activity {
String action = intent.getAction();
if (action.equals(ACTION_QUIT)) {
finish();
} else if (action.equals(ACTION_SET_VERSION)) {
} else if (action.equals(ACTION_REFRESH_DEVICELIST)) {
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;
if (deviceList.isEmpty()) {
refreshPairedDevices();
mGBDeviceAdapter.notifyDataSetChanged();
}
if (deviceAddress != null) {
for (GBDevice device : deviceList) {
if (device.getAddress().equals(deviceAddress)) {
device.setFirmwareVersion(firmwareVersion);
mGBDeviceAdapter.notifyDataSetChanged();
break;
}
}
}
}
@ -78,24 +83,10 @@ public class ControlCenter extends Activity {
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_QUIT);
filter.addAction(ACTION_SET_VERSION);
filter.addAction(ACTION_REFRESH_DEVICELIST);
registerReceiver(mReceiver, filter);
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Toast.makeText(this, "Bluetooth is not supported.", Toast.LENGTH_SHORT).show();
} else if (!btAdapter.isEnabled()) {
Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
} else {
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
if (device.getName().indexOf("Pebble") == 0) {
// Matching device found
deviceList.add(new GBDevice(device.getAddress(), device.getName()));
}
}
}
refreshPairedDevices();
/*
* Ask for permission to intercept notifications on first run.
*/
@ -149,4 +140,21 @@ public class ControlCenter extends Activity {
unregisterReceiver(mReceiver);
}
private void refreshPairedDevices() {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Toast.makeText(this, "Bluetooth is not supported.", Toast.LENGTH_SHORT).show();
} else if (!btAdapter.isEnabled()) {
Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
} else {
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
if (device.getName().indexOf("Pebble") == 0) {
// Matching device found
deviceList.add(new GBDevice(device.getAddress(), device.getName()));
}
}
}
}
}