Difference between revisions of "MQTT: Android Client Simple"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | '''PROBLEM:''' class startMqtt(); | |
− | |||
− | + | ==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 97: | ||
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 109: | ||
@Override | @Override | ||
− | public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { | + | public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { |
− | |||
} | } | ||
}); | }); | ||
Line 122: | Line 124: | ||
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 146: | ||
} | } | ||
}); | }); | ||
− | + | ||
} catch (MqttException ex){ | } catch (MqttException ex){ | ||
ex.printStackTrace(); | ex.printStackTrace(); | ||
} | } | ||
} | } | ||
− | |||
private void subscribeToTopic() { | private void subscribeToTopic() { | ||
Line 158: | Line 159: | ||
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 170: | Line 171: | ||
} | } | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | + | ==MainActivity.java== | |
− | |||
− | |||
− | |||
− | + | package itts.onno.ittsmqttwildan; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | import androidx.appcompat.app.AppCompatActivity; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | import android.os.Bundle; | |
− | + | import android.util.Log; | |
− | + | import android.widget.TextView; | |
− | |||
− | + | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | |
− | + | import org.eclipse.paho.client.mqttv3.MqttCallbackExtended; | |
− | + | import org.eclipse.paho.client.mqttv3.MqttMessage; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | import | + | import itts.onno.ittsmqttwildan.helpers.MqttHelper; |
− | |||
public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity { | ||
MqttHelper mqttHelper; | MqttHelper mqttHelper; | ||
− | |||
− | |||
− | |||
TextView dataReceived; | TextView dataReceived; | ||
@Override | @Override | ||
− | |||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||
− | setContentView(R.layout.activity_main); | + | setContentView(R.layout.activity_main); |
dataReceived = (TextView) findViewById(R.id.dataReceived); | dataReceived = (TextView) findViewById(R.id.dataReceived); | ||
− | |||
− | |||
− | startMqtt(); | + | // ------- problem & crashing------------- |
+ | // startMqtt(); | ||
} | } | ||
private void startMqtt(){ | private void startMqtt(){ | ||
mqttHelper = new MqttHelper(getApplicationContext()); | mqttHelper = new MqttHelper(getApplicationContext()); | ||
− | mqttHelper | + | mqttHelper.setCallback(new MqttCallbackExtended() { |
@Override | @Override | ||
public void connectComplete(boolean b, String s) { | public void connectComplete(boolean b, String s) { | ||
− | + | ||
} | } | ||
Line 502: | Line 227: | ||
Log.w("Debug",mqttMessage.toString()); | Log.w("Debug",mqttMessage.toString()); | ||
dataReceived.setText(mqttMessage.toString()); | dataReceived.setText(mqttMessage.toString()); | ||
− | |||
} | } | ||
Line 509: | Line 233: | ||
} | } | ||
− | + | }); | |
− | + | } | |
− | |||
− | + | ||
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 529: | Line 245: | ||
==Referensi== | ==Referensi== | ||
+ | * https://github.com/wildan2711/mqtt-android-tutorial | ||
* https://wildanmsyah.wordpress.com/2017/05/11/mqtt-android-client-tutorial/ | * https://wildanmsyah.wordpress.com/2017/05/11/mqtt-android-client-tutorial/ | ||
* https://github.com/wildan2711/mqtt-android-tutorial | * https://github.com/wildan2711/mqtt-android-tutorial | ||
* https://medium.com/swlh/android-and-mqtt-a-simple-guide-cb0cbba1931c | * https://medium.com/swlh/android-and-mqtt-a-simple-guide-cb0cbba1931c | ||
* https://www.eclipse.org/paho/index.php?page=clients/android/index.php | * https://www.eclipse.org/paho/index.php?page=clients/android/index.php |
Latest revision as of 09:53, 9 April 2022
PROBLEM: class startMqtt();
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(); } } }
MainActivity.java
package itts.onno.ittsmqttwildan; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttCallbackExtended; import org.eclipse.paho.client.mqttv3.MqttMessage; import itts.onno.ittsmqttwildan.helpers.MqttHelper; public class MainActivity extends AppCompatActivity { MqttHelper mqttHelper; TextView dataReceived; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dataReceived = (TextView) findViewById(R.id.dataReceived); // ------- problem & crashing------------- // startMqtt(); } private void startMqtt(){ mqttHelper = new MqttHelper(getApplicationContext()); mqttHelper.setCallback(new MqttCallbackExtended() { @Override public void connectComplete(boolean b, String s) { } @Override public void connectionLost(Throwable throwable) { } @Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { Log.w("Debug",mqttMessage.toString()); dataReceived.setText(mqttMessage.toString()); } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { } }); }
}
Referensi
- https://github.com/wildan2711/mqtt-android-tutorial
- https://wildanmsyah.wordpress.com/2017/05/11/mqtt-android-client-tutorial/
- https://github.com/wildan2711/mqtt-android-tutorial
- https://medium.com/swlh/android-and-mqtt-a-simple-guide-cb0cbba1931c
- https://www.eclipse.org/paho/index.php?page=clients/android/index.php