Use want config timer to prevent disconnect while config is running, stop force disconnecting.

This commit is contained in:
Garth Vander Houwen 2025-06-17 17:14:45 -07:00
parent db0119bed9
commit aafa7b7b31
2 changed files with 7 additions and 9 deletions

View file

@ -32,6 +32,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
public var isConnecting: Bool = false
public var isConnected: Bool = false
public var isSubscribed: Bool = false
public var allowDisconnect: Bool = false
private var configNonce: UInt32 = 1
var timeoutTimer: Timer?
var timeoutTimerCount = 0
@ -172,6 +173,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
isConnecting = false
isConnected = false
isSubscribed = false
allowDisconnect = false
self.connectedPeripheral = nil
invalidVersion = false
connectedVersion = "0.0.0"
@ -204,6 +206,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
self.FROMRADIO_characteristic = nil
self.isConnected = false
self.isSubscribed = false
self.allowDisconnect = false
self.invalidVersion = false
self.connectedVersion = "0.0.0"
self.stopScanning()
@ -563,7 +566,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
sendWantConfig()
} else {
Logger.mesh.error("🚨 Want Config failed after \(self.maxWantConfigRetries) attempts, forcing disconnect")
forceDisconnect()
lastConnectionError = "Bluetooth connection timeout, keep your node closer or reboot your radio if the problem continues.".localized
}
}
@ -576,12 +579,6 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
}
}
private func forceDisconnect() {
disconnectPeripheral(reconnect: false)
lastConnectionError = "Bluetooth connection timeout, keep your node closer or reboot your radio if the problem continues.".localized
Logger.mesh.error("💥 [BLE] Forced disconnect due to Want Config timeout")
}
// Call this to reset the retry mechanism (e.g., on new connection)
func resetWantConfigRetries() {
wantConfigRetryCount = 0
@ -1059,6 +1056,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate
invalidVersion = false
lastConnectionError = ""
isSubscribed = true
allowDisconnect = true
Logger.mesh.info("🤜 [BLE] Want Config Complete. ID:\(decodedInfo.configCompleteID, privacy: .public)")
if sendTime() {
}

View file

@ -90,7 +90,7 @@ struct Connect: View {
.foregroundColor(Color.gray)
.padding([.top])
.swipeActions {
if bleManager.isSubscribed {
if bleManager.allowDisconnect {
Button(role: .destructive) {
if let connectedPeripheral = bleManager.connectedPeripheral,
connectedPeripheral.peripheral.state == .connected {
@ -127,7 +127,7 @@ struct Connect: View {
Text("Short Name: \(node?.user?.shortName ?? "?")")
Text("Long Name: \(node?.user?.longName?.addingVariationSelectors ?? "Unknown".localized)")
Text("BLE RSSI: \(connectedPeripheral.rssi)")
if bleManager.isSubscribed {
if bleManager.allowDisconnect {
Button(role: .destructive) {
if let connectedPeripheral = bleManager.connectedPeripheral,
connectedPeripheral.peripheral.state == .connected {