add lwt to connect

This commit is contained in:
Eric House 2022-01-10 04:57:15 -08:00 committed by Eric House
parent 44923910ea
commit ce6537f2a9

View file

@ -269,6 +269,24 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
}
}
// Last Will and Testimate is the way I can get the server notified of a
// closed connection.
private void addLWTIf( MqttConnectOptions mqttConnectOptions )
{
if ( BuildConfig.DEBUG ) {
try {
JSONObject payload = new JSONObject();
payload.put( "devid", mDevID );
payload.put( "ts", Utils.getCurSeconds() );
mqttConnectOptions.setWill( "xw4/device/LWT", payload.toString().getBytes(), 2, false );
// mqttConnectOptions.setKeepAliveInterval( 15 ); // seconds; for testing
} catch ( JSONException je ) {
Log.e( TAG, "addLWTIf() ex: %s", je );
}
}
}
private void setup()
{
Log.d( TAG, "setup()" );
@ -277,6 +295,7 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
mqttConnectOptions.setCleanSession(false);
mqttConnectOptions.setUserName("xwuser");
mqttConnectOptions.setPassword("xw4r0cks".toCharArray());
addLWTIf( mqttConnectOptions );
try {
setState( State.CONNECTING );