diff --git a/MqttLibrary/build.gradle b/MqttLibrary/build.gradle index f2a616d..fe2c2dd 100644 --- a/MqttLibrary/build.gradle +++ b/MqttLibrary/build.gradle @@ -18,7 +18,7 @@ plugins { } group 'com.craxiom' -version '0.5.0' +version '0.6.0' android { compileSdkVersion 33 @@ -27,7 +27,7 @@ android { defaultConfig { minSdkVersion 24 targetSdkVersion 33 - versionCode 11 + versionCode 12 versionName version setProperty("archivesBaseName", "$applicationName-$versionName") diff --git a/MqttLibrary/src/main/java/com/craxiom/mqttlibrary/connection/DefaultMqttConnection.java b/MqttLibrary/src/main/java/com/craxiom/mqttlibrary/connection/DefaultMqttConnection.java index c59f074..751e7f7 100644 --- a/MqttLibrary/src/main/java/com/craxiom/mqttlibrary/connection/DefaultMqttConnection.java +++ b/MqttLibrary/src/main/java/com/craxiom/mqttlibrary/connection/DefaultMqttConnection.java @@ -8,11 +8,11 @@ import com.craxiom.mqttlibrary.IConnectionStateListener; import com.craxiom.mqttlibrary.IMqttService; import com.craxiom.mqttlibrary.R; +import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.MessageOrBuilder; import com.google.protobuf.util.JsonFormat; import com.hivemq.client.internal.mqtt.lifecycle.mqtt3.Mqtt3ClientDisconnectedContextView; import com.hivemq.client.mqtt.datatypes.MqttQos; -import com.hivemq.client.mqtt.exceptions.ConnectionFailedException; import com.hivemq.client.mqtt.lifecycle.MqttDisconnectSource; import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient; import com.hivemq.client.mqtt.mqtt3.Mqtt3Client; @@ -23,7 +23,6 @@ import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck; import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAckReturnCode; -import java.net.UnknownHostException; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; @@ -249,18 +248,29 @@ protected void publishMessage(String mqttMessageTopic, MessageOrBuilder message) { try { - final String messageJson = jsonFormatter.print(message); - - if (mqtt3Client.getState().isConnectedOrReconnect()) - { - mqtt3Client.publishWith().topic(mqttMessageTopic).qos(MqttQos.AT_LEAST_ONCE).payload(messageJson.getBytes()).send(); - } - } catch (Exception e) + final String jsonMessage = jsonFormatter.print(message); + publishMessage(mqttMessageTopic, jsonMessage); + } catch (InvalidProtocolBufferException e) { Timber.e(e, "Caught an exception when trying to send an MQTT message"); } } + /** + * Publishes the JSON string to the specified topic. + * + * @param mqttMessageTopic The MQTT topic to publish the message to. + * @param jsonMessage The JSON string to send to the MQTT broker. + * @since 0.6.0 + */ + protected void publishMessage(String mqttMessageTopic, String jsonMessage) + { + if (mqtt3Client.getState().isConnectedOrReconnect()) + { + mqtt3Client.publishWith().topic(mqttMessageTopic).qos(MqttQos.AT_LEAST_ONCE).payload(jsonMessage.getBytes()).send(); + } + } + /** * Adds an {@link IConnectionStateListener} so that it will be notified of all future MQTT connection state changes. * diff --git a/README.md b/README.md index 4c030de..ce336a3 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ It is important to note that the pieces provided in the library cannot stand on ## Change log +##### 0.6.0 - 2023-07-17 +* Overload the publishMessage method to enable sending plain JSON strings. + ##### [0.5.0](https://github.com/christianrowlands/android-mqtt-connection-lib/releases/tag/v0.5.0) - 2022-10-25 * Change the onMdmOverride method to protected so it can be overridden