0

The task: Send "Hello world" from an android device to a MQTT server.

The lib: PahoMqtt 3.1.1

IDE: Android Studio 3.5

Manifest permissions:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

Android's code:

private String topic    = "Lesson_MQTT_server";
private String broker   = "tcp://[host]:[port];
private char [] pass    = "the_password".toCharArray(); // Fake pass
private String user     = "the_user";
private String userId   = "the_user_id";
private String content  = "Hello world from android device!";
private int qos         = 2;
private MqttMessage message;
private MqttConnectOptions options;
private MqttClient client;
private TextView info;
private Button clickButton;



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

    info = (TextView)findViewById(R.id.HelloWorld);
    clickButton = (Button)findViewById(R.id.MyButton);

    clickButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                message = new MqttMessage();
                message.clearPayload();
                message.setPayload(content.getBytes());
                message.setQos(qos);
                message.setRetained(true);

                options = new MqttConnectOptions();
                options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
                options.setAutomaticReconnect(false);
                options.setCleanSession(false);
                options.setUserName(userId);
                options.setPassword(pass);

                client = new MqttClient (broker, user);
                client.connect(options);
                client.publish(topic, message);
                client.disconnect(10);
                client.close();

                info.setText("Check your server:)");

            } catch (MqttException ex){
                info.setText("Ops! Something went wrong :)");
            }
        }
    });

The problem: MqttException caused.

My observations: This code works fine if I use eclipse IDE.

MqttExceptions: CAUSE: null, Reason CODE: 0, Message: MqttException.

Haem
  • 929
  • 6
  • 15
  • 31
Michael
  • 9
  • 4
  • 1
    MqttException caused is way to vague for us to answer on. Can you show us a stacktrace? – Stultuske Oct 16 '19 at 12:56
  • 7
    instead of `info.setText("Ops! Something went wrong :)");` you should print the whole stacktrace if you want to know more about the cause... – assylias Oct 16 '19 at 12:56
  • You should do as assylias recommends and edit your question to paste the stack trace or its relevant elements, if you cannot figure it out right away) - that'll help folks answer you. – Mena Oct 16 '19 at 13:05
  • EDIT the question to add more detail. – hardillb Oct 16 '19 at 13:24
  • 1
    See also [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Haem Oct 16 '19 at 14:00

0 Answers0