From b19967f5e4f3804e2bf031c79ad11e6e45f075af Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Wed, 6 Oct 2021 17:51:52 -0700 Subject: [PATCH] Fix messages crash V 1.23 --- Meshtastic Client.xcodeproj/project.pbxproj | 8 ++- MeshtasticClient/Helpers/BLEManager.swift | 57 +++++++++++++++---- .../Helpers/LocalNotificationManager.swift | 8 +++ .../Views/Messages/Messages.swift | 13 ++++- 4 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 MeshtasticClient/Helpers/LocalNotificationManager.swift diff --git a/Meshtastic Client.xcodeproj/project.pbxproj b/Meshtastic Client.xcodeproj/project.pbxproj index 5b1772f4..85023f92 100644 --- a/Meshtastic Client.xcodeproj/project.pbxproj +++ b/Meshtastic Client.xcodeproj/project.pbxproj @@ -26,6 +26,7 @@ DD8EDE9426F97A2B00A5A10B /* SwiftProtobuf in Frameworks */ = {isa = PBXBuildFile; productRef = DD8EDE9326F97A2B00A5A10B /* SwiftProtobuf */; }; DD90860C26F684AF00DC5189 /* BatteryIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD90860B26F684AF00DC5189 /* BatteryIcon.swift */; }; DD90860E26F69BAE00DC5189 /* NodeMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD90860D26F69BAE00DC5189 /* NodeMap.swift */; }; + DD913639270DFF4C00D7ACF3 /* LocalNotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD913638270DFF4C00D7ACF3 /* LocalNotificationManager.swift */; }; DDAF8C5326EB1DF10058C060 /* BLEManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAF8C5226EB1DF10058C060 /* BLEManager.swift */; }; DDAF8C5826ED07FD0058C060 /* mesh.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAF8C5726ED07FD0058C060 /* mesh.pb.swift */; }; DDAF8C5D26ED09490058C060 /* portnums.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAF8C5C26ED09490058C060 /* portnums.pb.swift */; }; @@ -85,6 +86,7 @@ DD90860A26F645B700DC5189 /* MeshtasticClient.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MeshtasticClient.entitlements; sourceTree = ""; }; DD90860B26F684AF00DC5189 /* BatteryIcon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryIcon.swift; sourceTree = ""; }; DD90860D26F69BAE00DC5189 /* NodeMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NodeMap.swift; sourceTree = ""; }; + DD913638270DFF4C00D7ACF3 /* LocalNotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalNotificationManager.swift; sourceTree = ""; }; DDAF8C5226EB1DF10058C060 /* BLEManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BLEManager.swift; sourceTree = ""; }; DDAF8C5726ED07FD0058C060 /* mesh.pb.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = mesh.pb.swift; sourceTree = ""; }; DDAF8C5C26ED09490058C060 /* portnums.pb.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = portnums.pb.swift; sourceTree = ""; }; @@ -314,6 +316,7 @@ DDC2E1A626CEB3400042C5E4 /* LocationHelper.swift */, DDAF8C6D26ED19040058C060 /* Extensions.swift */, DD47E3D126F1210600029299 /* HelperFunctions.swift */, + DD913638270DFF4C00D7ACF3 /* LocalNotificationManager.swift */, ); path = Helpers; sourceTree = ""; @@ -460,6 +463,7 @@ DDC2E1A726CEB3400042C5E4 /* LocationHelper.swift in Sources */, DD47E3D226F1210600029299 /* HelperFunctions.swift in Sources */, DDAF8C5F26ED09B50058C060 /* radioconfig.pb.swift in Sources */, + DD913639270DFF4C00D7ACF3 /* LocalNotificationManager.swift in Sources */, DDAF8C5326EB1DF10058C060 /* BLEManager.swift in Sources */, DD90860E26F69BAE00DC5189 /* NodeMap.swift in Sources */, DD47E3DB26F3901B00029299 /* Channels.swift in Sources */, @@ -655,7 +659,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.22; + MARKETING_VERSION = 1.23; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = NO; @@ -682,7 +686,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.22; + MARKETING_VERSION = 1.23; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = NO; diff --git a/MeshtasticClient/Helpers/BLEManager.swift b/MeshtasticClient/Helpers/BLEManager.swift index 357cba05..6098084e 100644 --- a/MeshtasticClient/Helpers/BLEManager.swift +++ b/MeshtasticClient/Helpers/BLEManager.swift @@ -135,18 +135,23 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph // Send Broadcast Message public func sendMessage(message: String) -> Bool - { var success = true - if connectedPeripheral == nil || connectedPeripheral!.peripheral.state != CBPeripheralState.connected { - success = false - if lastConnectedNode.count > 10 { - connectToDevice(id: lastConnectedNode) - //Thread.sleep(forTimeInterval: 3) - } + { + + var success = true + + // 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.connectedNode == nil { + + if connectedPeripheral != nil && self.connectedNode == nil { + self.disconnectDevice() + // Lets disconnect and then reconnect a second later + } + success = false } else { - let messageModel = MessageModel(messageId: 0, messageTimeStamp: UInt32(Date().timeIntervalSince1970), fromUserId: self.connectedNode.id, toUserId: broadcastNodeId, fromUserLongName: self.connectedNode.user.longName, toUserLongName: "Broadcast", fromUserShortName: self.connectedNode.user.shortName, toUserShortName: "BC", receivedACK: false, messagePayload: message, direction: "OUT") + let messageModel = MessageModel(messageId: 0, messageTimeStamp: UInt32(Date().timeIntervalSince1970), fromUserId: self.connectedNode.num, toUserId: broadcastNodeId, fromUserLongName: self.connectedNode.user.longName, toUserLongName: "Broadcast", fromUserShortName: self.connectedNode.user.shortName, toUserShortName: "BC", receivedACK: false, messagePayload: message, direction: "OUT") let dataType = PortNum.textMessageApp let payloadData: Data = message.data(using: String.Encoding.utf8)! @@ -182,25 +187,45 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph // Disconnect Peripheral Event func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { + // Start a Scan so the disconnected peripheral is moved to the peripherals[] + self.startScanning() if let e = error { print("Central disconnected because \(e)") + let errorCode = (e as NSError).code + + if errorCode == 6 { // The connection has timed out unexpectedly. + // Happens when device is manually reset + + } + else if errorCode == 7 { // The specified device has disconnected from us. + + // Check if the peripheral is still visible and then reconnect + + } // connectToDevice(id: peripheral.identifier.uuidString) } else { print("Central disconnected! (no error)") } if(peripheral.identifier == connectedPeripheral.peripheral.identifier){ + // if connectedPeripheral = nil connectedNode = nil } print("Peripheral disconnected: " + peripheral.name!) - self.startScanning() + } // Discover Services Event func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { + if let e = error { + print("Discover Services error \(e)") + //let errorCode = (e as NSError).code + + } + guard let services = peripheral.services else { return } for service in services @@ -218,9 +243,14 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph // Discover Characteristics Event func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { - guard let characteristics = service.characteristics else { return } + if let e = error { + + print("Discover Characteristics error \(e)") + } + + guard let characteristics = service.characteristics else { return } - for characteristic in characteristics { + for characteristic in characteristics { switch characteristic.uuid { @@ -255,6 +285,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph // Data Read / Update Characteristic Event func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { + if let e = error { + + print("didUpdateValueFor Characteristic error \(e)") + } + switch characteristic.uuid { case FROMNUM_UUID: diff --git a/MeshtasticClient/Helpers/LocalNotificationManager.swift b/MeshtasticClient/Helpers/LocalNotificationManager.swift new file mode 100644 index 00000000..ab4adfd2 --- /dev/null +++ b/MeshtasticClient/Helpers/LocalNotificationManager.swift @@ -0,0 +1,8 @@ +// +// LocalNotificationManager.swift +// MeshtasticClient +// +// Created by Garth Vander Houwen on 10/6/21. +// + +import Foundation diff --git a/MeshtasticClient/Views/Messages/Messages.swift b/MeshtasticClient/Views/Messages/Messages.swift index c59d5538..ada8c0ac 100644 --- a/MeshtasticClient/Views/Messages/Messages.swift +++ b/MeshtasticClient/Views/Messages/Messages.swift @@ -91,11 +91,18 @@ struct Messages: View { typingMessage = "" } else { - let timer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { (timer) in - if bleManager.sendMessage(message: typingMessage) { - typingMessage = "" + if bleManager.lastConnectedNode.count > 10 { + if bleManager.peripherals.contains(where: { $0.id == bleManager.lastConnectedNode }) { + bleManager.connectToDevice(id: bleManager.lastConnectedNode) + let timer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { (timer) in + + if bleManager.sendMessage(message: typingMessage) { + typingMessage = "" + } + } } } + } } ) {