From ada2093e730c9aa3a825b13144ce6d657e82cc28 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sat, 1 Oct 2022 09:37:10 -0700 Subject: [PATCH] Set preferred radio automatically --- Meshtastic/Helpers/BLEManager.swift | 13 +++++++++++-- Meshtastic/Views/Bluetooth/Connect.swift | 8 +++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 1e378eca..c8d6206a 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -252,6 +252,15 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph self.isConnected = true + if userSettings?.preferredPeripheralId.count ?? 0 < 1 { + self.userSettings?.preferredPeripheralId = peripheral.identifier.uuidString + self.preferredPeripheral = true + } else if userSettings!.preferredPeripheralId == peripheral.identifier.uuidString { + self.preferredPeripheral = true + } else { + print("Trying to connect a non prefered peripheral") + } + // Invalidate and reset connection timer count, remove any connection errors self.lastConnectionError = "" self.timeoutTimerCount = 0 @@ -259,7 +268,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph self.timeoutTimer!.invalidate() } - + // Map the peripheral to the connectedPeripheral ObservedObjects connectedPeripheral = peripherals.filter({ $0.peripheral.identifier == peripheral.identifier }).first @@ -731,7 +740,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph // Get all the channels var i: UInt32 = 1; - var max: Int32 = self.connectedPeripheral.maxChannels + let max: Int32 = self.connectedPeripheral.maxChannels Timer.scheduledTimer(withTimeInterval: 0.4, repeats: true) { timer in diff --git a/Meshtastic/Views/Bluetooth/Connect.swift b/Meshtastic/Views/Bluetooth/Connect.swift index a6b5d53f..1a554947 100644 --- a/Meshtastic/Views/Bluetooth/Connect.swift +++ b/Meshtastic/Views/Bluetooth/Connect.swift @@ -79,17 +79,18 @@ struct Connect: View { Text("Preferred").font(.caption2) Text("Radio").font(.caption2) - Toggle("Preferred Radio", isOn: $isPreferredRadio) + Toggle("Preferred Radio", isOn: $bleManager.preferredPeripheral) .toggleStyle(SwitchToggleStyle(tint: .accentColor)) .labelsHidden() - .onChange(of: isPreferredRadio) { value in + .onChange(of: bleManager.preferredPeripheral) { value in if value { if bleManager.connectedPeripheral != nil { userSettings.preferredPeripheralId = bleManager.connectedPeripheral!.peripheral.identifier.uuidString - bleManager.preferredPeripheral = true + bleManager.preferredPeripheral = true + isPreferredRadio = true } @@ -100,6 +101,7 @@ struct Connect: View { userSettings.preferredPeripheralId = "" bleManager.preferredPeripheral = false + isPreferredRadio = false } } }