Search code examples
androidmqttmosquittopaho

How to connect mosquitto server to emulator in android studio


Please help me to understand how to connect mosquitto server to app in emulator. Using mosquitto broker in windows. Unable to connect the mosquitto server to emulator using ip address of the system. I could see these two ports in cmd as listening ports.

TCP 127.0.0.1:1883 and TCP [::1]:1883

I have added all dependencies,libraries and services of paho in the app.

String clientId = MqttClient.generateClientId();
                    mqttAndroidClient = new MqttAndroidClient(MainActivity.this, **"tcp://192.168.0.5:1883"**,clientId);
                    mqttAndroidClient.setCallback(new MqttCallback() {
                        @Override
                        public void connectionLost(Throwable cause) {
                            Log.d("MqttConnection", "Connection Lost!");
                        }
            
                        @Override
                        public void messageArrived(String topic, MqttMessage message) throws Exception {
                            Log.d("Message", topic + ":  + " + new String(message.getPayload()));
                        }
            
                        @Override
                        public void deliveryComplete(IMqttDeliveryToken token) {
                            Log.d("Delivery Complete", "Delivery Complete!");
                        }
                    });
            
                    connectButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
                        mqttConnectOptions.setAutomaticReconnect(true);
                        mqttConnectOptions.setCleanSession(false);
                        try {
                            mqttAndroidClient.connect(mqttConnectOptions,null, new IMqttActionListener() {
                                @Override
                                public void onSuccess(IMqttToken asyncActionToken) {
                                    Log.d("Mqtt Connection","Connection Success!");
                                }
                                @Override
                                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                                    Log.d("Mqtt Connection Failure","Failed to connect to: " +serverUri + exception.toString());
                                }
                            });
                        } catch (MqttException e) {
                            e.printStackTrace();
                        }
                    }
                });

Solution

  • 127.0.0.1 and ::1 always point to the machine that is running the code in question, which in the case of the emulator is the emulator NOT the host machine the emulator is running on.

    You can find a list of addresses in the documentation here: https://developer.android.com/studio/run/emulator-networking

    But 10.0.2.2 is the correct address to talk to the emulators host machine.

    Secondly, by default from v2.0.0 onwards mosquitto will only listen to localhost and will not allow connections without a username/password. You need to pass a config file to enable a listener that binds to ask interfaces and allows anonymous connections.

    Make sure you read the v2.0.0 release notes