Skip to content
Snippets Groups Projects
Commit 1980bc26 authored by René Schöne's avatar René Schöne
Browse files

Disable auto-reconnect to mqtt-broker if first attempt failed.

- updated to latest tools version
parent ea4e7cc8
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:3.1.3'
// NOTE: Do not place your application dependencies here; they belong
......
......@@ -175,7 +175,7 @@ public class MainActivity extends AppCompatActivity implements
} catch (MqttException e) {
Toast.makeText(MainActivity.this,
"Could not disconnect from running broker. Continue.",
Toast.LENGTH_LONG).show();
Toast.LENGTH_SHORT).show();
}
}
......@@ -183,14 +183,14 @@ public class MainActivity extends AppCompatActivity implements
EditText valueServerURI = findViewById(R.id.value_server_uri);
final String serverURI = valueServerURI.getText().toString();
Toast.makeText(MainActivity.this, "Connecting to " + serverURI,
Toast.LENGTH_LONG).show();
Toast.LENGTH_SHORT).show();
mqttAndroidClient = new MqttAndroidClient(this, serverURI,
"SensorSharing-mobile");
mqttAndroidClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
Toast.makeText(MainActivity.this, "Connection to MQTT broker lost",
Toast.LENGTH_LONG).show();
Toast.LENGTH_SHORT).show();
}
@Override
......@@ -203,7 +203,7 @@ public class MainActivity extends AppCompatActivity implements
}
});
MqttConnectOptions options = new MqttConnectOptions();
final MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setAutomaticReconnect(true);
options.setConnectionTimeout(5);
......@@ -218,12 +218,15 @@ public class MainActivity extends AppCompatActivity implements
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.e(TAG, "Connection to " + serverURI + " failed", exception);
Toast.makeText(MainActivity.this, "Connection to " + serverURI + " failed",
Toast.LENGTH_SHORT).show();
options.setAutomaticReconnect(false);
}
});
} catch (MqttException e) {
Toast.makeText(this, "Connection to " + serverURI + " failed",
Toast.LENGTH_LONG).show();
Toast.LENGTH_SHORT).show();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment