From 79c7085264da83d4f6d2488e51f0dacd0a722c06 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sun, 22 Mar 2015 12:46:28 +0100 Subject: [PATCH] refresh list of paried devices when bluetooth gets turned on --- .../BluetoothCommunicationService.java | 20 +++---- .../BluetoothStateChangeReceiver.java | 4 ++ .../gadgetbridge/ControlCenter.java | 56 +++++++++++-------- 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index 1a620c50b..a8f313489 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -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; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothStateChangeReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothStateChangeReceiver.java index d82d0bd4d..693c689ae 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothStateChangeReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothStateChangeReceiver.java @@ -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; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java index 8cf0c00c4..747ac0a1c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java @@ -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 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 pairedDevices = btAdapter.getBondedDevices(); + for (BluetoothDevice device : pairedDevices) { + if (device.getName().indexOf("Pebble") == 0) { + // Matching device found + deviceList.add(new GBDevice(device.getAddress(), device.getName())); + } + } + } + } + }