2016-09-30 42 views
2

我使用Android泛美衛生組織客戶端庫V1.1.0中,QoS 2. 我的發佈/訂閱代碼在MQTT:Android Paho客戶端 - 啓用網絡後如何啓用離線緩衝和發送消息?

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); 

和離線緩存選項:

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

    mClient.setBufferOpts(disconnectedBufferOptions); 

    subscribe(topic); 
} 

消息發佈到主題是否網絡可用,但是如果它禁用並再次啓用不發送的離線消息。 如何正確啓用離線緩衝選項併發送離線消息? 附加信息:mClient.getBufferedMessageCount()在網絡禁用併發布消息= 0後

回答

1

使用conOpt.setAutomaticReconnect(true)以及以下代碼片段中的onSuccess連接標記爲我解決了它。

DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions(); 

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

mClient.setBufferOpts(disconnectedBufferOptions);   
相關問題