java - Paho MQTT cleanSession set to false yet not receiving messages -


i testing mqtt project. able receive messages on topic client had subscribed when client connected. have set qos 1 , cleansession set false. unable receive messages sent subscribed topic when client connects again. in application work done helper service.

here code

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.access_network_state" />  <application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:theme="@style/apptheme" >     <activity         android:name=".mainactivity"         android:label="@string/app_name"         android:screenorientation="portrait" >         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity>      <service         android:name=".mqtthelperservice"         android:enabled="true"         android:exported="true" />      <!-- mqttservice -->     <service android:name="org.eclipse.paho.android.service.mqttservice" /> </application> 

mainactivity.java

package com.prateek.mqtttest;  import android.app.activity; import android.content.intent; import android.os.bundle;  public class mainactivity extends activity {     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         startservice(new intent(getbasecontext(), mqtthelperservice.class));     } } 

mqtthelperservice.java

package com.prateek.mqtttest;  import android.app.service; import android.content.intent; import android.os.binder; import android.os.ibinder; import android.widget.toast;  import org.eclipse.paho.android.service.mqttandroidclient; 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.mqttcallback; import org.eclipse.paho.client.mqttv3.mqttconnectoptions; import org.eclipse.paho.client.mqttv3.mqttexception; import org.eclipse.paho.client.mqttv3.mqttmessage;  public class mqtthelperservice extends service implements mqttcallback {      private static final string mqtt_uri = "tcp://broker.mqttdashboard.com:1883";     private static final string client_id = "prateek";     private static final string mqtt_topic = "mqttmessenger";     private static final int qos = 1;     private mqttandroidclient client;      public mqtthelperservice() {     }      @override     public int onstartcommand(intent intent, int flags, int startid) {         toast.maketext(this, "mqtt helper service started", toast.length_short).show();         new thread(new runnable() {             @override             public void run() {                 connect();             }         }, "mqtthelperservice").start();         return start_sticky;     }      public class mqtthelperbinder extends binder {         public mqtthelperservice getservice(){             return mqtthelperservice.this;         }     }      public void connect() {         client = new mqttandroidclient(this, mqtt_uri, client_id);         client.setcallback(this);          try {             mqttconnectoptions options = new mqttconnectoptions();             options.setcleansession(false);             client.connect(options, new imqttactionlistener() {                 @override                 public void onsuccess(imqtttoken imqtttoken) {                     toast.maketext(getbasecontext(), "connected mqtt broker", toast.length_short).show();                     subscribe();                 }                  @override                 public void onfailure(imqtttoken imqtttoken, throwable throwable) {                     toast.maketext(getbasecontext(), "failed connect: " + throwable.getmessage(), toast.length_short).show();                 }             });         } catch (mqttexception e) {             toast.maketext(this, "could not connect mqtt broker @ " + mqtt_uri, toast.length_short).show();         }     }      public void subscribe() {         try {             imqtttoken token = client.subscribe(mqtt_topic, qos, null, new imqttactionlistener() {                 @override                 public void onsuccess(imqtttoken imqtttoken) {                     toast.maketext(getbasecontext(), "subscription successful", toast.length_short).show();                 }                  @override                 public void onfailure(imqtttoken imqtttoken, throwable throwable) {                     toast.maketext(getbasecontext(), "subscription failed: " + throwable, toast.length_short).show();                 }             });          } catch (mqttexception e) {             toast.maketext(this, "could not subscribe", toast.length_short).show();         }     }      @override     public ibinder onbind(intent intent) {         // todo: return communication channel service.         throw new unsupportedoperationexception("not yet implemented");     }      @override     public void connectionlost(throwable throwable) {         toast.maketext(this, "connection lost", toast.length_short).show();     }      @override     public void messagearrived(string s, mqttmessage mqttmessage) throws exception {         toast.maketext(this, "message received on topic " + s, toast.length_short).show();     }      @override     public void deliverycomplete(imqttdeliverytoken imqttdeliverytoken) {     }      @override     public void ondestroy() {         super.ondestroy();         toast.maketext(this, "service destroyed", toast.length_short).show();     }   } 

i checked link with clear session flag set false, missing published values not find error in code

i've had same problem recently. now think solution simple, i've spent many many hours figure out.

this line 'bad':

client.connect(mqttoptions, mqqtactionlistener); 

the 'correct' is:

client.connect(mqttoptions, null, mqqtactionlistener); 

if call connect method 2 parameters, using constructor:

public imqtttoken connect(object usercontext, imqttactionlistener callback) throws mqttexception 

instead of right one:

public imqtttoken connect(mqttconnectoptions options, object usercontext, imqttactionlistener callback) throws mqttexception 

i hope problem too.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -