Android Question MQTT client is not connected

Philip Prins

Active Member
Licensed User
Longtime User
Hello ,

I get more then 10 fatal errors in 24 hours with 8 different devices,they connect to 2 different servers running mosquitto;


Exception org.eclipse.paho.client.mqttv3.MqttException: Client is not connected
org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException (ExceptionHelper.java:31)
org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait (ClientComms.java:143)
org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish (MqttAsyncClient.java:858)
org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish (MqttAsyncClient.java:822)
org.eclipse.paho.client.mqttv3.MqttAsyncClient.publish (MqttAsyncClient.java:829)
anywheresoftware.b4j.objects.MqttAsyncClientWrapper.Publish2 (MqttAsyncClientWrapper.java:100)

I have tried If Client.connected then etc and i have tried to create a boolean when the MQTT connection is made then true and if client disconnected the boolean is false.

Somehow the client.connected does not work reliable ,

I do not know what else to do to check if the client is connected is valid before publishing a message.
Are there others that have the same problem with the B4A MQTT library?

Regards
Philip
 

DonManfred

Expert
Licensed User
Longtime User
See the example.
Wait for the Connected event is raised.

B4X:
Sub mqtt_Connected (Success As Boolean)
    If Success = False Then
        Log(LastException)   
        lblStatus.Text = "Error connecting"
    Else
        lblStatus.Text = "Connected"
        mqtt.Subscribe("drawers/#", 0) ' or publish or whatever....
    End If
End Sub
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
See the example.
Wait for the Connected event is raised.

B4X:
Sub mqtt_Connected (Success As Boolean)
    If Success = False Then
        Log(LastException)  
        lblStatus.Text = "Error connecting"
    Else
        lblStatus.Text = "Connected"
        mqtt.Subscribe("drawers/#", 0) ' or publish or whatever....
    End If
End Sub

Hello DonManfred,

I Have used this and created a boolean mqtt connected = true and a false when the disconnected event occurs.
Still i get this error on 8 different devices,
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
You should check the state with Client.Connected. It is based on the same code that raises the error. So if Client.Connected returns true then you shouldn't see such error.

Hello Erel,

I get the error occasionly ,before publishing to mqtt i use if cliënt.connected is true then etc.
Some how the cliënt publishes when the cliënt is not connected even though cliënt.connected is true.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are you sure that you don't have multiple client objects?

This is the internal code that raises the error that you see:
B4X:
public void sendNoWait(MqttWireMessage message, MqttToken token) throws MqttException {
     final String methodName = "sendNoWait";
     if (isConnected() ||
         (!isConnected() && message instanceof MqttConnect) ||
         (isDisconnecting() && message instanceof MqttDisconnect)) {
       this.internalSend(message, token);
     } else {
       throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_CLIENT_NOT_CONNECTED); <--------------------------
     }
   }
It calls isConnected() to check whether the client is connected. This is the same method that is called when you check Client.Connected.
My guess is that the problem is somewhere else.
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Are you sure that you don't have multiple client objects?

This is the internal code that raises the error that you see:
B4X:
public void sendNoWait(MqttWireMessage message, MqttToken token) throws MqttException {
     final String methodName = "sendNoWait";
     if (isConnected() ||
         (!isConnected() && message instanceof MqttConnect) ||
         (isDisconnecting() && message instanceof MqttDisconnect)) {
       this.internalSend(message, token);
     } else {
       throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_CLIENT_NOT_CONNECTED); <--------------------------
     }
   }
It calls isConnected() to check whether the client is connected. This is the same method that is called when you check Client.Connected.
My guess is that the problem is somewhere else.

Hello Erel,

I connect to MQTT from a service ,the service is stopped after 2 minutes.
Should i call client.close at service destroy?

Regards,
Philip
 
Upvote 0
Top