From 154b7d28bb47ff794f5bba2226da8058f571691b Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Tue, 5 Jul 2016 22:39:05 +0200 Subject: [PATCH] Let discovery activity also display device aliases --- .../activities/DiscoveryActivity.java | 1 + .../gadgetbridge/impl/GBDeviceCandidate.java | 22 ++++++++++++++----- .../gadgetbridge/util/DeviceHelper.java | 17 ++------------ 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java index d77ea7a8..12a7f9e3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java @@ -179,6 +179,7 @@ public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemC } private void handleDeviceFound(BluetoothDevice device, short rssi) { + LOG.debug("found device: " + device.getName() + ", " + device.getAddress()); if (device.getBondState() == BluetoothDevice.BOND_BONDED) { return; // ignore already bonded devices } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceCandidate.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceCandidate.java index 286e5897..7820bc2a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceCandidate.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceCandidate.java @@ -8,6 +8,8 @@ import android.os.Parcelable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -76,14 +78,22 @@ public class GBDeviceCandidate implements Parcelable { } public String getName() { - String name = null; - if (device != null) { - name = device.getName(); + String deviceName = null; + try { + Method method = device.getClass().getMethod("getAliasName"); + if (method != null) { + deviceName = (String) method.invoke(device); + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) { + LOG.info("Could not get device alias for " + deviceName); } - if (name == null || name.length() == 0) { - name = GBApplication.getContext().getString(R.string._unknown_); + if (deviceName == null || deviceName.length() == 0) { + deviceName = device.getName(); } - return name; + if (deviceName == null || deviceName.length() == 0) { + deviceName = "(unknown)"; + } + return deviceName; } public short getRssi() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java index a562e893..21addeea 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java @@ -8,8 +8,6 @@ import android.widget.Toast; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; @@ -18,7 +16,6 @@ import java.util.Set; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; -import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager; import nodomain.freeyourgadget.gadgetbridge.devices.UnknownDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator; @@ -115,22 +112,12 @@ public class DeviceHelper { public GBDevice toSupportedDevice(BluetoothDevice device) { GBDeviceCandidate candidate = new GBDeviceCandidate(device, GBDevice.RSSI_UNKNOWN); - String deviceName = device.getName(); - try { - Method method = device.getClass().getMethod("getAliasName"); - if (method != null) { - deviceName = (String) method.invoke(device); - } - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) { - LOG.info("Could not get device alias for " + deviceName); - } - if (coordinator != null && coordinator.supports(candidate)) { - return new GBDevice(device.getAddress(), deviceName, coordinator.getDeviceType()); + return new GBDevice(device.getAddress(), candidate.getName(), coordinator.getDeviceType()); } for (DeviceCoordinator coordinator : getAllCoordinators()) { if (coordinator.supports(candidate)) { - return new GBDevice(device.getAddress(), deviceName, coordinator.getDeviceType()); + return new GBDevice(device.getAddress(), candidate.getName(), coordinator.getDeviceType()); } } return null;