'MQTT android studio connection failed

I am trying to create an android application to send mqtt messages to a broker mosquitto.

Here's what I did:

build.gradle app :

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.2'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

build.gradle project :

allprojects {
repositories {
    google()
    jcenter()
    maven {
        url "https://repo.eclipse.org/content/repositories/paho-releases/"
    }
}

}

manifest.xml :

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

</activity>
    <service android:name="org.eclipse.paho.android.service.MqttService" />
</application>

main activity :

String clientId = MqttClient.generateClientId();
    final MqttAndroidClient client = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.1.97:1883",clientId);

    try {
        IMqttToken token = client.connect();
        token.setActionCallback(new IMqttActionListener() {


            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                Log.d(TAG, "onSuccess");
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
                Log.d(TAG, "onFailure");
                exception.printStackTrace();
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }

I receive "On failure"



Solution 1:[1]

here is the error return by exception.printStackTrace();

error log

Solution 2:[2]

Please try this repo https://github.com/hannesa2/paho.mqtt.android It's a fork with almost all pull request from non-maintained upstream repo

dependencies {
  implementation 'androidx.legacy:legacy-support-v4:1.0.0'
  implementation 'com.github.hannesa2:paho.mqtt.android:3.3.5'
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mmmhh
Solution 2 hannes ach