From b2fef1bb1e005f6811a9f557c79f41a7a606f826 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Sun, 05 Apr 2026 08:29:31 +0200
Subject: [PATCH] fix: disable connectivity listener causing spurious reconnects, rely on MQTT keepalive

---
 lib/services/mqtt_service.dart |   31 ++-----------------------------
 1 files changed, 2 insertions(+), 29 deletions(-)

diff --git a/lib/services/mqtt_service.dart b/lib/services/mqtt_service.dart
index 841182d..7edcbff 100644
--- a/lib/services/mqtt_service.dart
+++ b/lib/services/mqtt_service.dart
@@ -107,35 +107,8 @@
     _intentionalClose = false;
     _setStatus(ConnectionStatus.connecting);
 
-    // Start listening for network changes (WiFi↔cellular, VPN connect/disconnect)
-    _connectivitySub ??= Connectivity().onConnectivityChanged.listen((results) {
-      if (_lastConnectivity != null && !_intentionalClose) {
-        final changed = results.length != _lastConnectivity!.length ||
-            !results.every((r) => _lastConnectivity!.contains(r));
-        if (changed) {
-          _mqttLog('MQTT: network changed: ${results.map((r) => r.name).join(",")} — forcing reconnect');
-          // Force disconnect and reconnect on new network
-          final client = _client;
-          if (client != null) {
-            _intentionalClose = true;
-            client.autoReconnect = false;
-            try { client.disconnect(); } catch (_) {}
-            _client = null;
-            _updatesSub?.cancel();
-            _updatesSub = null;
-            _intentionalClose = false;
-          }
-          _lastDiscoveredHost = null; // Clear cached discovery — subnet may have changed
-          connectedHost = null;
-          connectedVia = null;
-          _setStatus(ConnectionStatus.reconnecting);
-          Future.delayed(const Duration(milliseconds: 500), () {
-            if (!_intentionalClose) connect();
-          });
-        }
-      }
-      _lastConnectivity = results;
-    });
+    // Network change detection disabled — was causing spurious reconnects.
+    // MQTT keepalive + auto-reconnect handles dead connections reliably.
 
     // Load trusted cert fingerprint for TOFU verification
     if (_trustedFingerprint == null) await _loadTrustedFingerprint();

--
Gitblit v1.3.1