From fb9ac19b300d157a158ea0a87033e7a52cbb5e5e Mon Sep 17 00:00:00 2001 From: Harri Hohteri Date: Sat, 11 Jul 2026 00:05:24 +0300 Subject: [PATCH] Fix resolving and connecting to devices which have different short and full device names --- .../attach/ble/impl/IOSBleService.java | 46 +++++++++++-------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/modules/ble/src/main/java/com/gluonhq/attach/ble/impl/IOSBleService.java b/modules/ble/src/main/java/com/gluonhq/attach/ble/impl/IOSBleService.java index f4f9859a..c99e86ff 100644 --- a/modules/ble/src/main/java/com/gluonhq/attach/ble/impl/IOSBleService.java +++ b/modules/ble/src/main/java/com/gluonhq/attach/ble/impl/IOSBleService.java @@ -84,7 +84,6 @@ public class IOSBleService implements BleService { private static Consumer callback; private static final ObservableList devices = FXCollections.observableArrayList(); - private static final List deviceNames = new LinkedList<>(); private static final List profileNames = new LinkedList<>(); private static boolean debug; @@ -155,7 +154,6 @@ private static void setDetection(String uuid, int major, int minor, int rssi, in @Override public ObservableList startScanningDevices() { devices.clear(); - deviceNames.clear(); profileNames.clear(); startScanningPeripherals(); return devices; @@ -221,17 +219,19 @@ private static boolean checkDevice(BleDevice device) { if (device == null) { return false; } - if (device.getName() == null) { + String name = device.getName(); + if (name == null) { if (debug) { LOG.log(Level.INFO, "IOSBleService: Device with null name not allowed"); } return false; } - final boolean check = deviceNames.contains(device.getName()); - if (debug) { - LOG.log(Level.INFO, "IOSBleService: Device with name " + device.getName() + " in device list: " + check); + for (BleDevice device2 : devices) { + if (name.equals(device2.getName())) { + return true; + } } - return check; + return false; } // native @@ -245,26 +245,34 @@ private static boolean checkDevice(BleDevice device) { private static native void doSubscribe(String name, String uuidService, String uuidChar, boolean subscribe); private static void gotPeripheral(String name, String uuid) { - if ((name != null && deviceNames.contains(name)) || - (name == null && uuid != null && deviceNames.contains(uuid))) { - return; + if (debug) { + LOG.log(Level.INFO, String.format("IOSBleService got peripheral named %s and uuid: %s", name, uuid)); } - if (name != null && uuid != null && deviceNames.contains(uuid)) { - deviceNames.remove(uuid); - devices.removeIf(d -> uuid.equals(d.getAddress())); + if (uuid == null) return; + + BleDevice existingDevice = null; + for (BleDevice d : devices) { + if (uuid.equals(d.getAddress())) { + existingDevice = d; + break; + } } - if (debug) { - LOG.log(Level.INFO, String.format("IOSBleService got peripheral named %s and uuid: %s", name, uuid)); + if (existingDevice != null) { + String currentName = existingDevice.getName(); + if (name != null && !name.equals(currentName)) { + final BleDevice target = existingDevice; + Platform.runLater(() -> target.setName(name)); + } + return; } + BleDevice dev = new BleDevice(); dev.setName(name); dev.setAddress(uuid); Platform.runLater(() -> devices.add(dev)); - deviceNames.add(name != null ? name : uuid); - } - + private static void gotState(String name, String state) { if (debug) { LOG.log(Level.INFO, String.format("BLE device %s changed state to %s", name, state)); @@ -406,7 +414,7 @@ private static void gotDescriptor(String name, String descName, String data) { } private static Optional getDeviceByName(String name) { - if (name == null || !deviceNames.contains(name)) { + if (name == null) { return Optional.empty(); }