閱讀649 返回首頁    go iPhone_iPad_Mac_手機_平板_蘋果apple


JAVA-SDK使用(MQTT)__SDK使用參考_設備端接入手冊_阿裏雲物聯網套件-阿裏雲

本文以JAVA版SDK為例,演示如何讓設備通過MQTT協議連接到阿裏雲IoT,並通過PUB/SUB實現一個簡單的M2M通信。

使用示例

  • 第一步: 首先通過設備認證,獲取到對應的配置信息.
  • 第二步: 通過獲取到的公鑰證書來配置TLS/SSL,與阿裏雲進行連接.
  • 第三步: 通過MQTT協議與阿裏雲進行設備通信.

請使用JDK7來跑DEMO程序.運行DEMO之前,請首先修改 com.alibaba.iot.demo.util.Config 類,將對應的配置信息填寫完整。

第一步

獲取證書,利用IotAuthUtil類來處理. 具體的流程參考設備認證的流程.


  1. //Step 1: 獲取配置信息
  2. System.out.println("開始獲取配置信息!");
  3. if (Config.deviceName.equals(Config.deviceName2))
  4. {
  5. System.out.println("設備1和設備2不能相同!");
  6. return;
  7. }
  8. Map<String, String> result = IotAuthUtil.auth(Config.deviceName, Config.deviceSecret);
  9. //1.1 得到公鑰的BASE64編碼以後的字符串數據
  10. String pubKey = result.get("pubkey");
  11. //1.2 得到連接的目的地IP與端口
  12. String servers = result.get("servers");
  13. String targetServer = servers.substring(0, servers.indexOf("|"));
  14. //1.3 得到BASE64字符串解碼以後的公鑰證書文件
  15. byte[] pubKeyByteContent = Base64Util.decode(pubKey);
  16. //1.4 得到設備的ID
  17. String deviceId1 = result.get("deviceId");
  18. result = IotAuthUtil.auth(Config.deviceName2, Config.deviceSecret2);
  19. String deviceId2 = result.get("deviceId");
  20. System.out.println("獲取配置成功! 接入地址: " + targetServer);

第二步

配置TLS/SSL信息.


  1. InputStream is = new ByteArrayInputStream(pubKeyByteContent);
  2. InputStream caInput = new BufferedInputStream(is);
  3. CertificateFactory cf = CertificateFactory.getInstance("X.509");
  4. Certificate ca = null;
  5. try {
  6. ca = cf.generateCertificate(caInput);
  7. } catch (CertificateException e) {
  8. e.printStackTrace();
  9. } finally {
  10. caInput.close();
  11. }
  12. String keyStoreType = KeyStore.getDefaultType();
  13. KeyStore keyStore = KeyStore.getInstance(keyStoreType);
  14. keyStore.load(null, null);
  15. keyStore.setCertificateEntry("ca", ca);
  16. String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
  17. TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
  18. tmf.init(keyStore);
  19. SSLContext context = SSLContext.getInstance("TLSV1.2");
  20. context.init(null, tmf.getTrustManagers(), null);
  21. SSLSocketFactory socketFactory = context.getSocketFactory();

第三步

與阿裏雲建立TLS/SSL連接.


需要注意的是在配置MQTT連接時, Connect協議當中 clientId 屬性值為 productKey:deviceId username 屬性值為 ToUpperCase(MD5_32(productKey+productSecret+deviceId+deviceSecret)) 這樣以便阿裏雲對當前連接的有效性進行驗證.

  1. final String topic = Config.topic;
  2. String broker = "ssl://" + targetServer;
  3. //客戶端ID格式: productKey + deviceId.
  4. String clientId = Config.productKey + ":" + deviceId;
  5. MemoryPersistence persistence = new MemoryPersistence();
  6. SSLSocketFactory socketFactory = createSSLSocket(stest);
  7. final MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
  8. MqttConnectOptions connOpts = new MqttConnectOptions();
  9. connOpts.setMqttVersion(4);// MQTT 3.1.1
  10. connOpts.setSocketFactory(socketFactory);
  11. String signUserName = signUserName(deviceId);
  12. connOpts.setUserName(signUserName);
  13. connOpts.setKeepAliveInterval(65);
  14. System.out.println("進行連接, 目的地: " + broker);
  15. sampleClient.connect(connOpts);

第四步

進行消息Publish.


  1. String content = "Message From Device:" + Config.deviceName2;
  2. MqttMessage message = new MqttMessage(content.getBytes());
  3. message.setQos(1);
  4. sampleClient.publish(topic, message);
  5. System.out.println("消息發布成功!");

備注:在第四步進行數據通信時需要創建Topic,請在控製台將設備添加授權。如果多個設備使用同一個Topic即可實現M2M通信。

最後更新:2016-07-26 18:54:03

  上一篇:go C-SDK使用(其它)__SDK使用參考_設備端接入手冊_阿裏雲物聯網套件-阿裏雲
  下一篇:go JAVA-SDK使用(CCP)__SDK使用參考_設備端接入手冊_阿裏雲物聯網套件-阿裏雲