简体   繁体   中英

Android Paho client - How to enable offline buffering and send messages after network enabled?

I am use Android Paho client library v1.1.0, QoS 2. My code for pub/sub over MQTT:

mClient = new MqttAndroidClient(this, uri, clientId, new   MqttDefaultFilePersistence());
MqttConnectOptions conOpt = new MqttConnectOptions();

conOpt.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
conOpt.setCleanSession(true);
conOpt.setAutomaticReconnect(true);

mClient.setTraceEnabled(true);

mClient.setCallback(this);
mClient.setTraceCallback(this);

IMqttToken connectToken = mClient.connect(conOpt, null, this);

And offline buffering options:

@Override
public void onSuccess(IMqttToken iMqttToken) {
    this.disconnectedBufferOptions = new DisconnectedBufferOptions();
    this.disconnectedBufferOptions.setBufferEnabled(true);

    mClient.setBufferOpts(disconnectedBufferOptions);

    subscribe(topic);
}

Messages published to topic if the network is available, but if it disabled and enabled again offline messages not sending. How to correctly enable offline buffering option and send offline messages? Additional info: mClient.getBufferedMessageCount() after network disabled and publish message = 0

using conOpt.setAutomaticReconnect(true) along with the following code snippet in onSuccess of connect token solved it for me.

DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();

disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);

mClient.setBufferOpts(disconnectedBufferOptions);        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM