0

In my Android Studio, I would like to connect the Mqtt Android client to my laptop host (in the same machine). I make it similar to this guide

https://www.hivemq.com/blog/mqtt-client-library-enyclopedia-paho-android-service/

Then, I found that the Android 12 (API 32 in my case) may not support the org.eclipse.paho:org.eclipse.paho.android.service:1.1.1 dependency. So, I followed this solution below, by imported the serviceLibrary-release.aar library from github provided instead. (The problem appear in this link was the same of my case)

Android paho mqtt crashes Android 12 - Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE

After that, I ran into another error.

error: constructor MqttAndroidClient in class MqttAndroidClient cannot be applied to given types;
                MqttAndroidClient client = new MqttAndroidClient(MainActivity.this, "tcp://10.0.2.2:1883", clientId);
                                           ^
  required: Context,String,String,Ack
  found: MainActivity,String,String
  reason: actual and formal argument lists differ in length

So I'm not sure that the library from the solution above can be applied to my old code, or, do I need to modify some code?

Here is my code and the gradle file.

repositories

maven {
    url "https://repo.eclipse.org/content/repositories/paho-releases/"
}

Dependencies

implementation files('libs/serviceLibrary-release.aar')
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'

Android Manifest (Added permission below)

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<service android:name="info.mqtt.android.service.MqttService"/>

Main Activity

import info.mqtt.android.service.MqttAndroidClient;
public class MainActivity extends AppCompatActivity {

    private Button buttonConnect;

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

        buttonConnect = findViewById(R.id.buttonConnect);
        buttonConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String clientId = MqttClient.generateClientId();
                Toast.makeText(MainActivity.this, clientId, Toast.LENGTH_SHORT).show();

                MqttAndroidClient client = new MqttAndroidClient(MainActivity.this, "tcp://10.0.2.2:1883", clientId);

                try {
                    IMqttToken token = client.connect();
                    token.setActionCallback(new IMqttActionListener() {
                        @Override
                        public void onSuccess(IMqttToken asyncActionToken) {
                            Log.d("Debug", "onSuccess");
                            Toast.makeText(MainActivity.this, "onSuccess", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                            Log.d("Debug", "onFailure");
                            Toast.makeText(MainActivity.this, "onFailure", Toast.LENGTH_SHORT).show();
                            exception.printStackTrace();
                        }
                    });
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

The error appear in this line (when the button is clicked)

MqttAndroidClient client = new MqttAndroidClient(MainActivity.this, "tcp://10.0.2.2:1883", clientId);

From the error message prompted above. I think that's because the constructor's parameter of this class require a type Ack also, but I have no idea on that.

gtltc
  • 15
  • 4

1 Answers1

0

From output you provided, it seems you only need to specify Ack as the last parameter of your constructor. It's acknowledgment that you received a message. According to official description, there is two modes available.

First, MqttAndroidClient.Ack.AUTO_ACK, which acknowledge automatically as soon as you received a message.

And then you have MqttAndroidClient.Ack.MANUAL_ACK, which requires you to manually acknowledge by doing MqttAndroidClient.acknowledgeMessage(String)

You can test it simply by adding the auto for now, and if it's ok then you can manually acknowledge yourself with custom rules.

Jacouille
  • 951
  • 8
  • 14
  • I changed it to `MqttAndroidClient client = new MqttAndroidClient(MainActivity.this, "tcp://10.0.2.2:1883", clientId, Ack.AUTO_ACK);`. I've got this error instead `java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;` Not sure that do you mean like using Ack from paho or from my downloaded library? Because I'm using the downloaded one. – gtltc Apr 12 '22 at 11:10
  • This error come from Kotlin, i think it's not related to MqttAndroidClient. see this https://stackoverflow.com/questions/43943474/android-library-module-developed-in-kotlin-exported-to-java-application-causing – Jacouille Apr 12 '22 at 11:45
  • I have no idea about that, maybe it comes from the library I got from this link 'https://stackoverflow.com/questions/71155187/android-paho-mqtt-crashes-android-12-targeting-s-version-31-and-above-requi'. I found that it is written in Kotlin, but I used it for my Java Android App. So I don't know where it gets an error by MqttAndroidClient line. For now, I'm thinking about downgrading my api to be around api version 30 to avoid the problem in paho if I can't figure it out. – gtltc Apr 12 '22 at 12:47
  • also I wonder if I should use it for Kotlin application instead? Since the library is written in Kotlin. – gtltc Apr 12 '22 at 12:49
  • Yeah app in Android and lib in Kotlin may cause similar problems, you should definitely use Kotlin for your app, or Java for both app and lib. You can take a look at HiveMq as an alternative to Paho, which is easier IMHO – Jacouille Apr 12 '22 at 13:46