Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ios: update device name in frontend #3244

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())")
Expand All @@ -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) {
Expand All @@ -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
Expand Down
Loading