| .. | .. |
|---|
| 515 | 515 | switch (state) { |
|---|
| 516 | 516 | case AppLifecycleState.resumed: |
|---|
| 517 | 517 | 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...'); |
|---|
| 526 | 523 | _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(); |
|---|
| 530 | 533 | } |
|---|
| 531 | | - _setStatus(ConnectionStatus.reconnecting); |
|---|
| 532 | | - Future.delayed(const Duration(milliseconds: 300), () { |
|---|
| 533 | | - if (!_intentionalClose) connect(); |
|---|
| 534 | | - }); |
|---|
| 535 | 534 | case AppLifecycleState.paused: |
|---|
| 536 | 535 | break; |
|---|
| 537 | 536 | default: |
|---|