MQTT on Android: connection lost

2019-04-14 13:22发布

I'm trying to use an MQTT client on Android application but I cannot get it working.
My MQTT client is 0.4.0 and my Android is 4.0.3.

The app is very simple: it has an EditText and a button to publish the text. Further it has a TextView to show the received message.

Every time is click on the "Publish" button, the "connection lost" message is shown, sometimes two or three times, as it tried to connect multiple times and then disconnected.
Also, without clicking on the button, if I try to publish a message on the "123456789" topic via another client nothing happen (no "message arrived" message is shown).

I put both the INTERNET and WRITE_EXTERNAL_STORAGE permissions and the application does not close with an exception, simply no message are sent/received.

This is the MainActivity

package com.example.mqtttest;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private static final String MQTT_DIR = "/mnt/sdcard";
    private static final String MQTT_URI = "tcp://m2m.eclipse.org:1883";
    private MqttClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            MqttDefaultFilePersistence mdfp = new MqttDefaultFilePersistence(
                    MQTT_DIR);
            client = new MqttClient(MQTT_URI, "1", mdfp);

            client.connect();
            client.subscribe("123456789");
            client.setCallback(new MqttCallback() {

                @Override
                public void connectionLost(Throwable arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("Connection lost");
                    try {
                        client.connect();
                    } catch (MqttException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void messageArrived(String arg0, MqttMessage arg1)
                        throws Exception {
                    // TODO Auto-generated method stub
                    TextView txt = (TextView) findViewById(R.id.txt);
                    txt.setText(arg1.toString());
                }
            });

            Button btn = (Button) findViewById(R.id.btn);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    try {
                        MqttMessage msg = new MqttMessage();
                        msg.setPayload((((EditText) findViewById(R.id.editTxt))
                                .getText().toString()).getBytes());
                        client.publish("123456789", msg);
                    } catch (MqttException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        } catch (MqttException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

New info If I use org.eclipse.paho.client.mqttv3.jar the app works fine.
The problem is only with org.eclipse.paho.client.mqttv3-0.9.0.jar

标签: android mqtt
3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-14 13:44

You don't have to change the server , please set the protocol version

Blockquote connOpts.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);

查看更多
Rolldiameter
3楼-- · 2019-04-14 14:02
@Override
            public void messageArrived(String arg0, MqttMessage arg1)
                    throws Exception {
                // TODO Auto-generated method stub
                TextView txt = (TextView) findViewById(R.id.txt);
                txt.setText(arg1.toString());
            }

It's not in MainThread,so setText will throw Exception.Delete it and try again.

查看更多
在下西门庆
4楼-- · 2019-04-14 14:06

Your Broker could be using the version 3.1.1 of the spec.

Paho 1.0 implements MQTT 3.1.1 as well as the existing MQTT 3.1 specification. While Paho 0.9 implemented just only MQTT 3.1 spec.

查看更多
登录 后发表回答