Giter VIP home page Giter VIP logo

mqtt-client's Introduction

Simple Android MQTT Client

A simple wrapper that allows you to publish and subscribe to a topic on an MQTT Server.

Include

Include jcenter in your repo list:

    
    buildscript {
        repositories {
            jcenter()
        }
    }

Add to your dependencies:


    compile 'com.shredder:mqtt:0.0.+'
    

Usage

Create your configuration:

        MqttManagerConfig mqttConfiguration = new MqttManagerConfig() {
            @Override
            public String getHost() {
                //Hive provides a free brokering service, best change this before going live!
                return "tcp://broker.hivemq.com:1883";
            }

            @Override
            public QualityOfService getQualityOfService() {
                //RTFM for the meaning of these. FireandForget is best to start with
                return QualityOfService.FireAndForget;
                //return QualityOfService.GuaranteedDelivery;
                //return QualityOfService.GuaranteedOnceOnlyDelivery;
            }

            @Override
            public String getUniqueId() {
                //Each client that connects to the MQTT Server must have their own unique ID. 
                // Otherwise they are kicked off by the next connection with the same ID
                return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        };
        

Next create a MQTT Manager and subscribe to a topic:

        
           this.mqttManager = new MqttManager(config, new MqttManager.Listener() {
                    @Override
                    public void onMessageReceived(String message, String topic) {
                        // Do something with the message.
                    }
                });
           this.mqttManager.start(); // don't forget to start the manager
           this.mqttManager.subscribe("myinterestingtopic");

                

or if you are only interested in publishing and not subscribing

        
           this.mqttManager = new MqttManager(config, null);
           this.mqttManager.start();
           
                

now you can publish to your heart's content:

        
        this.mqttManager.publish("Is there anyone out there?", "myinterestingtopic");
           
                

mqtt-client's People

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.