From 3c1a99caf455a4e84ced5f673c591d883bd1a716 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 30 Aug 2022 07:46:46 -0500 Subject: [PATCH 1/2] Fixed NRF first pair issue --- Meshtastic/Helpers/BLEManager.swift | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index b7b3a602..da83909a 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -324,7 +324,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph if meshLoggingEnabled { MeshLogger.log("✅ BLE Service for Meshtastic discovered by \(peripheral.name ?? "Unknown")") } //peripheral.discoverCharacteristics(nil, for: service) peripheral.discoverCharacteristics([TORADIO_UUID, FROMRADIO_UUID, FROMNUM_UUID], for: service) - + } else if (service.uuid == DFUSERVICE_UUID) { print("✅ Meshtastic DFU service discovered OK") @@ -357,12 +357,6 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph if meshLoggingEnabled { MeshLogger.log("✅ BLE did discover TORADIO characteristic for Meshtastic by \(peripheral.name ?? "Unknown")") } TORADIO_characteristic = characteristic - var toRadio: ToRadio = ToRadio() - configNonce += 1 - toRadio.wantConfigID = configNonce - - let binaryData: Data = try! toRadio.serializedData() - peripheral.writeValue(binaryData, for: characteristic, type: .withResponse) case FROMRADIO_UUID: @@ -410,7 +404,25 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph break } } + if (![FROMNUM_characteristic, FROMNUM_characteristic, TORADIO_characteristic].contains(nil)) { + sendWantConfig() + } } + + @objc func sendWantConfig() { + guard (connectedPeripheral!.peripheral.state == CBPeripheralState.connected) else { return } + + MeshLogger.log("ℹ️ Issuing wantConfig to \(connectedPeripheral!.peripheral.name ?? "Unknown")") + //BLE Characteristics discovered, issue wantConfig + var toRadio: ToRadio = ToRadio() + configNonce += 1 + toRadio.wantConfigID = configNonce + let binaryData: Data = try! toRadio.serializedData() + connectedPeripheral!.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) + + // Either Read the config complete value or from num notify value + connectedPeripheral!.peripheral.readValue(for: FROMRADIO_characteristic) + } func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) { From 170ea193eaed8c876939984e1ed565fa2e5a82a3 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 30 Aug 2022 07:57:15 -0500 Subject: [PATCH 2/2] Removed objective c exposition --- Meshtastic/Helpers/BLEManager.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index da83909a..6cd93f9b 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -409,7 +409,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } } - @objc func sendWantConfig() { + func sendWantConfig() { guard (connectedPeripheral!.peripheral.state == CBPeripheralState.connected) else { return } MeshLogger.log("ℹ️ Issuing wantConfig to \(connectedPeripheral!.peripheral.name ?? "Unknown")")