From b8c7db1799702ee9729737b3177583a124db8554 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Wed, 2 Nov 2022 06:02:58 -0700 Subject: [PATCH] Stop BLE error from continuing and crashing the app --- Meshtastic/Helpers/BLEManager.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 80af2665..0c13dd73 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -324,8 +324,12 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph // MARK: Discover Characteristics Event func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { + if let e = error { - MeshLogger.log("🚫 BLE didDiscoverCharacteristicsFor error by \(peripheral.name ?? "Unknown") \(e)") + MeshLogger.log("🚫 BLE Discover Characteristics error for \(peripheral.name ?? "Unknown") \(e) disconnecting device") + // Try and stop crashes when this error occurs + disconnectPeripheral() + return } guard let characteristics = service.characteristics else { return } @@ -637,9 +641,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph default: print("🚨 Unhandled Characteristic UUID: \(characteristic.uuid)") } - - // Either Read the config complete value or from num notify value - peripheral.readValue(for: FROMRADIO_characteristic) + if FROMRADIO_characteristic != nil { + + // Either Read the config complete value or from num notify value + peripheral.readValue(for: FROMRADIO_characteristic) + } } public func sendMessage(message: String, toUserNum: Int64, isEmoji: Bool, replyID: Int64) -> Bool {