Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cl.inacap.antofagasta.ejemploappmtqq;
- 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://m14.cloudmqtt.com:12750";
- final String clientId = "ExampleAndroidClient";
- final String subscriptionTopic1 = "sensor/temp";
- final String subscriptionTopic2 = "sensor/hum";
- final String username = "icfgwkgf";
- final String password = "Id-MlBDw_1N4";
- 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(subscriptionTopic1);
- subscribeToTopic(subscriptionTopic2);
- }
- @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(String topic) {
- try {
- mqttAndroidClient.subscribe(topic, 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("Exceptionst subscribing");
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement