From ffd9beee0579660964200088e1bba8639822ef90 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 13 Feb 2022 21:49:58 -0800 Subject: [PATCH] Simplify connected peripheral code, try an fix the bugs when connecting multiple nodes --- MeshtasticClient/Helpers/BLEManager.swift | 51 ++++++++----------- .../CoreDataSample.xcdatamodel/contents | 6 ++- .../Views/Bluetooth/Connect.swift | 5 +- .../Views/Messages/UserMessageList.swift | 2 +- 4 files changed, 26 insertions(+), 38 deletions(-) diff --git a/MeshtasticClient/Helpers/BLEManager.swift b/MeshtasticClient/Helpers/BLEManager.swift index 65de5bcc..77500b03 100644 --- a/MeshtasticClient/Helpers/BLEManager.swift +++ b/MeshtasticClient/Helpers/BLEManager.swift @@ -206,27 +206,6 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph connectedPeripheral = peripherals.filter({ $0.peripheral.identifier == peripheral.identifier }).first connectedPeripheral.peripheral.delegate = self - let fetchConnectedPeripheralRequest: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") - fetchConnectedPeripheralRequest.predicate = NSPredicate(format: "bleName MATCHES %@", String(peripheral.name ?? "???")) - - do { - let fetchedNode = try context?.fetch(fetchConnectedPeripheralRequest) as! [NodeInfoEntity] - - if fetchedNode.count == 1 { - - connectedPeripheral.num = fetchedNode[0].user!.num - connectedPeripheral.shortName = fetchedNode[0].user!.shortName! - connectedPeripheral.longName = fetchedNode[0].user!.longName! - connectedPeripheral.firmwareVersion = (fetchedNode[0].myInfo?.firmwareVersion ?? "Unknown") - } - - } catch { - print("💥 Fetch NodeInfo Failed") - if meshLoggingEnabled { MeshLogger.log("💥 Fetch NodeInfo Failed") } - } - - //lastConnectedPeripheral = peripheral.identifier.uuidString - // Discover Services peripheral.discoverServices([meshtasticServiceCBUUID]) if meshLoggingEnabled { MeshLogger.log("✅ BLE Connected: \(peripheral.name ?? "Unknown")") } @@ -429,6 +408,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph let myInfo = MyInfoEntity(context: context!) myInfo.myNodeNum = Int64(decodedInfo.myInfo.myNodeNum) myInfo.hasGps = decodedInfo.myInfo.hasGps_p + myInfo.channelUtilization = decodedInfo.myInfo.channelUtilization myInfo.numBands = Int32(bitPattern: decodedInfo.myInfo.numBands) // Swift does strings weird, this does work @@ -441,8 +421,9 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph myInfo.messageTimeoutMsec = Int32(bitPattern: decodedInfo.myInfo.messageTimeoutMsec) myInfo.minAppVersion = Int32(bitPattern: decodedInfo.myInfo.minAppVersion) myInfo.maxChannels = Int32(bitPattern: decodedInfo.myInfo.maxChannels) - connectedPeripheral.num = myInfo.myNodeNum - connectedPeripheral.firmwareVersion = myInfo.firmwareVersion ?? "Unknown" + self.connectedPeripheral.num = myInfo.myNodeNum + self.connectedPeripheral.firmwareVersion = myInfo.firmwareVersion ?? "Unknown" + self.connectedPeripheral.name = myInfo.bleName ?? "Unknown" let fetchBCUserRequest: NSFetchRequest = NSFetchRequest.init(entityName: "UserEntity") fetchBCUserRequest.predicate = NSPredicate(format: "num == %lld", Int64(decodedInfo.myInfo.myNodeNum)) @@ -470,6 +451,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph fetchedMyInfo[0].myNodeNum = Int64(decodedInfo.myInfo.myNodeNum) fetchedMyInfo[0].hasGps = decodedInfo.myInfo.hasGps_p + fetchedMyInfo[0].channelUtilization = decodedInfo.myInfo.channelUtilization fetchedMyInfo[0].numBands = Int32(bitPattern: decodedInfo.myInfo.numBands) let lastDotIndex = decodedInfo.myInfo.firmwareVersion.lastIndex(of: ".")//.lastIndex(of: ".", offsetBy: -1) var version = decodedInfo.myInfo.firmwareVersion[...(lastDotIndex ?? String.Index(utf16Offset:6, in: decodedInfo.myInfo.firmwareVersion))] @@ -479,6 +461,10 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph fetchedMyInfo[0].messageTimeoutMsec = Int32(bitPattern: decodedInfo.myInfo.messageTimeoutMsec) fetchedMyInfo[0].minAppVersion = Int32(bitPattern: decodedInfo.myInfo.minAppVersion) fetchedMyInfo[0].maxChannels = Int32(bitPattern: decodedInfo.myInfo.maxChannels) + connectedPeripheral.num = fetchedMyInfo[0].myNodeNum + connectedPeripheral.firmwareVersion = fetchedMyInfo[0].firmwareVersion ?? "Unknown" + connectedPeripheral.name = fetchedMyInfo[0].bleName ?? "Unknown" + } do { @@ -523,15 +509,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } newNode.snr = decodedInfo.nodeInfo.snr - if self.connectedPeripheral != nil && self.connectedPeripheral.num == newNode.id { + if self.connectedPeripheral != nil && self.connectedPeripheral.num == newNode.num { - newNode.bleName = self.connectedPeripheral.peripheral.name if decodedInfo.nodeInfo.hasUser { - + connectedPeripheral.name = decodedInfo.nodeInfo.user.longName - connectedPeripheral.longName = decodedInfo.nodeInfo.user.longName - connectedPeripheral.shortName = decodedInfo.nodeInfo.user.shortName - connectedPeripheral.num = Int64(decodedInfo.nodeInfo.num) } } @@ -589,8 +571,15 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph fetchedNode[0].lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(decodedInfo.nodeInfo.lastHeard))) } - //fetchedNode[0].lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(decodedInfo.nodeInfo.lastHeard))) fetchedNode[0].snr = decodedInfo.nodeInfo.snr + + if self.connectedPeripheral != nil && self.connectedPeripheral.num == fetchedNode[0].num { + + if decodedInfo.nodeInfo.hasUser { + + self.connectedPeripheral.name = fetchedNode[0].user!.longName ?? "Unknown" + } + } if decodedInfo.nodeInfo.hasUser { @@ -882,7 +871,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } else if decodedInfo.packet.decoded.portnum == PortNum.routingApp { - let currentNodeNum = connectedPeripheral.num + let currentNodeNum = self.connectedPeripheral.num diff --git a/MeshtasticClient/Meshtastic.xcdatamodeld/CoreDataSample.xcdatamodel/contents b/MeshtasticClient/Meshtastic.xcdatamodeld/CoreDataSample.xcdatamodel/contents index 75fc2789..8f7d888b 100644 --- a/MeshtasticClient/Meshtastic.xcdatamodeld/CoreDataSample.xcdatamodel/contents +++ b/MeshtasticClient/Meshtastic.xcdatamodeld/CoreDataSample.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -20,6 +20,8 @@ + + @@ -74,7 +76,7 @@ - + diff --git a/MeshtasticClient/Views/Bluetooth/Connect.swift b/MeshtasticClient/Views/Bluetooth/Connect.swift index e08695d5..7bfba712 100644 --- a/MeshtasticClient/Views/Bluetooth/Connect.swift +++ b/MeshtasticClient/Views/Bluetooth/Connect.swift @@ -67,11 +67,8 @@ struct Connect: View { if bleManager.connectedPeripheral != nil { - Text(bleManager.connectedPeripheral.longName).font(.title2) + Text(bleManager.connectedPeripheral.name).font(.title2) - } else { - - Text(String(bleManager.connectedPeripheral.peripheral.name ?? "Unknown")).font(.title2) } Text("BLE Name: ").font(.caption)+Text(bleManager.connectedPeripheral.peripheral.name ?? "Unknown") .font(.caption).foregroundColor(Color.gray) diff --git a/MeshtasticClient/Views/Messages/UserMessageList.swift b/MeshtasticClient/Views/Messages/UserMessageList.swift index b794d2e6..c97fcc99 100644 --- a/MeshtasticClient/Views/Messages/UserMessageList.swift +++ b/MeshtasticClient/Views/Messages/UserMessageList.swift @@ -34,7 +34,7 @@ struct UserMessageList: View { var body: some View { let firmwareVersion = bleManager.lastConnnectionVersion - let minimumVersion = "1.2.54" + let minimumVersion = "1.2.60" let hasTapbackSupport = minimumVersion.compare(firmwareVersion, options: .numeric) == .orderedAscending || minimumVersion.compare(firmwareVersion, options: .numeric) == .orderedSame VStack {