Difference between revisions of "MQTT: Android Client Example"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) (Created page with " It also contain one textView that will subscribe and show the message received from the MQTT service. * When click button Publish, you will see the “the payload” in the...") |
Onnowpurbo (talk | contribs) |
||
Line 1: | Line 1: | ||
+ | ==settings.gradle== | ||
− | + | pluginManagement { | |
− | + | repositories { | |
− | + | gradlePluginPortal() | |
− | + | google() | |
+ | mavenCentral() | ||
+ | maven { | ||
+ | url "https://repo.eclipse.org/content/repositories/paho-releases/" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | dependencyResolutionManagement { | ||
+ | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) | ||
+ | repositories { | ||
+ | google() | ||
+ | mavenCentral() | ||
+ | maven { | ||
+ | url "https://repo.eclipse.org/content/repositories/paho-releases/" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | rootProject.name = "ITTSPahoMQTT" | ||
+ | include ':app' | ||
− | + | ==build.gradle :app== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | dependencies { | ||
+ | ..... | ||
implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') { | implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') { | ||
exclude module: 'support-v4' | exclude module: 'support-v4' | ||
} | } | ||
− | |||
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' | implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' | ||
} | } | ||
Line 34: | Line 39: | ||
− | |||
− | |||
− | + | ==AndroidManifest.xml== | |
− | |||
− | |||
− | <uses-permission android:name="android.permission.WAKE_LOCK" /> | + | <?xml version="1.0" encoding="utf-8"?> |
− | + | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
− | + | package="itts.onno.ittspahomqtt"> | |
− | + | ||
+ | <uses-permission android:name="android.permission.WAKE_LOCK" /> | ||
+ | <uses-permission android:name="android.permission.INTERNET" /> | ||
+ | <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
+ | <uses-permission android:name="android.permission.READ_PHONE_STATE" /> | ||
+ | |||
+ | <application | ||
+ | ..... | ||
+ | <service android:name="org.eclipse.paho.android.service.MqttService" > | ||
+ | </service> | ||
+ | </application> | ||
+ | </manifest> | ||
− | + | ==activity_main.xml== | |
− | |||
− | |||
− | |||
<?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||
Line 93: | Line 102: | ||
app:layout_constraintHorizontal_bias="0.201" | app:layout_constraintHorizontal_bias="0.201" | ||
app:layout_constraintStart_toStartOf="parent" | app:layout_constraintStart_toStartOf="parent" | ||
− | app:layout_constraintTop_toTopOf="parent" /> | + | app:layout_constraintTop_toTopOf="parent" /> |
<Button | <Button | ||
Line 105: | Line 114: | ||
app:layout_constraintHorizontal_bias="0.78" | app:layout_constraintHorizontal_bias="0.78" | ||
app:layout_constraintStart_toStartOf="parent" | app:layout_constraintStart_toStartOf="parent" | ||
− | app:layout_constraintTop_toTopOf="parent" /> | + | app:layout_constraintTop_toTopOf="parent" /> |
</androidx.constraintlayout.widget.ConstraintLayout> | </androidx.constraintlayout.widget.ConstraintLayout> | ||
Line 111: | Line 120: | ||
+ | ==MainActivity.java== | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Revision as of 10:52, 4 April 2022
settings.gradle
pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() maven { url "https://repo.eclipse.org/content/repositories/paho-releases/" } } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url "https://repo.eclipse.org/content/repositories/paho-releases/" } } } rootProject.name = "ITTSPahoMQTT" include ':app'
build.gradle :app
dependencies { ..... implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') { exclude module: 'support-v4' } implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="itts.onno.ittspahomqtt"> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application ..... <service android:name="org.eclipse.paho.android.service.MqttService" > </service> </application> </manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="236dp" android:onClick="published" android:text="PUBLISH" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/subText" android:layout_width="141dp" android:layout_height="68dp" android:layout_marginTop="360dp" android:text="TextView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/connBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="524dp" android:onClick="conn" android:text="connect" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.201" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/disconnBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="524dp" android:onClick="disconn" android:text="disconnect" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.78" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>