From 7bdea75856742753eae0df116f57d4d2334b9c37 Mon Sep 17 00:00:00 2001 From: Marko Bencun Date: Wed, 19 Mar 2025 11:33:42 +0100 Subject: [PATCH] ios: update device name in frontend The name can be updated either the discovery method being called again for the same device, or via the peripheralDidUpdateName method. --- frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift b/frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift index 28c88083ef..84cb66925a 100644 --- a/frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift +++ b/frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift @@ -127,13 +127,13 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) { let identifier = peripheral.identifier + print("BLE: discovered \(peripheral.name ?? "unknown device")") if state.discoveredPeripherals[identifier] == nil { state.discoveredPeripherals[identifier] = PeripheralMetadata( peripheral: peripheral, discoveredDate: Date(), connectionState: .discovered ) - print("BLE: discovered \(peripheral.name ?? "unknown device")") if let data = advertisementData["kCBAdvDataManufacturerData"] as? Data { let data = data.advanced(by: 2) // 2 bytes for manufacturer ID print("BLE: manufacturer data: \(data.hexEncodedString())") @@ -149,9 +149,10 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB print("BLE: skip auto-connect for device \(identifier.uuidString)") } } - - updateBackendState() } + + // We update the state for the frontend even if we had already registered the device as the name may have been updated. + updateBackendState() } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { @@ -164,6 +165,13 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB peripheral.discoverServices(nil) } + func peripheralDidUpdateName(_ peripheral: CBPeripheral) { + print("BLE: didUpdateName, new name: \(peripheral.name ?? "unknown device")") + // The peripheral is already in our state, so we just update the frontend to show + // the new name. + updateBackendState() + } + func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { let errorMessage = error?.localizedDescription ?? "unknown error" state.discoveredPeripherals[peripheral.identifier]?.connectionState = .error