项目作者: GregoryHo

项目描述 :
An android mqtt client.
高级语言: Java
项目地址: git://github.com/GregoryHo/MqttManager.git
创建时间: 2017-08-04T09:59:46Z
项目社区:https://github.com/GregoryHo/MqttManager

开源协议:MIT License

下载


MQTT Manager

A connection / subscribe / publish handler of MQTT

license

Create Connection

  1. Connection connection = Connection.createConnection(context, "endpoint", "clientId")
  2. .addConnectionOptions(new Connection.ConnectOptionsBuilder().setUser("test")
  3. .setPassword("1234")
  4. .setConnectionTimeout(30)
  5. .setKeppAliveInterval(10));
  6. connection.connect(new OnActionListener() {
  7. @Override public void onSuccess(MqttTopic mqttTopic, String message) {
  8. /* do something when connected */
  9. }
  10. @Override public void onFailure(MqttTopic mqttTopic, Throwable throwable) {
  11. /* some error handle when failure */
  12. }
  13. });
  14. `

Create Topic

  1. /* demo topic, create every topic with prefix /DEMO/ */
  2. public class DemoTopic extends MqttTopic {
  3. private final String topic;
  4. private String message;
  5. public DemoTopic(String topic) {
  6. this(topic, "");
  7. }
  8. public DemoTopic(String mqttTopic, String message) {
  9. super("/DEMO/" + topic);
  10. this.topic = topic;
  11. this.message = message;
  12. }
  13. public String getTopic() {
  14. return topic;
  15. }
  16. }
  17. /* topic for subscribe */
  18. public class SubscribeTopic extends DemoTopic implements Subscription {
  19. public SubscribeTopic(String topic) {
  20. super(topic);
  21. }
  22. @Override public int getSubscriptionQoS() {
  23. return MqttConstants.AT_LEAST_ONCE;
  24. }
  25. }
  26. /* topic for publish */
  27. public class PublishTopic extends DemoTopic implements Publishing {
  28. public PublishTopic(String topic) {
  29. super(topic);
  30. }
  31. public PublishTopic(String topic, String message) {
  32. super(topic, message);
  33. }
  34. @Override public int getPublishingQoS() {
  35. return MqttConstants.EXACTLY_ONCE;
  36. }
  37. @Override public int isRetained() {
  38. return MqttConstants.RETAINED;
  39. }
  40. @Override public String getPublishingMessage() {
  41. return getMessage();
  42. }
  43. }

Subscribe Topic

The connection will check current state,
if connected, it will just subscribe the topic,
otherwise it will automatic connect to server and subscribe when connected.

Single

  1. connection.subscribeTopic(topic, new OnActionListener<SubscribeTopic>() {
  2. @Override public void onSuccess(SubscribeTopic topic, String message) {
  3. /* subscribe topic succeeded */
  4. }
  5. @Override public void onFailure(SubscribeTopic topic, Throwable throwable) {
  6. /* subscribe topic failure */
  7. }
  8. });

Multiple

  1. List<DemoTopic> subscriptionList = new ArrayList<>();
  2. subscriptionList.add(topic);
  3. subscriptionList.add(topic2);
  4. connection.subscribeTopics(subscriptionList, new OnActionListener<SubscribeTopic>() {
  5. @Override public void onSuccess(SubscribeTopic topic, String message) {
  6. /* subscribe topic succeeded */
  7. }
  8. @Override public void onFailure(SubscribeTopic topic, Throwable throwable) {
  9. /* subscribe topic failure */
  10. }
  11. });

Publish Topic

The connection will check current state,
if connected, it will just publish the topic,
otherwise it will automatic connect to server and publish when connected.

Single

  1. topic.setMessage("some message");
  2. /* doesn't care about callback */
  3. connection.publishTopic(topic, null);
  4. connection.publishTopic(topic, new OnActionListener<PublishTopic>() {
  5. @Override public void onSuccess(PublishTopic mqttTopic, String message) {
  6. /* publish topic succeeded */
  7. }
  8. @Override public void onFailure(PublishTopic mqttTopic, Throwable throwable) {
  9. /* publish topic failure */
  10. }
  11. })