服务端测试:
EMQX Cloud:在线测试
MQTTX:需要下载电脑客户端
android:
项目下build.gradle
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1' implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' //LocalBroadcastManager这个类被弃用了,需要添加 implementation 'com.android.support:support-v4:30.4.1'
工程下 gradle.properties
#LocalBroadcastManager这个类被弃用了,需要添加 android.enableJetifier=true
清单文件:
public interface IGetMessageCallBack { void setMessage(String message); }
MqttServiceConnection
-
- public class MqttServiceConnection implements ServiceConnection {
-
- private MQTTService mqttService;
- private IGetMessageCallBack iGetMessageCallBack;
-
- @Override
- public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
- mqttService = ((MQTTService.CustomBinder) iBinder).getService();
- mqttService.setIGetMessageCallBack(iGetMessageCallBack);
- }
-
- @Override
- public void onServiceDisconnected(ComponentName componentName) {
-
- }
-
- public MQTTService getMqttService() {
- return mqttService;
- }
-
- public void setIGetMessageCallBack(IGetMessageCallBack iGetMessageCallBack) {
- this.iGetMessageCallBack