Android Studio - MQTT not connecting

后端 未结 4 1183
不思量自难忘°
不思量自难忘° 2020-12-10 18:26

I have just started learning using MQTT protocol with Android Studio. Using mosquitto broker, I am able to exchange messages between pub/sub

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 18:54

    Ok so you need two libraries to use MQTT in Android. One is the mqtt paho client and other being Android service library.

    compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'
    compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
    

    Then, use MqttAndroidClient instead of MqttClient.

    So do new MqttAndroidClient(...).

    I posted a full Android MQTT service example here, if that helps.

    EDIT: full activity example

    (1) MemoryPersistence added when creating new MqttAndroidClient.
    (2) Two parameters added to .connect() method of the MqttAndroidClient (mqttConnectOptions and null).
    (3) Also, printing the error on onFailure()

    public class HomeActivity extends AppCompatActivity {
    
        private MqttAndroidClient client;
        private final MemoryPersistence persistence = new MemoryPersistence();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final MqttAndroidClient mqttAndroidClient = new MqttAndroidClient(this.getApplicationContext(), "tcp://localhost:1883", "androidSampleClient", persistence);
            mqttAndroidClient.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable cause) {
                    System.out.println("Connection was lost!");
                }
    
                @Override
                public void messageArrived(String topic, MqttMessage message) throws Exception {
                    System.out.println("Message Arrived!: " + topic + ": " + new String(message.getPayload()));
                }
    
                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                    System.out.println("Delivery Complete!");
                }
            });
    
            MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
            mqttConnectOptions.setCleanSession(true);
    
            try {
                mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken asyncActionToken) {
                        System.out.println("Connection Success!");
                        try {
                            System.out.println("Subscribing to /test");
                            mqttAndroidClient.subscribe("/test", 0);
                            System.out.println("Subscribed to /test");
                            System.out.println("Publishing message..");
                            mqttAndroidClient.publish("/test", new MqttMessage("Hello world testing..!".getBytes()));
                        } catch (MqttException ex) {
    
                        }
                    }
    
                    @Override
                    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                        System.out.println("Connection Failure!");
                        System.out.println("throwable: " + exception.toString());
                    }
                });
            } catch (MqttException ex) {
                System.out.println(ex.toString());
            }
        }
    }
    

提交回复
热议问题