如何求解Android 12无法使用MQTTANDROIDCLIENT

发布于 2025-01-21 04:28:57 字数 4192 浏览 3 评论 0 原文

在我的Android Studio中,我想将MQTT Android客户端连接到我的笔记本电脑主机(在同一台计算机中)。我使其与本指南相似

然后,我发现Android 12(在我的情况下为API 32)可能不支持 org。 eclipse.paho:org.eclipse.paho.android.service:1.1.1 依赖关系。因此,我遵循下面的解决方案,以导入 servicelibrary-release.aar 库中提供的库。 (此链接中出现的问题是我的情况相同的)

android paho mqtt崩溃android 12-针对S+(版本31及更高版本)要求flag_immutable或flag_mutable之一

之后,我遇到了另一个错误。

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

因此,我不确定上面解决方案的库是否可以应用于我的旧代码,还是需要修改一些代码?

这是我的代码和Gradle文件。

存储库

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

依赖项

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

android清单 (下面添加了权限)

<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"/>

主要活动

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();
                }
            }
        });
    }
}

该错误出现在此行中(单击按钮时)

mqttandroidclient client = new mqttandroidclient(mainActivity.this,“ tcp://10.0.2.2:1883”,clientid);

从上面提示的错误消息中。我认为这是因为此类的构造函数的参数也需要类型 ack ,但我对此不知道。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2025-01-28 04:28:57

从您提供的输出中,您似乎只需要将ACK指定为构造函数的最后一个参数。
确认您收到了一条消息。根据官方的,有两种模式可用。

首先, mqttandroidclient.ack.auto_ack ,一旦收到消息,它会自动确认。

然后,您拥有 mqttandroidclient.ack.ack.manual_ack ,它要求您通过执行 mqttandroidclient.acknowledgemessage(string)(string)

您可以通过现在添加自动添加自动,从如果可以的话,那么您可以手动承认自己使用自定义规则。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文