Paho MqttAndroidClient.connect always fails

Farshid T picture Farshid T · Sep 5, 2015 · Viewed 11.7k times · Source

I'd like to publish messages from an android service to a local server. Here is parts of my code in the simplest form based on snippets from here.

MemoryPersistence memPer;
MqttAndroidClient client;

@Override
public IBinder onBind(Intent intent) {
    memPer = new MemoryPersistence();
    client = new MqttAndroidClient(this, "tcp://192.168.1.42:1883", "clientid", memPer);

    try {
        client.connect(null, new IMqttActionListener() {

            @Override
            public void onSuccess(IMqttToken mqttToken) {
                Log.i("MQTT", "Client connected");
                Log.i("MQTT", "Topics=" + mqttToken.getTopics());

                MqttMessage message = new MqttMessage("Hello, I am Android Mqtt Client.".getBytes());
                message.setQos(2);
                message.setRetained(false);

                try {
                    client.publish("messages", message);

                    Log.i("MQTT", "Message published");

                    client.disconnect();
                    Log.i("MQTT", "client disconnected");
                } catch (MqttPersistenceException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (MqttException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(IMqttToken arg0, Throwable arg1) {
                // TODO Auto-generated method stub
                Log.i("MQTT", "Client connection failed: " + arg1.getMessage());
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }

    return mBinder;
}

But the onFailure function is always called and I get the error:

I/MQTT﹕ Client connection failed: cannot start service org.eclipse.paho.android.service.MqttService

Apparently returned by the library because 'listener != null', Line 410. Using the debugger, it shows that 'listener = SensorLoggerService$1@3634'. SensorLoggerService is my service.

Any idea what could be going wrong? Thanks a lot.

Answer

Dario Fiumicello picture Dario Fiumicello · Nov 25, 2015

The same issue for me; in my case, the problem was that the <service> tag was outside the <application> tag.

In the beginning I had this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
...
<service android:name="org.eclipse.paho.android.service.MqttService">
    </service>
...
<application
    android:name="com.mycompany.myapp" ... >
...
</application>

Then I changed to this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
...
<application
    android:name="com.mycompany.myapp" ... >
...
    <service android:name="org.eclipse.paho.android.service.MqttService">
    </service>

</application>

And everything worked!

You need also to add the INTERNET, ACCESS_NETWORK_STATE and WAKE_LOCK permissions.

HTH