MQTT android client

Overview

killerbee

MQTT android client

For comparison with Paho Client check the blog on Killer Bee vs Paho.

Adding as a Dependency

This package is currently hosted as a github package. Github currently supports public package hosting but requires github personal access token to fetch them. In short you need to have a github account to use this as a dependency.

Setting up github credentials

GITHUB_USER=
GITHUB_PERSONAL_ACCESS_TOKEN=

Add maven repository for github package

Add the maven repo for KillerBee as shown below in build.gradle of the project root.

def githubProperties = new Properties()
githubProperties.load(new FileInputStream(rootProject.file("github.properties")))

allprojects {
    repositories {
        google()
        mavenCentral()
        //Use for local testing of library after assemble and publishToMavenLocal
        //mavenLocal()

        maven {
            name = "KillerBee"
            url = uri("https://maven.pkg.github.com/adonmo/killerbee")
            credentials {
                username = githubProperties['GITHUB_USER']
                password = githubProperties['GITHUB_PERSONAL_ACCESS_TOKEN']
            }
        }
    }
}

Add dependency to build.gradle in app folder

implementation 'com.adonmo.libraries:killerbee:1.0.1'

Sample Implementation

, throwable: Throwable? ) { if (status == MQTTActionStatus.SUCCESS) { mqttClient.publish("HelloBee", "World".toByteArray(), 1, false) } } override fun connectionLost(connectOptions: ConnectOptions, throwable: Throwable?) { Log.d( LOG_TAG, "Connection lost for [${connectOptions.clientID}] from [${connectOptions.serverURI}]" ) } override fun messageArrived( topic: String?, message: ByteArray? ) { message?.let { Log.d(LOG_TAG, "Received message [$message]") } //mqttClient.disconnect() } } ">
package com.adonmo.sample.killerbee

import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.adonmo.killerbee.AndroidMQTTClient
import com.adonmo.killerbee.IMQTTConnectionCallback
import com.adonmo.killerbee.action.MQTTActionStatus
import com.adonmo.killerbee.adapter.ConnectOptions
import com.adonmo.killerbee.helper.Constants.LOG_TAG
import java.util.concurrent.ScheduledThreadPoolExecutor

class MainActivity : AppCompatActivity(), IMQTTConnectionCallback {
    private lateinit var mqttThread: HandlerThread
    private lateinit var mqttHandler: Handler

    private lateinit var mqttClient: AndroidMQTTClient
    private lateinit var executor: ScheduledThreadPoolExecutor

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.v(LOG_TAG, "Running on thread [${Thread.currentThread()}]")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mqttThread = HandlerThread("mqttThread")
        mqttThread.start()
        mqttHandler = Handler(mqttThread.looper)

        /* As it stands a minimum of 4 threads seems to be necessary to let the MQTT client run
            as it blocks a few of them(3 based on testing) with a looper  most likely */
        executor = ScheduledThreadPoolExecutor(4)
        mqttClient = AndroidMQTTClient(
            ConnectOptions(
                clientID = "OG",
                serverURI = "tcp://broker.hivemq.com:1883",
                cleanSession = true,
                keepAliveInterval = 30,
                maxReconnectDelay = 60000,
                automaticReconnect = true,
            ),
            mqttHandler,
            this,
            executorService = executor
        )
        mqttClient.connect()
    }

    override fun connectActionFinished(
        status: MQTTActionStatus,
        connectOptions: ConnectOptions,
        throwable: Throwable?
    ) {
        if (status == MQTTActionStatus.SUCCESS) {
            mqttClient.subscribe("Jello", 1)
            mqttClient.subscribe(arrayOf("HelloBee", "BeeHello"), intArrayOf(1, 0))
        } else {
            Log.e(
                LOG_TAG,
                "Connection Action Failed for [${connectOptions.clientID}] to [${connectOptions.serverURI}]"
            )
        }
    }

    override fun disconnectActionFinished(status: MQTTActionStatus, throwable: Throwable?) {
        Log.d(LOG_TAG, "Disconnect Action Status: [$status]")
    }

    override fun publishActionFinished(
        status: MQTTActionStatus,
        messagePayload: ByteArray,
        throwable: Throwable?
    ) {
        if (status == MQTTActionStatus.SUCCESS) {
            Log.d(LOG_TAG, "Published message $messagePayload")
        }
    }

    override fun subscribeActionFinished(
        status: MQTTActionStatus,
        topic: String,
        throwable: Throwable?
    ) {
        if (status == MQTTActionStatus.SUCCESS) {
            mqttClient.publish("HelloBee", "World".toByteArray(), 1, false)
        }
    }

    override fun subscribeMultipleActionFinished(
        status: MQTTActionStatus,
        topics: Array<String>,
        throwable: Throwable?
    ) {
        if (status == MQTTActionStatus.SUCCESS) {
            mqttClient.publish("HelloBee", "World".toByteArray(), 1, false)
        }
    }

    override fun connectionLost(connectOptions: ConnectOptions, throwable: Throwable?) {
        Log.d(
            LOG_TAG,
            "Connection lost for [${connectOptions.clientID}] from [${connectOptions.serverURI}]"
        )
    }

    override fun messageArrived(
        topic: String?,
        message: ByteArray?
    ) {
        message?.let {
            Log.d(LOG_TAG, "Received message [$message]")
        }
        //mqttClient.disconnect()
    }
}
You might also like...
Ktorfit - a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit
Ktorfit - a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

Ktorfit is a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

SimpleApiCalls is a type-safe REST client for Android. The library provides the ability to interact with APIs and send network requests with HttpURLConnection.

SimpleApiCalls 📢 SimpleApiCalls is a type-safe REST client for Android. The library provides the ability to interact with APIs and send network reque

Asynchronous Http and WebSocket Client library for Java

Async Http Client Follow @AsyncHttpClient on Twitter. The AsyncHttpClient (AHC) library allows Java applications to easily execute HTTP requests and a

Multiplatform coroutine-based HTTP client wrapper for Kotlin

networkinkt This is a lightweight HTTP client for Kotlin. It relies on coroutines on both JS & JVM platforms. Here is a simple GET request: val text =

Kotlin DSL http client
Kotlin DSL http client

Introduction Kotlin DSL http client Features 🔹 Developers Experience-driven library without verbosity. 🔹 Native way to use http client in Kotlin. 🔹

Unirest in Java: Simplified, lightweight HTTP client library.

Unirest for Java Install With Maven: !-- Pull in as a traditional dependency -- dependency groupIdcom.konghq/groupId artifactIdunire

Unirest in Java: Simplified, lightweight HTTP client library.

Unirest for Java Install With Maven: !-- Pull in as a traditional dependency -- dependency groupIdcom.konghq/groupId artifactIdunire

A gRPC Kotlin based server and client starter that builds with Gradle and runs on the JVM
A gRPC Kotlin based server and client starter that builds with Gradle and runs on the JVM

gRPC Kotlin starter Overview This directory contains a simple bar service written as a Kotlin gRPC example. You can find detailed instructions for bui

A Kotlin client for the gRPC based Bar Service
A Kotlin client for the gRPC based Bar Service

Bar Service Kotlin Client Overview This directory contains a simple bar service client written in Kotlin against generated models (protos) You can fin

Comments
  • It's using Paho library inside?

    It's using Paho library inside?

    I am confused why are you using Paho library inside your project?

    `dependencies {

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "androidx.core:core-ktx:$rootProject.ext.ktx_version"
    implementation "androidx.appcompat:appcompat:$rootProject.ext.app_compat_version"
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.ext.constraintLayoutVersion"
    testImplementation "junit:junit:$rootProject.ext.junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$rootProject.ext.androidJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
    testImplementation 'io.mockk:mockk:1.11.0'
    
    api "org.eclipse.paho:org.eclipse.paho.client.mqttv3:${rootProject.ext.clientVersion}"
    

    }`

    opened by mmehdi 1
Releases(v1.0.1)
  • v1.0.1(Jun 6, 2021)

  • v1.0.0(Jun 5, 2021)

    This is the very first iteration of Killer Bee. It is intended to support basic usage of MQTT client in android. Features like persistence and custom ping mechanisms are pushed to next release.

    Source code(tar.gz)
    Source code(zip)
Owner
Adonmo
Rethinking Outdoor Advertising
Adonmo
MQTT, publisher-subscriber, Applicazione android per il monitoraggio dello stato vitale dell'utente e dell'ambiente circostante.

MQTT, publisher-subscriber, Applicazione android per il monitoraggio dello stato vitale dell'utente e dell'ambiente circostante.

Andrea Castronovo 2 Jan 12, 2022
Ktor-Client this is the client part that uses the Ktor server

Ktor-Client this is the client part that uses the Ktor server Previews Tech stack & Open source libraries Minimum SDK level 21. Kotlin+ Coroutines + F

Mohamed Emad 4 Dec 23, 2022
Simple kafka client for monitoring topic events. Client has UI powered by Darklaf

kafka-client Simple kafka client for monitoring topic values. How to start $ java -jar kafka-client-1.0.jar How to use specify kafka hosts in config.y

Salavat 0 Jun 3, 2022
Kotlin-echo-client - Echo client using Kotlin with Ktor networking library

Overview This repository contains an echo server implemented with Kotlin and kto

Elliot Barlas 2 Sep 1, 2022
Android network client based on Cronet. This library let you easily use QUIC protocol in your Android projects

Android network client based on Cronet. This library let you easily use QUIC protocol in your Android projects

VK.com 104 Dec 12, 2022
Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

OkHttp See the project website for documentation and APIs. HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP

Square 43.4k Jan 5, 2023
A type-safe HTTP client for Android and the JVM

Retrofit A type-safe HTTP client for Android and Java. For more information please see the website. Download Download the latest JAR or grab from Mave

Square 41k Jan 5, 2023
Android client for ProjectRTC - a WebRTC demo

AndroidRTC WebRTC Live Streaming An Android client for ProjectRTC. It is designed to demonstrate WebRTC video calls between androids and/or desktop br

Chabardes 1.5k Jan 6, 2023
Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.

AndroidAsync AndroidAsync is a low level network protocol library. If you are looking for an easy to use, higher level, Android aware, http request li

Koushik Dutta 7.3k Jan 2, 2023
An android asynchronous http client built on top of HttpURLConnection.

Versions 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 Version 1.0.6 Description An android asynchronous http client based on HttpURLConnection. Updates U

David 15 Mar 29, 2020