{"id":18876023,"url":"https://github.com/igenius-srl/android-mqtt-service","last_synced_at":"2025-10-08T15:58:38.617Z","repository":{"id":66358978,"uuid":"79118761","full_name":"iGenius-Srl/android-mqtt-service","owner":"iGenius-Srl","description":"MQTT service for Android","archived":false,"fork":false,"pushed_at":"2018-08-21T11:13:43.000Z","size":324,"stargazers_count":42,"open_issues_count":6,"forks_count":24,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-14T17:55:45.613Z","etag":null,"topics":["android","background","mqtt","mqtt-service","paho","service"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iGenius-Srl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-16T13:03:19.000Z","updated_at":"2024-07-11T09:28:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"0c5a3eff-c61e-44b1-9a8d-ec5096eb4ef6","html_url":"https://github.com/iGenius-Srl/android-mqtt-service","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/iGenius-Srl/android-mqtt-service","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGenius-Srl%2Fandroid-mqtt-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGenius-Srl%2Fandroid-mqtt-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGenius-Srl%2Fandroid-mqtt-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGenius-Srl%2Fandroid-mqtt-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iGenius-Srl","download_url":"https://codeload.github.com/iGenius-Srl/android-mqtt-service/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGenius-Srl%2Fandroid-mqtt-service/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278972318,"owners_count":26078017,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","background","mqtt","mqtt-service","paho","service"],"created_at":"2024-11-08T06:10:13.793Z","updated_at":"2025-10-08T15:58:38.612Z","avatar_url":"https://github.com/iGenius-Srl.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Android MQTT Service [![Build Status](https://travis-ci.org/iGenius-Srl/android-mqtt-service.svg?branch=master)](https://travis-ci.org/iGenius-Srl/android-mqtt-service) [![Download](https://api.bintray.com/packages/igenius-code/maven/android-mqtt-service/images/download.svg) ](https://bintray.com/igenius-code/maven/android-mqtt-service/_latestVersion)\n\nA tiny wrapper around [Eclipse Paho MQTT Java library](https://github.com/iGenius-Srl/paho.mqtt.java), to have a lightweight background Android service, which handles all the following operations in a background thread:\n\n* connect to broker\n* disconnect from broker\n* publish on topics\n* subscribe to topics\n* receive messages\n\n## Setup\n```groovy\nimplementation 'net.igenius:mqttservice:1.6.4'\n```\n\nBefore using the library, you have to initialize it. The ideal place to do that is in your [Application subclass](http://developer.android.com/reference/android/app/Application.html):\n```java\npublic class Initializer extends Application {\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n\n        MQTTService.NAMESPACE = \"com.yourcompany.yourapp\"; //or BuildConfig.APPLICATION_ID;\n        MQTTService.KEEP_ALIVE_INTERVAL = 60; //in seconds\n        MQTTService.CONNECT_TIMEOUT = 30; //in seconds\n    }\n}\n```\n\n## Send commands to the MQTT service\nAll the commands supported by the service are implemented in the `MQTTServiceCommand` class. Some of them are:\n```java\n// connect to the broker\nMQTTServiceCommand.connect(final Context context, final String brokerUrl,\n                           final String clientId, final String username,\n                           final String password)\n\n// disconnect from the broker\nMQTTServiceCommand.disconnect(final Context context)\n\n// subscribe to one or more topics\nMQTTServiceCommand.subscribe(final Context context, final int qos,\n                             final boolean autoResubscribeOnReconnect,\n                             final String... topics)\n\n// connect to a broker and subscribe to one or more topics with a QoS\nMQTTServiceCommand.connectAndSubscribe(final Context context,\n                                       final String brokerUrl,\n                                       final String clientId,\n                                       final String username,\n                                       final String password,\n                                       final int qos,\n                                       final boolean autoResubscribeOnReconnect,\n                                       final String... topics)\n\n// publish a payload to a topic, with a specific QoS\nMQTTServiceCommand.publish(final Context context, final String topic,\n                           final byte[] payload, final int qos)\n\n// publish a payload to a topic, with QoS 0\nMQTTServiceCommand.publish(final Context context, final String topic,\n                           final byte[] payload)\n```\nExplore the class for complete JavaDocs and all the available options.\n\n## Receive MQTT events\n### Globally in the app\nTo receive events globally in the app, even if it's in background, create a new class in your project:\n```java\nimport android.content.Context;\n\nimport net.igenius.mqttservice.MQTTServiceReceiver;\n\npublic class MQTTReceiver extends MQTTServiceReceiver {\n\n    @Override\n    public void onPublishSuccessful(Context context, String requestId,\n                                    String topic) {\n        // called when a message has been successfully published\n    }\n\n    @Override\n    public void onSubscriptionSuccessful(Context context, String requestId,\n                                         String topic) {\n        // called when a subscription is successful\n    }\n\n    @Override\n    public void onSubscriptionError(Context context, String requestId,\n                                    String topic, Exception exception) {\n        // called when a subscription is not successful.\n        // This usually happens when the broker does not give permissions\n        // for the requested topic\n    }\n\n    @Override\n    public void onMessageArrived(Context context, String topic,\n                                 byte[] payload) {\n        // called when a new message arrives on any topic\n    }\n\n    @Override\n    public void onConnectionSuccessful(Context context, String requestId) {\n        // called when the connection is successful\n    }\n\n    @Override\n    public void onException(Context context, String requestId,\n                            Exception exception) {\n        // called when an error happens\n    }\n\n    @Override\n    public void onConnectionStatus(Context context, boolean connected) {\n        // called when connection status is requested or changes\n    }\n}\n```\nand then register it into the manifest (before `\u003c/application\u003e` tag):\n```xml\n\u003creceiver\n    android:name=\".MQTTReceiver\"\n    android:enabled=\"true\"\n    android:exported=\"false\"\u003e\n    \u003cintent-filter\u003e\n        \u003caction android:name=\"com.yourcompany.yourapp.mqtt.broadcast\" /\u003e\n    \u003c/intent-filter\u003e\n\u003c/receiver\u003e\n```\nBear in mind that `com.yourcompany.yourapp` MUST be the same you defined in `MQTTService.NAMESPACE` setting.\n\n### Inside an Activity\n```java\npublic class YourActivity extends AppCompatActivity {\n    private MQTTServiceReceiver receiver = new MQTTServiceReceiver() {\n        @Override\n        public void onSubscriptionSuccessful(Context context,\n                                             String requestId, String topic) {\n            // called when a message has been successfully published\n        }\n\n        @Override\n        public void onSubscriptionError(Context context, String requestId,\n                                        String topic, Exception exception) {\n            // called when a subscription is not successful.\n            // This usually happens when the broker does not give permissions\n            // for the requested topic\n        }\n\n        @Override\n        public void onPublishSuccessful(Context context, String requestId, String topic) {\n            // called when a subscription is successful\n        }\n\n        @Override\n        public void onMessageArrived(Context context, String topic,\n                                     byte[] payload) {\n            // called when a new message arrives on any topic\n        }\n\n        @Override\n        public void onConnectionSuccessful(Context context, String requestId) {\n            // called when the connection is successful\n        }\n\n        @Override\n        public void onException(Context context, String requestId,\n                                Exception exception) {\n            // called when an error happens\n        }\n\n        @Override\n        public void onConnectionStatus(Context context, boolean connected) {\n            // called when connection status is requested or changes\n        }\n    };\n\n    @Override\n    protected void onResume() {\n        super.onResume();\n        receiver.register(this);\n    }\n\n    @Override\n    protected void onPause() {\n        super.onPause();\n        receiver.unregister(this);\n    }\n}\n```\n\n## Logging\nBy default the library logging is disabled. You can enable debug log by invoking:\n```java\nMQTTServiceLogger.setLogLevel(LogLevel.DEBUG);\n```\nwherever you want in your code. You can adjust the level of detail from DEBUG to OFF.\n\nThe library logger uses `android.util.Log` by default, so you will get the output in `LogCat`. If you want to redirect logs to different output or use a different logger, you can provide your own delegate implementation like this:\n```java\nMQTTServiceLogger.setLoggerDelegate(new MQTTServiceLogger.LoggerDelegate() {\n    @Override\n    public void error(String tag, String message) {\n        //your own implementation here\n    }\n\n    @Override\n    public void error(String tag, String message, Throwable exception) {\n        //your own implementation here\n    }\n\n    @Override\n    public void debug(String tag, String message) {\n        //your own implementation here\n    }\n\n    @Override\n    public void info(String tag, String message) {\n        //your own implementation here\n    }\n});\n```\n\n## Example\nYou can find a fully working demo app which uses this library in the `example-app` directory. Just checkout the project and give it a try.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figenius-srl%2Fandroid-mqtt-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Figenius-srl%2Fandroid-mqtt-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figenius-srl%2Fandroid-mqtt-service/lists"}