Matthias Nott
2026-03-24 45898e1e305b2278d688cb771deae89e78c8f909
lib/services/mqtt_service.dart
....@@ -515,23 +515,22 @@
515515 switch (state) {
516516 case AppLifecycleState.resumed:
517517 if (_intentionalClose) break;
518
- _mqttLog('MQTT: app resumed, status=$_status');
519
- // iOS kills TCP sockets during suspend. Always force a clean
520
- // reconnect to avoid the "looks connected but dead" state.
521
- final resumeClient = _client;
522
- if (resumeClient != null) {
523
- _intentionalClose = true; // Prevent _onDisconnected from cascading
524
- resumeClient.autoReconnect = false;
525
- resumeClient.disconnect();
518
+ _mqttLog('MQTT: app resumed, status=$_status client=${_client != null} mqttState=${_client?.connectionStatus?.state}');
519
+ final client = _client;
520
+ if (client == null || client.connectionStatus?.state != MqttConnectionState.connected) {
521
+ // Clearly disconnected — just reconnect
522
+ _mqttLog('MQTT: not connected on resume, reconnecting...');
526523 _client = null;
527
- _updatesSub?.cancel();
528
- _updatesSub = null;
529
- _intentionalClose = false;
524
+ _setStatus(ConnectionStatus.reconnecting);
525
+ connect();
526
+ } else {
527
+ // Thinks it's connected — send a sync command to verify.
528
+ // If the connection is dead, the MQTT library will detect it
529
+ // via the next keepalive cycle and auto-reconnect will fire.
530
+ // Also re-request sessions to refresh the drawer.
531
+ _mqttLog('MQTT: appears connected, refreshing sessions...');
532
+ onOpen?.call();
530533 }
531
- _setStatus(ConnectionStatus.reconnecting);
532
- Future.delayed(const Duration(milliseconds: 300), () {
533
- if (!_intentionalClose) connect();
534
- });
535534 case AppLifecycleState.paused:
536535 break;
537536 default: