diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 6f03dbe1..a70c8ac8 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -1057,6 +1057,43 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph return false } + public func connectToPreferredPeripheral() -> Bool { + + var success = false + // Return false if we are not properly connected to a device, handle retry logic in the view for now + if connectedPeripheral == nil || connectedPeripheral!.peripheral.state != CBPeripheralState.connected { + + self.disconnectPeripheral() + self.startScanning() + + // Try and connect to the preferredPeripherial first + let preferredPeripheral = peripherals.filter({ $0.peripheral.identifier.uuidString == UserDefaults.standard.object(forKey: "preferredPeripheralId") as? String ?? "" }).first + if preferredPeripheral != nil && preferredPeripheral?.peripheral != nil { + connectTo(peripheral: preferredPeripheral!.peripheral) + success = true + } + } else if connectedPeripheral != nil && isSubscribed { + success = true + } + return success + } + + public func saveChannelSet(base64String: String) -> Bool { + + if isConnected { + var decodedString = base64String.base64urlToBase64() + if let decodedData = Data(base64Encoded: decodedString) { + do { + var channelSet: ChannelSet = try ChannelSet(serializedData: decodedData) + print(channelSet) + } catch { + print("Invalid Meshtastic QR Code Link") + } + } + } + return false + } + public func saveUser(config: User, fromUser: UserEntity, toUser: UserEntity) -> Int64 { var adminPacket = AdminMessage() diff --git a/Meshtastic/MeshtasticApp.swift b/Meshtastic/MeshtasticApp.swift index a15663e4..46ea6c3a 100644 --- a/Meshtastic/MeshtasticApp.swift +++ b/Meshtastic/MeshtasticApp.swift @@ -24,12 +24,10 @@ struct MeshtasticAppleApp: App { .environmentObject(bleManager) .environmentObject(userSettings) .sheet(isPresented: $saveChannels) { - - SaveChannelQRCode(channelHash: channelSettings ?? "Empty Channel URL") + SaveChannelQRCode(channelHash: channelSettings ?? "Empty Channel URL", bleManager: bleManager) .presentationDetents([.medium, .large]) .presentationDragIndicator(.visible) } - .onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in print("URL received \(userActivity)") diff --git a/Meshtastic/Views/Settings/Config/DeviceConfig.swift b/Meshtastic/Views/Settings/Config/DeviceConfig.swift index 241f828c..841d6b4b 100644 --- a/Meshtastic/Views/Settings/Config/DeviceConfig.swift +++ b/Meshtastic/Views/Settings/Config/DeviceConfig.swift @@ -16,7 +16,6 @@ struct DeviceConfig: View { @State private var isPresentingNodeDBResetConfirm = false @State private var isPresentingFactoryResetConfirm = false @State private var isPresentingSaveConfirm = false - @State var initialLoad: Bool = true @State var hasChanges = false @State var deviceRole = 0 @@ -166,17 +165,11 @@ struct DeviceConfig: View { ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????") }) .onAppear { - - if self.initialLoad{ - - self.bleManager.context = context - - self.deviceRole = Int(node?.deviceConfig?.role ?? 0) - self.serialEnabled = (node?.deviceConfig?.serialEnabled ?? true) - self.debugLogEnabled = node?.deviceConfig?.debugLogEnabled ?? false - self.hasChanges = false - self.initialLoad = false - } + self.bleManager.context = context + self.deviceRole = Int(node?.deviceConfig?.role ?? 0) + self.serialEnabled = (node?.deviceConfig?.serialEnabled ?? true) + self.debugLogEnabled = node?.deviceConfig?.debugLogEnabled ?? false + self.hasChanges = false } .onChange(of: deviceRole) { newRole in diff --git a/Meshtastic/Views/Settings/SaveChannelQRCode.swift b/Meshtastic/Views/Settings/SaveChannelQRCode.swift index 8d5e3f5c..29c77a19 100644 --- a/Meshtastic/Views/Settings/SaveChannelQRCode.swift +++ b/Meshtastic/Views/Settings/SaveChannelQRCode.swift @@ -8,7 +8,9 @@ import SwiftUI struct SaveChannelQRCode: View { var channelHash: String - + var bleManager: BLEManager + @State var connectedToDevice = false + var body: some View { VStack { Text("Save Channel Settings?") @@ -17,32 +19,23 @@ struct SaveChannelQRCode: View { .foregroundColor(.gray) .font(.callout) .padding() - Text(channelHash) - .font(.caption2) - .foregroundColor(.gray) - .padding() - Button { - - } label: { - - Label("Save", systemImage: "square.and.arrow.down") - } - .buttonStyle(.bordered) - .buttonBorderShape(.capsule) - .controlSize(.large) - .padding() - } - .onChange(of: channelHash) { newSettings in - var decodedString = newSettings.base64urlToBase64() - if let decodedData = Data(base64Encoded: decodedString) { - do { - var channelSet: ChannelSet = try ChannelSet(serializedData: decodedData) - print(channelSet) - } catch { - print("Invalid Meshtastic QR Code Link") + + Button { + let success = bleManager.saveChannelSet(base64String: channelHash) + + } label: { + Label("Save", systemImage: "square.and.arrow.down") } - } + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() + .disabled(!connectedToDevice) + + } + .onAppear { + connectedToDevice = bleManager.connectToPreferredPeripheral() } } }