@@ -127,13 +127,13 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
127
127
128
128
func centralManager( _ central: CBCentralManager , didDiscover peripheral: CBPeripheral , advertisementData: [ String : Any ] , rssi RSSI: NSNumber ) {
129
129
let identifier = peripheral. identifier
130
+ print ( " BLE: discovered \( peripheral. name ?? " unknown device " ) " )
130
131
if state. discoveredPeripherals [ identifier] == nil {
131
132
state. discoveredPeripherals [ identifier] = PeripheralMetadata (
132
133
peripheral: peripheral,
133
134
discoveredDate: Date ( ) ,
134
135
connectionState: . discovered
135
136
)
136
- print ( " BLE: discovered \( peripheral. name ?? " unknown device " ) " )
137
137
if let data = advertisementData [ " kCBAdvDataManufacturerData " ] as? Data {
138
138
let data = data. advanced ( by: 2 ) // 2 bytes for manufacturer ID
139
139
print ( " BLE: manufacturer data: \( data. hexEncodedString ( ) ) " )
@@ -149,9 +149,10 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
149
149
print ( " BLE: skip auto-connect for device \( identifier. uuidString) " )
150
150
}
151
151
}
152
-
153
- updateBackendState ( )
154
152
}
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 ( )
155
156
}
156
157
157
158
func centralManager( _ central: CBCentralManager , didConnect peripheral: CBPeripheral ) {
@@ -164,6 +165,13 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
164
165
peripheral. discoverServices ( nil )
165
166
}
166
167
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
+
167
175
func centralManager( _ central: CBCentralManager , didFailToConnect peripheral: CBPeripheral , error: Error ? ) {
168
176
let errorMessage = error? . localizedDescription ?? " unknown error "
169
177
state. discoveredPeripherals [ peripheral. identifier] ? . connectionState = . error
0 commit comments