Difference between revisions of "MQTT: Android Client Simple"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
Line 1: | Line 1: | ||
− | + | ==settings.gradle== | |
− | + | pluginManagement { | |
− | + | repositories { | |
− | + | gradlePluginPortal() | |
− | + | google() | |
− | + | mavenCentral() | |
− | + | maven { | |
− | + | url "https://repo.eclipse.org/content/repositories/paho-snapshots/" | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
− | + | dependencyResolutionManagement { | |
− | + | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) | |
− | + | repositories { | |
− | + | google() | |
− | + | mavenCentral() | |
− | + | maven { | |
+ | url "https://repo.eclipse.org/content/repositories/paho-snapshots/" | ||
+ | } | ||
+ | } | ||
} | } | ||
+ | rootProject.name = "ITTSMQTTWildan" | ||
+ | include ':app' | ||
− | |||
− | |||
− | + | ==build.gradle (app)== | |
− | |||
− | + | .... | |
+ | dependencies { | ||
+ | ..... | ||
+ | implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' | ||
+ | implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' | ||
+ | } | ||
− | |||
− | + | ==main_activity.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"> | ||
+ | |||
+ | <TextView | ||
+ | android:id="@+id/dataReceived" | ||
+ | android:layout_width="wrap_content" | ||
+ | android:layout_height="wrap_content" | ||
+ | app:layout_constraintBottom_toBottomOf="parent" | ||
+ | app:layout_constraintLeft_toLeftOf="parent" | ||
+ | app:layout_constraintRight_toRightOf="parent" | ||
+ | app:layout_constraintTop_toTopOf="parent" /> | ||
+ | |||
+ | </androidx.constraintlayout.widget.ConstraintLayout> | ||
− | |||
− | |||
− | + | ==Package helpers== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | package itts.onno.ittsmqttwildan.helpers; | ||
+ | |||
+ | import android.content.Context; | ||
+ | import android.util.Log; | ||
+ | |||
+ | import org.eclipse.paho.android.service.MqttAndroidClient; | ||
+ | import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions; | ||
+ | import org.eclipse.paho.client.mqttv3.IMqttActionListener; | ||
+ | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | ||
+ | import org.eclipse.paho.client.mqttv3.IMqttToken; | ||
+ | import org.eclipse.paho.client.mqttv3.MqttCallbackExtended; | ||
+ | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; | ||
+ | import org.eclipse.paho.client.mqttv3.MqttException; | ||
+ | import org.eclipse.paho.client.mqttv3.MqttMessage; | ||
+ | |||
public class MqttHelper { | public class MqttHelper { | ||
public MqttAndroidClient mqttAndroidClient; | public MqttAndroidClient mqttAndroidClient; | ||
− | final String serverUri = "tcp:// | + | final String serverUri = "tcp://192.168.0.111:1883"; |
final String clientId = "ExampleAndroidClient"; | final String clientId = "ExampleAndroidClient"; | ||
− | final String subscriptionTopic = " | + | final String subscriptionTopic = "event"; |
− | final String username = " | + | final String username = "test"; |
− | final String password = " | + | final String password = "123456"; |
− | public MqttHelper(Context context){ | + | public MqttHelper(Context context) { |
mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId); | mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId); | ||
mqttAndroidClient.setCallback(new MqttCallbackExtended() { | mqttAndroidClient.setCallback(new MqttCallbackExtended() { | ||
Line 93: | Line 94: | ||
public void connectComplete(boolean b, String s) { | public void connectComplete(boolean b, String s) { | ||
Log.w("mqtt", s); | Log.w("mqtt", s); | ||
− | } | + | } |
@Override | @Override | ||
public void connectionLost(Throwable throwable) { | public void connectionLost(Throwable throwable) { | ||
− | |||
} | } | ||
Line 106: | Line 106: | ||
@Override | @Override | ||
− | public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { | + | public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { |
− | |||
} | } | ||
}); | }); | ||
Line 122: | Line 121: | ||
mqttConnectOptions.setCleanSession(false); | mqttConnectOptions.setCleanSession(false); | ||
mqttConnectOptions.setUserName(username); | mqttConnectOptions.setUserName(username); | ||
− | mqttConnectOptions.setPassword(password.toCharArray()); | + | mqttConnectOptions.setPassword(password.toCharArray()); |
try { | try { | ||
Line 144: | Line 143: | ||
} | } | ||
}); | }); | ||
− | + | ||
} catch (MqttException ex){ | } catch (MqttException ex){ | ||
ex.printStackTrace(); | ex.printStackTrace(); | ||
} | } | ||
} | } | ||
− | |||
private void subscribeToTopic() { | private void subscribeToTopic() { | ||
Line 158: | Line 156: | ||
Log.w("Mqtt","Subscribed!"); | Log.w("Mqtt","Subscribed!"); | ||
} | } | ||
− | + | ||
@Override | @Override | ||
public void onFailure(IMqttToken asyncActionToken, Throwable exception) { | public void onFailure(IMqttToken asyncActionToken, Throwable exception) { | ||
Log.w("Mqtt", "Subscribed fail!"); | Log.w("Mqtt", "Subscribed fail!"); | ||
} | } | ||
− | }); | + | }); |
} catch (MqttException ex) { | } catch (MqttException ex) { | ||
Line 169: | Line 167: | ||
ex.printStackTrace(); | ex.printStackTrace(); | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Revision as of 13:43, 4 April 2022
settings.gradle
pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" } } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" } } } rootProject.name = "ITTSMQTTWildan" include ':app'
build.gradle (app)
.... dependencies { ..... implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' }
main_activity.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"> <TextView android:id="@+id/dataReceived" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Package helpers
package itts.onno.ittsmqttwildan.helpers; import android.content.Context; import android.util.Log; import org.eclipse.paho.android.service.MqttAndroidClient; import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions; import org.eclipse.paho.client.mqttv3.IMqttActionListener; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.IMqttToken; import org.eclipse.paho.client.mqttv3.MqttCallbackExtended; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; public class MqttHelper { public MqttAndroidClient mqttAndroidClient; final String serverUri = "tcp://192.168.0.111:1883"; final String clientId = "ExampleAndroidClient"; final String subscriptionTopic = "event"; final String username = "test"; final String password = "123456"; public MqttHelper(Context context) { mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId); mqttAndroidClient.setCallback(new MqttCallbackExtended() { @Override public void connectComplete(boolean b, String s) { Log.w("mqtt", s); } @Override public void connectionLost(Throwable throwable) { } @Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { Log.w("Mqtt", mqttMessage.toString()); } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { } }); connect(); } public void setCallback(MqttCallbackExtended callback) { mqttAndroidClient.setCallback(callback); } private void connect(){ MqttConnectOptions mqttConnectOptions = new MqttConnectOptions(); mqttConnectOptions.setAutomaticReconnect(true); mqttConnectOptions.setCleanSession(false); mqttConnectOptions.setUserName(username); mqttConnectOptions.setPassword(password.toCharArray()); try { mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions(); disconnectedBufferOptions.setBufferEnabled(true); disconnectedBufferOptions.setBufferSize(100); disconnectedBufferOptions.setPersistBuffer(false); disconnectedBufferOptions.setDeleteOldestMessages(false); mqttAndroidClient.setBufferOpts(disconnectedBufferOptions); subscribeToTopic(); } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { Log.w("Mqtt", "Failed to connect to: " + serverUri + exception.toString()); } }); } catch (MqttException ex){ ex.printStackTrace(); } } private void subscribeToTopic() { try { mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { Log.w("Mqtt","Subscribed!"); } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { Log.w("Mqtt", "Subscribed fail!"); } }); } catch (MqttException ex) { System.err.println("Exception whilst subscribing"); ex.printStackTrace(); } } }