I am making a simple android app in Kotlin. I wish to use hivemq to publish messages which will be picked up by an ESP 8266. I found a library and adapted the sample code from this page: https://hivemq.github.io/hivemq-mqtt-client/docs/quick-start/ .
The problem is that the debug log message never prints, which leads to the conclusion that whenComplete never executes. It is my first time using Kotlin. Any clues would be helpful.
Thanks!
My code at this point is:
val builder = MqttClient.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("server.hivemq.cloud")
.serverPort(8883)
.sslWithDefaultConfig();
val client = builder.useMqttVersion5().buildAsync();
client.connectWith()
.simpleAuth()
.username("username")
.password("password".toByteArray())
.applySimpleAuth()
.send()
.whenComplete { connAck, throwable ->
run {
Log.d("TAG", "Some debug string");
if (throwable != null) {
Toast.makeText(
context,
"Unable to connect to HiveMQ servers!!",
Toast.LENGTH_LONG
).show();
} else {
client.publishWith()
.topic("my/topic")
.payload("ChopChop".toByteArray())
.send()
.whenComplete { publish, throwable ->
run {
if(throwable != null) {
Toast.makeText(context, "Unable to publish message!", Toast.LENGTH_LONG).show();
}
client.disconnect();
}
}
}
}
}
It's always exciting seeing folks interested in MQTT. Based on what you've stated here, it sounds like our Log is not being printed.
I'd first like to have you make sure that Log has been imported for this implementation.
import android.util.LogAdditionally, your format here for the Log.d usage may be a little off - this syntax should better match Log.d usage requirements.
Log.d(TAG, msg="Log message here")As an aside, I also highly recommend the HiveMQ Community Forum as a resource for client implementation questions - full disclosure, I am a member of the HiveMQ team. The community forum can be found here.
Best, Aaron from the HiveMQ Team