Compare commits
2 Commits
4eb15845b3
...
82e7b9ecd8
| Author | SHA1 | Date | |
|---|---|---|---|
| 82e7b9ecd8 | |||
| 21f535bd59 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -15,6 +15,7 @@ gen/
|
|||||||
|
|
||||||
# Local configuration file (sdk path, etc)
|
# Local configuration file (sdk path, etc)
|
||||||
local.properties
|
local.properties
|
||||||
|
mqtt.properties
|
||||||
|
|
||||||
# Windows thumbnail db
|
# Windows thumbnail db
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
|
import java.io.FileInputStream
|
||||||
|
import java.util.Properties
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id("com.android.application")
|
id("com.android.application")
|
||||||
id("org.jetbrains.kotlin.android")
|
id("org.jetbrains.kotlin.android")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val mqttProperties = Properties()
|
||||||
|
mqttProperties.load(FileInputStream(rootProject.file("mqtt.properties")))
|
||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "de.chrissthecoder.store"
|
namespace = "de.chrissthecoder.store"
|
||||||
compileSdk = 34
|
compileSdk = 34
|
||||||
@@ -13,6 +19,11 @@ android {
|
|||||||
targetSdk = 34
|
targetSdk = 34
|
||||||
versionCode = 1
|
versionCode = 1
|
||||||
versionName = "1.0"
|
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 {
|
buildTypes {
|
||||||
@@ -36,6 +47,7 @@ android {
|
|||||||
|
|
||||||
buildFeatures {
|
buildFeatures {
|
||||||
viewBinding = true
|
viewBinding = true
|
||||||
|
buildConfig = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,4 +60,5 @@ dependencies {
|
|||||||
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
|
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
|
||||||
implementation("androidx.navigation:navigation-fragment-ktx:2.7.7")
|
implementation("androidx.navigation:navigation-fragment-ktx:2.7.7")
|
||||||
implementation("androidx.navigation:navigation-ui-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")
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<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
|
<application android:icon="@mipmap/ic_launcher"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:label="@string/app_name"
|
||||||
android:label="@string/app_name"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:supportsRtl="true"
|
||||||
android:supportsRtl="true"
|
android:theme="@style/Theme.Store"
|
||||||
android:theme="@style/Theme.Store"
|
tools:targetApi="31">
|
||||||
tools:targetApi="31">
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
@@ -16,10 +16,8 @@
|
|||||||
android:theme="@style/Theme.Store.NoActionBar">
|
android:theme="@style/Theme.Store.NoActionBar">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
118
app/src/main/java/de/chrissthecoder/store/mqtt/MqttHandler.kt
Normal file
118
app/src/main/java/de/chrissthecoder/store/mqtt/MqttHandler.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||||
plugins {
|
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
|
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
|
||||||
}
|
}
|
||||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
|||||||
#Thu Mar 07 13:51:32 CET 2024
|
#Thu Mar 07 13:51:32 CET 2024
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
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
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|||||||
Reference in New Issue
Block a user