1、最方便的方法,使用dlc的module连接mqtt
步骤、1——导入dlc依赖和module
//dlc_mqttmodule
implementation project(':garbagerecyclebox')//dlc公库
implementation('com.github.DlcAndroidTeam123456:DlcCommonLibrary:1.0.31-simple') {exclude group: 'me.yokeyword', module: 'fragmentation'
}
步骤、2——创建工具方法,调用即可
// MqttProxy.init(this);//dlc的mqtt类使用
// MqttMgr.get().publish();//dlc发送mqtt对象
MqttMgr.get().publish(MqttInformation.GDJBHSX_RECEIVE, 0, openBackBean);
public static final String GDJBHSX_RECEIVE = "sdfyhsx_receive/" + DeviceUtil.getAndroidId();
/*** @author : LGQ* @date : 2020/05/18 10* @desc :*/public class MqttProxy {private static Activity activity;public static void init(Context context) {activity = (Activity) context;DlcMqttConnectOptions dlcMqttConnectOptions = new DlcMqttConnectOptions(context,MqttInformation.SERVER_URI, MqttInformation.USER_NAME, MqttInformation.PASS_WORD);
// MqttMgr.get().addSubscribeTopics(MqttInformation.GDJBHSX_PUSH_APKBG, 0);MqttMgr.get().addSubscribeTopics(MqttInformation.GDJBHSX_PUSH, 0);LgqLogutil.e("主题地址=== "+MqttInformation.GDJBHSX_PUSH);
// MqttMgr.get().addSubscribeTopics(MqttInformation.GDJBHSX_PUSH_UDCOMPANY, 0);
// MqttMgr.get().addSubscribeTopics(MqttInformation.GDJBHSX_PUSH_ERRORKE, 0);
// MqttMgr.get().addSubscribeTopics(MqttInformation.GDJBHSX_PUSH_UPDATEAPK, 0);MqttMgr.get().init(context, dlcMqttConnectOptions, new BaseMqttCallBack() {@Overridepublic void messageArrived(String topic, String message, int messageId) {handleMessageArrived(topic, message, messageId);
// ObServernotice.getInstance().notifyObserver(5481,5,message,null);LgqLogutil.e("mqtt注册成功sxxxx00000=== "+message);}@Overridepublic void onSuccess(DlcMqttToken asyncActionToken) {LgqLogutil.e("mqtt注册成功sxxxx");}@Overridepublic void onFailure(DlcMqttException e) {LgqLogutil.e("成功sxxxx1111111");}});}public static void onDestroy() {MqttMgr.get().onDestroy();activity = null;}private static void handleMessageArrived(String topic, String message, int messageId) {try {
// JSONObject json = new JSONObject(message);
// String typeName = json.getString("topic");LgqLogutil.e("返回数据====="+message);
// switch (typeName) {
//
// }} catch (Exception e) {e.printStackTrace();}Log.e("))))))))))))", "topic:" + topic + ",message:" + message + ",messageId:" + messageId);}
}
2、使用原生mqtt类实现mqtt推送以及上报数据
步骤、1——创建工具方法,调用即可
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
// MqttProxy.init(this);//dlc的mqtt类使用
// MqttMgr.get().publish();//dlc发送mqtt对象initMqtt();//原生mqtt类使用}private void initMqtt() {Looper.get().looper();}}
、、、、、、、、其余工具方法下载demo复制即可、、、、、、、、、、、、、、
/*** author: meixi* Date: 2020/8/28* Desc: mqtt初始化,心跳相关*/
public class Looper {private static class LooperHolder {static final Looper INSTANCE = new Looper();}public static Looper get() {return Looper.LooperHolder.INSTANCE;}private Looper() {//启动mqttDlcMqttConnectOptions mqttOption = new DlcMqttConnectOptions(App.sInstance, "tcp://120.77.72.190:18", "dlc", "123456", App.deviceId);mqttOption.setAutoReconnect(true);MqttManager.get().init(App.sInstance, mqttOption, new DLCMqttCallBack());MqttManager.get().subscribe(new String[]{"xmshzthg_toapk/" + App.deviceId, "xmshzthg_toapk/common"}, new int[]{0, 0});}@SuppressLint("CheckResult")public void looper() {Observable.interval(2, 30, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {@Overridepublic void accept(Long aLong) {HeartbeatBean heartBean = App.heartBeatData;if (heartBean == null) {LogPlus.i("心跳数据为空,不做心跳上传");return;}heartBean.timestamp = getTime();heartBean.macno = App.deviceId;LogPlus.i("lgq", "温度--c111-上传心跳- ");MqttManager.get().publish("xmshzthg_toback/" + App.deviceId, 0, heartBean);//todo 待验证,防止开始有心跳后来没心跳的情况,一直发送发送上一次的数据,导致异常在线状态的出现}});}public String getTime() {long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳String str = String.valueOf(time);return str;}}
demo下载链接: https://download.csdn.net/download/meixi_android/12767464
云盘下载:https://pan.baidu.com/s/1Nl9aQW-l3SxSlwWdd60uew
在线bug交流:扣扣1085220040