Skip to content

Commit 7bdea75

Browse files
committed
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.
1 parent c6d52e8 commit 7bdea75

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift

+11-3
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
127127

128128
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
129129
let identifier = peripheral.identifier
130+
print("BLE: discovered \(peripheral.name ?? "unknown device")")
130131
if state.discoveredPeripherals[identifier] == nil {
131132
state.discoveredPeripherals[identifier] = PeripheralMetadata(
132133
peripheral: peripheral,
133134
discoveredDate: Date(),
134135
connectionState: .discovered
135136
)
136-
print("BLE: discovered \(peripheral.name ?? "unknown device")")
137137
if let data = advertisementData["kCBAdvDataManufacturerData"] as? Data {
138138
let data = data.advanced(by: 2) // 2 bytes for manufacturer ID
139139
print("BLE: manufacturer data: \(data.hexEncodedString())")
@@ -149,9 +149,10 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
149149
print("BLE: skip auto-connect for device \(identifier.uuidString)")
150150
}
151151
}
152-
153-
updateBackendState()
154152
}
153+
154+
// We update the state for the frontend even if we had already registered the device as the name may have been updated.
155+
updateBackendState()
155156
}
156157

157158
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
@@ -164,6 +165,13 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
164165
peripheral.discoverServices(nil)
165166
}
166167

168+
func peripheralDidUpdateName(_ peripheral: CBPeripheral) {
169+
print("BLE: didUpdateName, new name: \(peripheral.name ?? "unknown device")")
170+
// The peripheral is already in our state, so we just update the frontend to show
171+
// the new name.
172+
updateBackendState()
173+
}
174+
167175
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
168176
let errorMessage = error?.localizedDescription ?? "unknown error"
169177
state.discoveredPeripherals[peripheral.identifier]?.connectionState = .error

0 commit comments

Comments
 (0)