From c11da9816e6c2699e9ae09cbb1825c019e418a55 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Fri, 22 Oct 2021 20:58:20 -0700 Subject: [PATCH] V 1.26.7 Fix up BLE disconnect timer, update preferred peripheral toggle to only save empty when there is a connected peripheral --- Meshtastic Client.xcodeproj/project.pbxproj | 4 +- MeshtasticClient/Helpers/BLEManager.swift | 58 ++++++++++++------- .../Views/Bluetooth/Connect.swift | 10 ++-- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/Meshtastic Client.xcodeproj/project.pbxproj b/Meshtastic Client.xcodeproj/project.pbxproj index 37f06821..e908738c 100644 --- a/Meshtastic Client.xcodeproj/project.pbxproj +++ b/Meshtastic Client.xcodeproj/project.pbxproj @@ -668,7 +668,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.26.6; + MARKETING_VERSION = 1.26.7; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = YES; @@ -695,7 +695,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.26.6; + MARKETING_VERSION = 1.26.7; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = YES; diff --git a/MeshtasticClient/Helpers/BLEManager.swift b/MeshtasticClient/Helpers/BLEManager.swift index a3dcf5c7..62dceb25 100644 --- a/MeshtasticClient/Helpers/BLEManager.swift +++ b/MeshtasticClient/Helpers/BLEManager.swift @@ -34,6 +34,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph var isDisconnectedByUser = false var timeoutTimer: Timer? + var runCount = 0 private var meshLoggingEnabled: Bool = true @@ -103,30 +104,48 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph /// @objc func timeoutTimerFired(timer: Timer) { - timer.invalidate() - //lastConnectionError = "BLE Connection timed out" //radio \(connectedPeripheral.peripheral.name ?? "Unknown") - if connectedPeripheral != nil { - self.centralManager?.cancelPeripheralConnection(connectedPeripheral.peripheral) - connectedNode = nil - connectedPeripheral = nil + guard let context = timer.userInfo as? [String: String] else { return } + let name = context["name", default: "Unknown"] + + runCount += 1 + + if runCount == 5 { + + timeoutTimer?.invalidate() + runCount = 0 + if connectedPeripheral != nil { + + self.centralManager?.cancelPeripheralConnection(connectedPeripheral.peripheral) + connectedNode = nil + connectedPeripheral = nil + + } + print("BLE Timeout Timer Fired \(runCount) Time(s) Connection Failed: \(name)") + Logger.log("BLE Timeout Timer Fired \(runCount) Time(s) Connection Failed: \(name)") + } + else { + print("BLE Timeout Timer Fired \(runCount) Time(s): \(name)") + Logger.log("BLE Timeout Timer Fired \(runCount) Time(s): \(name)") } - print("BLE-Timeout-Timer fired!") - Logger.log("BLE-Timeout-Timer fired!") self.startScanning() } // Connect to a specific peripheral func connectTo(peripheral: CBPeripheral) { + + if meshLoggingEnabled { Logger.log("BLE Connecting: \(peripheral.name ?? "Unknown")") } + print("BLE Connecting: \(peripheral.name ?? "Unknown")") stopScanning() - if self.connectedPeripheral != nil && self.connectedPeripheral.peripheral.state == CBPeripheralState.connected { + + if self.connectedPeripheral != nil { self.disconnectDevice() } - + self.centralManager?.connect(peripheral) - self.timeoutTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(timeoutTimerFired), userInfo: nil, repeats: false) - if meshLoggingEnabled { Logger.log("BLE Connecting: \(peripheral.name ?? "Unknown")") } - print("BLE Connecting: \(peripheral.name ?? "Unknown")") + + let context = ["name": "@\(peripheral.name ?? "Unknown")"] + self.timeoutTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timeoutTimerFired), userInfo: context, repeats: true) } // Disconnect Device function @@ -196,16 +215,12 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph // Error Code 6: The connection has timed out unexpectedly. // Happens when device is manually reset / powered off - lastConnectionError = "\(e.localizedDescription) The app will automatically reconnect to the preferred radio if it reappears within 3 seconds." + lastConnectionError = "\(e.localizedDescription) The app will automatically reconnect to the preferred radio if it reappears within 5 seconds." self.connectedNode = nil self.connectedPeripheral = nil + if meshLoggingEnabled { Logger.log("BLE Reconnecting: \(peripheral.name ?? "Unknown")" ) } + print("Reconnecting to \(peripheral.name ?? "Unknown")") self.connectTo(peripheral: peripheral) - - // 2 second delay for device to power back on - //let _ = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { (timer) in - - - // } } else if errorCode == 7 { // The specified device has disconnected from us. @@ -249,8 +264,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph for service in services { - - + if (service.uuid == meshtasticServiceCBUUID) { print("Meshtastic service discovered OK") diff --git a/MeshtasticClient/Views/Bluetooth/Connect.swift b/MeshtasticClient/Views/Bluetooth/Connect.swift index a984ff80..bb0960a4 100644 --- a/MeshtasticClient/Views/Bluetooth/Connect.swift +++ b/MeshtasticClient/Views/Bluetooth/Connect.swift @@ -21,7 +21,6 @@ struct Connect: View { @EnvironmentObject var userSettings: UserSettings @State var isPreferredRadio: Bool = false - var body: some View { @@ -35,7 +34,7 @@ struct Connect: View { Section(header: Text("Connection Error").font(.title)) { - Text(bleManager.lastConnectionError).font(.subheadline).foregroundColor(.red) + Text(bleManager.lastConnectionError).font(.headline).foregroundColor(.red) } .textCase(nil) } @@ -87,8 +86,11 @@ struct Connect: View { userSettings.preferredPeripheralId = bleManager.connectedPeripheral!.peripheral.identifier.uuidString } else { - userSettings.preferredPeripheralId = "" - userSettings.preferredPeripheralName = "" + + if bleManager.connectedNode != nil { + userSettings.preferredPeripheralId = "" + userSettings.preferredPeripheralName = "" + } } } }