Here is sample code which shows the problem. In plain desktop Java it takes a consistent 1 minute, 3 seconds to fail. I use the same code on android, and on WiFi it takes about the same time (a little over a minute), but on 3G/4G it takes 2+ minutes, and I timed one (
google.com) at >10 minutes before killing it.
public class SuperSimple
{
public static final String timeDiffFormat = "%1$tM:%1$tS.%1$tL";
private static MqttClient client;
public static void main(String... args)
{
final long startTime = System.currentTimeMillis();
try
{
MqttConnectOptions mOptions = new MqttConnectOptions();
mOptions.setKeepAliveInterval(20);
mOptions.setConnectionTimeout(20);
client.connect(mOptions);
}
catch (MqttException mEx)
{
long errorTime = System.currentTimeMillis();
Date difference = new Date(errorTime - startTime);
String time = String.format(Locale.US, timeDiffFormat, difference);
String message = mEx.getMessage();
System.out.printf("(%s)Exception: %s%n", time, message);
}
try
{
client.disconnect();
}
catch (MqttException e)
{
//This always happens: already disconnected.
}
}
}