赞
踩
先看效果图:
OneNet:
第一步:创建产品
1、多协议接入
2、添加产品
3、填写产品信息
4、我已经创建过了,就不重新创了,我的相关填写如下图
5、创建成功界面
第二步:创建设备
1、打开设备列表,点击添加设备
2、添加设备的具体信息(注意:这里的鉴权信息相当于密码,是创建设备时你自己随便设的)
Android:
第一步:配置(这里配置分为两个小点)
1、在build.gradle中的dependencies添加依赖(MQTT的实现一般使用这两个库):
- implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
- implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'
2、在AndroidManifest.xml中设置权限以及添加服务:
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.WAKE_LOCK"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<service android:name="org.eclipse.paho.android.service.MqttService"/>
第二步:页面布局(在activity_main.xml中添加按钮)
- <Button
- android:id="@+id/conBtn"
- android:layout_width="130dp"
- android:layout_height="60dp"
- android:text="Connect"
- android:textSize="18sp"/>
-
- <Button
- android:id="@+id/subBtn"
- android:layout_width="130dp"
- android:layout_height="60dp"
- android:text="订阅"
- android:textSize="18sp"/>
-
- <Button
- android:id="@+id/pubBtn"
- android:layout_width="130dp"
- android:layout_height="60dp"
- android:text="发布"
- android:textSize="18sp"/>
-
- <Button
- android:id="@+id/disConBtn"
- android:layout_width="130dp"
- android:layout_height="60dp"
- android:text="断开连接"
- android:textSize="18sp"/>
第三步:初始化
1、按钮点击监听事件(连接、订阅、发布以及断开连接)
- private void initView() {
- conBtn = findViewById(R.id.conBtn);
- subBtn = findViewById(R.id.subBtn);
- pubBtn = findViewById(R.id.pubBtn);
- disConBtn = findViewById(R.id.disConBtn);
-
- conBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (!client.isConnected()) {
- try {
- // 连接
- client.connect(conOpt, null, mqttActionListener);
- } catch (MqttException e) {
- Toast.makeText(MainActivity.this, "mqttConnect fail:" + e.getStackTrace(), Toast.LENGTH_SHORT).show();
- }
- }
- }
- });
-
- subBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (client.isConnected()) {
- mqttSubscribe("Topic名称");
- }else {
- Toast.makeText(MainActivity.this, "请连接服务器!", Toast.LENGTH_SHORT).show();
- }
- }
- });
-
- pubBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (client.isConnected()) {
- mqttPublish("Topic名称","数据");
- }else {
- Toast.makeText(MainActivity.this, "请连接服务器!", Toast.LENGTH_SHORT).show();
- }
- }
- });
-
- disConBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- if (client.isConnected()) {
- // 断开连接
- client.disconnect();
- Toast.makeText(MainActivity.this, "设备已断开!", Toast.LENGTH_SHORT).show();
- }else {
- Toast.makeText(MainActivity.this, "设备未连接!", Toast.LENGTH_SHORT).show();
- }
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- });
- }
2、MQTT产品以及设备的配置
- private MqttAndroidClient client;
- private MqttConnectOptions conOpt;
- private MqttCallback mqttCallback;
- private IMqttActionListener mqttActionListener;
-
- private static final String SERVER_URL = "tcp://mqtt.heclouds.com:6002";
- private static final String CLIENT_ID = "设备ID";
- private static final String USER_NAME = "产品ID";
- private static final String PASSWORD = "鉴权信息";
3、mqtt客户端初始化
- iniData();
- initView();
-
- client = new MqttAndroidClient(this, SERVER_URL, CLIENT_ID);
-
- client.setCallback(mqttCallback);
- conOpt = new MqttConnectOptions();
- conOpt.setCleanSession(true);
- conOpt.setConnectionTimeout(10);
- conOpt.setKeepAliveInterval(30);
- conOpt.setUserName(USER_NAME.trim());
- conOpt.setPassword(PASSWORD.trim().toCharArray());
4、mqtt数据回读以及动作监听
- private void iniData() {
- mqttCallback = new MqttCallback() {
- @Override
- public void connectionLost(Throwable cause) {
-
- }
-
- @Override
- public void messageArrived(String topic, MqttMessage message) throws Exception {
- // 收到消息,打印消息
- String msg = new String(message.getPayload());
- Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
- Log.d(TAG, "收到消息:" + msg);
- }
-
- @Override
- public void deliveryComplete(IMqttDeliveryToken token) {
-
- }
- };
-
- mqttActionListener = new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- Log.d(TAG, "连接成功");
- }
-
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- Log.d(TAG, "连接失败");
- }
- };
- }
第四步:订阅
- public void mqttSubscribe(String topic) {
- try {
- // 订阅myTopic话题
- client.subscribe(topic, 1, null, new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- Toast.makeText(MainActivity.this, "订阅成功", Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- Toast.makeText(MainActivity.this, "订阅失败", Toast.LENGTH_SHORT).show();
- }
- });
- } catch (MqttException e) {
- Toast.makeText(MainActivity.this, "订阅失败:" + e.getStackTrace(), Toast.LENGTH_SHORT).show();
- }
- }
第五步:发布
- public void mqttPublish(String topic,String msg) {
- Integer qos = 0;
- Boolean retained = false;
- try {
- if (client != null) {
- client.publish(topic, msg.getBytes(), qos.intValue(), retained.booleanValue(), null, new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- Toast.makeText(MainActivity.this, "发布消息成功", Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- Toast.makeText(MainActivity.this, "发布消息失败", Toast.LENGTH_SHORT).show();
- }
- });
- }
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。