安卓开发之mqtt协议实例代码

 更新时间:2017年12月15日 14:18:42   作者:流浪少年  
下面小编就为大家分享一篇安卓开发之mqtt协议实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

脚本之家 / 编程助手:解决程序员“几乎”所有问题!
脚本之家官方知识库 → 点击立即使用

首先物联网协议mqtt协议是基于tcp/ip协议的,使用了官方的mqttclient框架

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
*初始化mqttclient
*/
private void init() {
    try {
      //MQTT的连接设置
      options = new MqttConnectOptions();
      //host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
      client = new MqttClient(new Ip().host, username,
          new MemoryPersistence());
      //设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
      options.setCleanSession(false);
//options.setWill(myTopic,null,2,false);
 
      //设置连接的用户名
      options.setUserName(login_token);
      //设置连接的密码
      options.setPassword(passWord.toCharArray());
      // 设置超时时间 单位为秒
      options.setConnectionTimeout(10);
      // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
      options.setKeepAliveInterval(60);
      //设置回调
      client.setCallback(new MqttCallback() {
        @Override
        public void connectionLost(Throwable cause) {
          //连接丢失后,一般在这里面进行重连
          System.out.println("connectionLost----------");
 
        }
 
        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {
          //publish后会执行到这里
          System.out.println("deliveryComplete---------"
              + token.isComplete());
        }
        @Override
        public void messageArrived(String topicName, MqttMessage message)
            throws Exception {
          byte[] message1 = message.getPayload();
          // subscribe后得到的消息会执行到这里面
          System.out.println("messageArrived----------" + message1[0] + Arrays.toString(message1));
          System.out.println(message1[0] == 5);
          String id = new String(subBytes(message1, 1, 16), "UTF-8");
 
          System.out.print("mqtt收到的id" + id);
          DeviceList device = getBookById(id);
          System.out.print("device" + device.getName());
          String name = device.getName();
          String gName = device.getgName();
          String type = device.getType();
          System.out.print("名字为" + name + gName);
          /**
          * 使用handler发送matt接收的消息,格式为二进制数据
          * */
 
          Message msg = new Message();
          msg.what = 1;
          if (message1[0] == 1) {
//            msg.obj = name + "设备心跳";
//            handler.sendMessage(msg);
            return;
          }
          if (message1[0] == 2) {
            msg.obj = gName + "" + name + "报警";
            msg.arg1 = Integer.parseInt(type);
            handler.sendMessage(msg);
            return;
          }
          if (message1[0] == 3) {
            msg.obj = gName + "" + name + "上线";
            handler.sendMessage(msg);
            return;
          }
          if (message1[0] == 4) {
            msg.obj = gName + "" + name + "离线";
            handler.sendMessage(msg);
            return;
          }
          if (message1[0] == 5) {
            if (message1[17] == 0) {
              msg.obj = gName + "" + name + "关门";
            } else {
              msg.obj = gName + "" + name + "开门";
            }
 
 
            handler.sendMessage(msg);
            return;
          }
          if (message1[0] == 20 && message1[17] > 0 && message1[17] < 20) {
 
            msg.obj = name + "电池电量: " + message1[17] + "%";
            handler.sendMessage(msg);
            System.out.println("电池:" + name + "电池电量: " + message1[17] + "%");
            return;
          }
          if (message1[17] > 0) {
            SharedPreferences sp = getActivity().getSharedPreferences(id, getActivity().MODE_PRIVATE);
            // 获得sp的编辑器
            SharedPreferences.Editor ed = sp.edit();
            // 以键值对的显示将用户名和密码保存到sp中
            ed.putString("battery", String.valueOf(message1[17]));
            // 提交用户名和密码
            ed.commit();
          }
        }
      });
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  public byte[] subBytes(byte[] src, int begin, int count) {
    byte[] bs = new byte[count];
    System.arraycopy(src, begin, bs, 0, count);
    return bs;
  }
 
  //根据id拿到属性为id的Book对象。
  public static DeviceList getBookById(String id) {
    DeviceList book = new DeviceList();
    book.setId(id);//设置传入的id值
    //books.indexOf()根据id比较对象是否相等
    return deviceList1.get(deviceList1.indexOf(book));
    //返回关联id的Book对象。
  }
   
  private void connect() {
    new Thread(new Runnable() {
 
      @Override
      public void run() {
        try {
          client.connect(options);
          Message msg = new Message();
          msg.what = 2;
          handler.sendMessage(msg);
        } catch (Exception e) {
          e.printStackTrace();
          Message msg = new Message();
          msg.what = 3;
          handler.sendMessage(msg);
        }
      }
    }).start();
  }
 
  protected boolean onKeyDown(int keyCode, KeyEvent event) {
    if (client != null && keyCode == KeyEvent.KEYCODE_BACK) {
      try {
        client.disconnect();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return super.getActivity().onKeyDown(keyCode, event);
  }
 
  @Override
  public void onDestroy() {
    super.onDestroy();
    try {
      scheduler.shutdown();
      client.disconnect();
    } catch (MqttException e) {
      e.printStackTrace();
    }
  }
 
  private void startReconnect() {
    scheduler = Executors.newSingleThreadScheduledExecutor();
    scheduler.scheduleAtFixedRate(new Runnable() {
 
      @Override
      public void run() {
        if (!client.isConnected()) {
          connect();
        }
      }
    }, 0 * 1000, 10 * 1000, TimeUnit.MILLISECONDS);
  }
其次使用handlermessage接收消息,并已notifacation的形式展示在通知栏页面
 handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 1) {
          NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
          Notification myNotify = new Notification();
          myNotify.icon = R.drawable.logo;
          myNotify.tickerText = "新消息";
          myNotify.when = System.currentTimeMillis();
          //使用默认的声音
          myNotify.defaults |= Notification.DEFAULT_SOUND;
 
//使用默认的震动
          myNotify.defaults |= Notification.DEFAULT_VIBRATE;
 
//使用默认的声音、振动、闪光
          myNotify.defaults = Notification.DEFAULT_ALL;
// myNotify.flags=Notification.FLAG_AUTO_CANCEL;
          // myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
          RemoteViews rv = new RemoteViews(getActivity().getPackageName(),
              R.layout.activity_notification1);
          rv.setTextViewText(R.id.tv_desc, (String) msg.obj);
          myNotify.contentView = rv;
          Intent intent = new Intent(getActivity(), MainActivity.class);
          //  intent.addCategory(Intent.CATEGORY_LAUNCHER);
          //  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
          android.app.PendingIntent contentIntent = android.app.PendingIntent.getActivity(getActivity(), 0,
              intent, 0);
          myNotify.contentIntent = contentIntent;
          manager.notify(i1++, myNotify);
          PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
          PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
              PowerManager.SCREEN_DIM_WAKE_LOCK, "target");
          boolean screen = pm.isScreenOn();
          if (!screen) {//如果灭屏
            wakeLock.acquire();
            wakeLock.release();
          }
          refresh();
        } else if (msg.what == 2) {
          System.out.println("连接成功");
          System.out.print("连接成功大小" + listDatas2.size());
          try {
            client.subscribe(myTopic, 1);
            client.subscribe(myTopic1, 1);
          } catch (MqttException e) {
            e.printStackTrace();
          }
        } else if (msg.what == 3) {
          //Toast.makeText(MainActivity.this, "连接失败,系统正在重连", Toast.LENGTH_SHORT).show();
          System.out.println("连接失败,系统正在重连");
        }
      }
    };

以上这篇安卓开发之mqtt协议实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

蓄力AI

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

原文链接:http://www.cnblogs.com/wuxilvxin/archive/2017/12/15/8042481.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!

相关文章

最新评论