Matthias Nott
9 days ago 8b82df83d3773bcc8fafc5e925ef2ca05e5c9472
lib/services/mqtt_service.dart
....@@ -440,9 +440,7 @@
440440 );
441441 _mqttLog('MQTT: connect result=${result?.state}');
442442 if (result?.state == MqttConnectionState.connected) {
443
- // Don't enable autoReconnect — we handle reconnection ourselves
444
- // in didChangeAppLifecycleState(resumed). The library's autoReconnect
445
- // competes with our force-reconnect and causes connection flickering.
443
+ client.autoReconnect = true;
446444 return true;
447445 }
448446 _client = null;
....@@ -800,24 +798,10 @@
800798 switch (state) {
801799 case AppLifecycleState.resumed:
802800 if (_intentionalClose) break;
803
- _mqttLog('MQTT: app resumed — reconnecting to last host');
804
- // Kill old client completely (disable autoReconnect first to prevent
805
- // the MQTT library from spawning its own reconnect attempt)
806
- final oldClient = _client;
807
- if (oldClient != null) {
808
- oldClient.autoReconnect = false;
809
- try { oldClient.disconnect(); } catch (_) {}
810
- }
811
- _updatesSub?.cancel();
812
- _updatesSub = null;
813
- _client = null;
814
- _setStatus(ConnectionStatus.reconnecting);
815
- onReconnecting?.call();
816
- if (connectedHost != null) {
817
- _fastReconnect(connectedHost!);
818
- } else {
819
- connect();
820
- }
801
+ _mqttLog('MQTT: app resumed');
802
+ // Let autoReconnect handle dead connections (keepalive timeout).
803
+ // Just trigger catch_up to fetch missed messages and rebuild UI.
804
+ onResume?.call();
821805 case AppLifecycleState.paused:
822806 break;
823807 default: