Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Can I trust MQTT when phone is in power safe mode
Can I trust MQTT when phone is in power safe mode [message #1821809] Thu, 20 February 2020 13:16
column column is currently offline column columnFriend
Messages: 21
Registered: December 2017
Junior Member
I have simple Paho MQTT Android client application. Everything works fine when phone is connected to charger or active. My application miss MQTT messages when phone is disconnected from charger and is closed (I call it power safe mode). In this situation after some time phone starts to miss MQTT messages. According to log taken from phone we can see that messages appear when phone is waked up:


Thu Feb 20 2020 at 02:41:27:776 pm  Message:  aaa/ : 2
Thu Feb 20 2020 at 02:41:49:537 pm  Message:  aaa/ : 3
Thu Feb 20 2020 at 02:44:26:972 pm  Message:  aaa/ : 2
Thu Feb 20 2020 at 02:44:47:913 pm  Message:  aaa/ : 3
Thu Feb 20 2020 at 02:45:20:876 pm  Message:  aaa/ : 4
Thu Feb 20 2020 at 02:46:01:322 pm  Message:  aaa/ : 5
Thu Feb 20 2020 at 02:46:52:873 pm  Message:  aaa/ : 6
Thu Feb 20 2020 at 02:47:09:993 pm  The Connection was lost.
Thu Feb 20 2020 at 02:54:44:263 pm  Reconnected to : ssl://myserver:8887
Thu Feb 20 2020 at 02:54:44:357 pm  Subscribed!
Thu Feb 20 2020 at 02:54:48:196 pm  MainActivity.onStart
Thu Feb 20 2020 at 02:55:28:465 pm  MainActivity.onStop
Thu Feb 20 2020 at 02:55:33:080 pm  Message:  aaa/ : 12
Thu Feb 20 2020 at 02:57:35:070 pm  Message:  aaa/ : 13
Thu Feb 20 2020 at 02:58:30:264 pm  The Connection was lost.
Thu Feb 20 2020 at 03:02:54:001 pm  Reconnected to : ssl://myserver:8887
Thu Feb 20 2020 at 03:02:54:103 pm  Subscribed!


Messages 7-11 just not reached my device. How to solve this problem?

Application:

public class PahoExampleActivity extends AppCompatActivity {


    MqttAndroidClient mqttAndroidClient;

    final String serverUri = "ssl://myserver:8887";

    String clientId = "ExampleAndroidClient";
    final String subscriptionTopic = "aaa/";
    final String publishTopic = "exampleAndroidPublishTopic";






    u/Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Timber.plant(new  Timber.DebugTree());
        Timber.plant(new FileLoggingTree(this));
        Timber.tag(Utils.TIMBER_TAG).v("starting");


        setContentView(R.layout.activity_main);

        mqttAndroidClient = new MqttAndroidClient(getApplicationContext(), serverUri, clientId);
        mqttAndroidClient.setCallback(new MqttCallbackExtended() {
            u/Override
            public void connectComplete(boolean reconnect, String serverURI) {

                if (reconnect) {
                    Timber.tag(Utils.TIMBER_TAG).v("Reconnected to : " + serverURI);
                    // Because Clean Session is true, we need to re-subscribe
                    subscribeToTopic();
                } else {
                    Timber.tag(Utils.TIMBER_TAG).v("Connected to: " + serverURI);

                }
            }

            u/Override
            public void connectionLost(Throwable cause) {
                Timber.tag(Utils.TIMBER_TAG).v("The Connection was lost.");

            }

            u/Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {
                Timber.tag(Utils.TIMBER_TAG).v("Incoming message: " + new String(message.getPayload()));
            }

            u/Override
            public void deliveryComplete(IMqttDeliveryToken token) {

            }
        });

        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setAutomaticReconnect(true);
        mqttConnectOptions.setCleanSession(false);

        mqttConnectOptions.setKeepAliveInterval(300);
        mqttConnectOptions.setUserName("a");
        mqttConnectOptions.setPassword("a".toCharArray());


        try {
            mqttConnectOptions.setSocketFactory(SocketFactoryMQ.getSocketFactory(this,""));
        } catch (KeyStoreException e) {
            Timber.e ( e);

        } catch (NoSuchAlgorithmException e) {
            Timber.tag(Utils.TIMBER_TAG).e ( e);
        } catch (IOException e) {
            Timber.tag(Utils.TIMBER_TAG).e ( e);
        } catch (KeyManagementException e) {
            Timber.tag(Utils.TIMBER_TAG).e ( e);
        } catch (CertificateException e) {
            Timber.tag(Utils.TIMBER_TAG).e ( e);
        } catch (UnrecoverableKeyException e) {
            Timber.tag(Utils.TIMBER_TAG).e ( e);
        }



        try {
            //addToHistory("Connecting to " + serverUri);
            mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
                u/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();
                }

                u/Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Timber.tag(Utils.TIMBER_TAG).v("Failed to connect to: " + serverUri);
                }
            });


        } catch (MqttException ex){
            ex.printStackTrace();
        }

    }


    public void subscribeToTopic(){
        try {
            mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
                u/Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    Timber.tag(Utils.TIMBER_TAG).v("Subscribed!");
                }

                u/Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Timber.tag(Utils.TIMBER_TAG).v("Failed to subscribe");
                }
            });


            mqttAndroidClient.subscribe(subscriptionTopic, 0, new IMqttMessageListener() {
                u/Override
                public void messageArrived(String topic, MqttMessage message) throws Exception {
                    // message Arrived!
                    Timber.tag(Utils.TIMBER_TAG).v("Message: " + topic + " : " + new String(message.getPayload()));
                    sendNotification(topic,new String(message.getPayload()));
                }
            });

        } catch (MqttException ex){
            System.err.println("Exception whilst subscribing");
            ex.printStackTrace();
        }
    }




    public void sendNotification(String title, String message) {

        ... 

    }
}
Previous Topic:Managing reconnections to MQTT server
Next Topic:Java MQTT5 implementation
Goto Forum:
  


Current Time: Wed Oct 28 18:54:08 GMT 2020

Powered by FUDForum. Page generated in 0.01489 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top