Created
November 29, 2018 13:02
-
-
Save hussanhijazi/4fd7c737ccb4f1006ad2e36f3108ddcc to your computer and use it in GitHub Desktop.
Kotlin Mqtt Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.hussan.mqttandroid | |
import android.content.Context | |
import android.util.Log | |
import org.eclipse.paho.android.service.MqttAndroidClient | |
import org.eclipse.paho.client.mqttv3.IMqttActionListener | |
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken | |
import org.eclipse.paho.client.mqttv3.IMqttToken | |
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended | |
import org.eclipse.paho.client.mqttv3.MqttClient | |
import org.eclipse.paho.client.mqttv3.MqttException | |
import org.eclipse.paho.client.mqttv3.MqttMessage | |
class MqttClient(private val context: Context) { | |
val client by lazy { | |
val clientId = MqttClient.generateClientId() | |
MqttAndroidClient(context, "tcp://iot.eclipse.org:1883", | |
clientId) | |
} | |
companion object { | |
const val TAG = "MqttClient" | |
} | |
fun connect(topics: Array<String>? = null, | |
messageCallBack: ((topic: String, message: MqttMessage) -> Unit)? = null) { | |
try { | |
client.connect() | |
client.setCallback(object : MqttCallbackExtended { | |
override fun connectComplete(reconnect: Boolean, serverURI: String) { | |
topics?.forEach { | |
subscribeTopic(it) | |
} | |
Log.d(TAG, "Connected to: $serverURI") | |
} | |
override fun connectionLost(cause: Throwable) { | |
Log.d(TAG, "The Connection was lost.") | |
} | |
@Throws(Exception::class) | |
override fun messageArrived(topic: String, message: MqttMessage) { | |
Log.d(TAG, "Incoming message from $topic: " + message.toString()) | |
messageCallBack?.invoke(topic, message) | |
} | |
override fun deliveryComplete(token: IMqttDeliveryToken) { | |
} | |
}) | |
} catch (e: MqttException) { | |
e.printStackTrace() | |
} | |
} | |
fun publishMessage(topic: String, msg: String) { | |
try { | |
val message = MqttMessage() | |
message.payload = msg.toByteArray() | |
client.publish(topic, message.payload, 0, true) | |
Log.d(TAG, "$msg published to $topic") | |
} catch (e: MqttException) { | |
Log.d(TAG, "Error Publishing to $topic: " + e.message) | |
e.printStackTrace() | |
} | |
} | |
fun subscribeTopic(topic: String, qos: Int = 0) { | |
client.subscribe(topic, qos).actionCallback = object : IMqttActionListener { | |
override fun onSuccess(asyncActionToken: IMqttToken) { | |
Log.d(TAG, "Subscribed to $topic") | |
} | |
override fun onFailure(asyncActionToken: IMqttToken, exception: Throwable) { | |
Log.d(TAG, "Failed to subscribe to $topic") | |
exception.printStackTrace() | |
} | |
} | |
} | |
fun close() { | |
client.apply { | |
unregisterResources() | |
close() | |
} | |
} | |
} |
Hi, Alepazz. Thanks for yout comment.
You can instantiate the MqttClient like this:
https://github.com/hussanhijazi/coffee-iot/blob/403b5945406a38d537b0fc6f19f8c6d551af5253/app/src/main/java/br/com/hussan/coffeeiot/ui/coffee/CoffeeActivity.kt#L29
https://github.com/hussanhijazi/mqtt-android/blob/9c948d71ac3dac83f4204cfaf8dac8a92ade8c3e/app/src/main/java/br/com/hussan/mqttandroid/SensorActivity.kt#L21
We need the context to intatiate the MqttAndroidClient
from org.eclipse.paho
library
Thanks
thx for this class; it helped me to get started
FYI, I don't believe as is it will compile since MqttClient.generateClientId()
I had to create it which is not a big a deal. I had to return any client id
Many thx again; very helpful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! I have a question for you, if you can help me. How can I instantiate the class MqttClient with a context? I'm not able to understand what is a context and which is the meaning of that parameter.
Thanks you a lot!