GithubHelp home page GithubHelp logo

cplasf1925 / android-mqtt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zhele/android-mqtt

1.0 1.0 0.0 178 KB

MQTT在Android上的二次封装,使用更加简单方便。

License: Apache License 2.0

Kotlin 100.00%

android-mqtt's Introduction

MQTT在Android上的二次封装

Download

添加依赖

  • 在project的build.gradle文件中添加maven仓库

    repositories {
        maven {
            url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
        }
    }
    
  • 在app的build.gradle文件中添加依赖

    dependencies {
        implementation 'com.pingerx:android-mqtt:1.0.4'
    }
    

使用流程

  • 初始化MQTT

    MqttManager.getInstance().init(context,config)
    
  • 连接MQTT服务端

    MqttManager.getInstance().connect {
        onConnectSuccess {
            showTips("服务器连接成功")
        }
        onConnectFailed {
            showTips("服务器连接失败:${it?.message}")
        }
    }
    
  • 订阅一个话题

    MqttManager.getInstance().subscribe(topic) {
        onSubscriberSuccess {
            showTips("订阅成功")
        }
        onSubscriberFailed {
            showTips("订阅失败:${it?.message}")
        }
        onDeliveryComplete {
            showTips("消息推送完毕:$it")
        }
        onConnectionLost {
            showTips("连接已断开")
        }
    }
    
  • 推送一条消息

    MqttManager.getInstance().publishMessage(topic, "Hello Mqtt...")
    
  • 主动断开连接

    MqttManager.getInstance().disconnect()
    
  • 关闭MQTT客户端(一般app退出时调用)

    MqttManager.getInstance().close()
    

相关学习资料

android-mqtt's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.