[Solved] Flutter MQTT Disconnect Immediately After Connecting

Dependency Used: mqtt_client

Error: disconnecting automatically right after the connection is established

Log:

I/flutter (24905): 2-2025-01-29 07:31:40.215640 -- Authenticating with username '{diyusthad}' and password '{raspberry}'
I/flutter (24905): 2-2025-01-29 07:31:40.219568 -- MqttClient::connect - Connection timeout period is 5000 milliseconds
I/flutter (24905): 2-2025-01-29 07:31:40.220388 -- MqttClient::connect - keep alive is enabled with a value of 20 seconds
I/flutter (24905): 2-2025-01-29 07:31:40.220833 -- MqttConnectionKeepAlive:: Initialised with a keep alive value of 20 seconds
I/flutter (24905): 2-2025-01-29 07:31:40.221026 -- MqttConnectionKeepAlive:: Disconnect on no ping response is disabled
I/flutter (24905): 2-2025-01-29 07:31:40.221286 -- MqttConnectionHandlerBase::connect - server 192.168.1.3, port 1883
I/flutter (24905): 2-2025-01-29 07:31:40.221490 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (24905): 2-2025-01-29 07:31:40.221640 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false
I/flutter (24905): 2-2025-01-29 07:31:40.221790 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (24905): 2-2025-01-29 07:31:40.221956 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect
I/flutter (24905): 2-2025-01-29 07:31:40.222095 -- MqttNormalConnection::connect - entered
I/flutter (24905): 2-2025-01-29 07:31:42.540151 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter (24905): MqttConnectionState.faulted

Solution: Change the client.setProtocolV311() to client.setProtocolV31() fixes the problem

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top