Compare commits

...

2 Commits

Author SHA1 Message Date
82e7b9ecd8 Upgrade Gradle from 8.2.2 to 8.3.0 2024-03-07 23:45:11 +01:00
21f535bd59 Add "org.eclipse.paho" Mqtt Client 2024-03-07 23:33:24 +01:00
6 changed files with 143 additions and 13 deletions

1
.gitignore vendored
View File

@@ -15,6 +15,7 @@ gen/
# Local configuration file (sdk path, etc)
local.properties
mqtt.properties
# Windows thumbnail db
Thumbs.db

View File

@@ -1,8 +1,14 @@
import java.io.FileInputStream
import java.util.Properties
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
val mqttProperties = Properties()
mqttProperties.load(FileInputStream(rootProject.file("mqtt.properties")))
android {
namespace = "de.chrissthecoder.store"
compileSdk = 34
@@ -13,6 +19,11 @@ android {
targetSdk = 34
versionCode = 1
versionName = "1.0"
buildConfigField("String", "MQTT_USERNAME", mqttProperties.getProperty("username"))
buildConfigField("String", "MQTT_PASSWORD", mqttProperties.getProperty("password"))
buildConfigField("String", "MQTT_HOST", mqttProperties.getProperty("host"))
buildConfigField("String", "MQTT_PORT", mqttProperties.getProperty("port"))
}
buildTypes {
@@ -36,6 +47,7 @@ android {
buildFeatures {
viewBinding = true
buildConfig = true
}
}
@@ -48,4 +60,5 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
implementation("androidx.navigation:navigation-fragment-ktx:2.7.7")
implementation("androidx.navigation:navigation-ui-ktx:2.7.7")
implementation("org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5")
}

View File

@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Store"
tools:targetApi="31">
<application android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Store"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
@@ -16,10 +16,8 @@
android:theme="@style/Theme.Store.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,118 @@
package de.chrissthecoder.store.mqtt
import android.util.Log
import de.chrissthecoder.store.BuildConfig
import org.eclipse.paho.client.mqttv3.*
class MqttHandler {
companion object {
private const val LINE = "_________________________"
private const val CONNECTION_TIMEOUT = 3
private const val CONNECTION_KEEP_ALIVE_INTERVAL = 60
private const val CONNECTION_CLEAN_SESSION = true
private const val CONNECTION_RECONNECT = true
}
private val client: MqttClient
init {
Log.d(this.javaClass.name, "Enter Initializer")
val clientId: String = "Test Smartphone"
val serverURL = "tcp://" + BuildConfig.MQTT_HOST + ":" + BuildConfig.MQTT_PORT
client = MqttClient(serverURL, clientId, null)
Log.d(this.javaClass.name, "Set Mqtt Call-Back and Connect")
client.setCallback(object : MqttCallbackExtended {
override fun connectionLost(cause: Throwable?) {
Log.w(this.javaClass.name, "Connection Lost")
}
override fun messageArrived(topic: String?, message: MqttMessage?) {
Log.d(this.javaClass.name, "Message on Topic: \"" + topic + "\" Arrived")
val payload = message?.payload?.toString(Charsets.UTF_8)
}
override fun deliveryComplete(token: IMqttDeliveryToken?) {
Log.d(this.javaClass.name, "Message successful delivered")
}
override fun connectComplete(reconnect: Boolean, serverURI: String?) {
var message = "Successful "
if (reconnect) { message += "re" }
message += "connected to: "
serverURI?.let { Log.d(this.javaClass.name, message + it) }
}
})
Log.d(this.javaClass.name, "MQTT Credentials:")
Log.d(this.javaClass.name, LINE)
Log.d(this.javaClass.name, "Client ID: " + clientId)
Log.d(this.javaClass.name, "Server URL: " + serverURL)
Log.d(this.javaClass.name, "Username: " + BuildConfig.MQTT_USERNAME)
Log.d(this.javaClass.name, "Password: " + BuildConfig.MQTT_PASSWORD)
Log.d(this.javaClass.name, LINE)
connect()
}
private fun connect() {
try {
Log.d(this.javaClass.name, "Connecting to Broker...")
// Set up the connection options
val mqttConnectOptions = MqttConnectOptions()
mqttConnectOptions.isAutomaticReconnect = CONNECTION_RECONNECT
mqttConnectOptions.isCleanSession = CONNECTION_CLEAN_SESSION
mqttConnectOptions.userName = BuildConfig.MQTT_USERNAME
mqttConnectOptions.password = BuildConfig.MQTT_PASSWORD.toCharArray()
mqttConnectOptions.connectionTimeout = CONNECTION_TIMEOUT
mqttConnectOptions.keepAliveInterval = CONNECTION_KEEP_ALIVE_INTERVAL
client.connect(mqttConnectOptions)
} catch (ex: MqttException) {
handleConnectionFailure(ex)
}
}
private fun handleConnectionFailure(cause: Throwable?) {
Log.w(this.javaClass.name,"Failed to connect: ${Log.getStackTraceString(cause)}")
}
fun subscribe(topic: String, qos: Int = 0) {
try {
Log.d(this.javaClass.name, "Subscribe to Topic: $topic")
client.subscribe(topic, qos)
} catch (ex: MqttException) {
Log.e(this.javaClass.name,"Error on subscribing to Topic: $topic", ex)
}
}
fun publish(topic: String, msg: String, qos: Int = 0, retained: Boolean = false) {
try {
val mqttMessage = MqttMessage(msg.toByteArray())
client.publish(topic, mqttMessage.payload, qos, retained)
Log.d(this.javaClass.name, "Message published to topic `$topic`: $msg")
} catch (e: MqttException) {
Log.e(this.javaClass.name, "Error publishing to $topic: " + e.message, e)
}
}
fun disconnect() {
try {
Log.d(this.javaClass.name, "Disconnecting from Broker...")
client.disconnect()
} catch (e: MqttException) {
Log.e(this.javaClass.name, "Error disconnection from Broker: " + e.message, e)
e.printStackTrace()
}
}
fun isConnected(): Boolean {
return client.isConnected
}
}

View File

@@ -1,5 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.2.2" apply false
id("com.android.application") version "8.3.0" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}

View File

@@ -1,6 +1,6 @@
#Thu Mar 07 13:51:32 CET 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists