From 3449833fbffd0c3d10db9cafc28b99f2ab23b700 Mon Sep 17 00:00:00 2001 From: M Nanda Rimansyah Date: Tue, 20 Oct 2015 19:48:48 +0700 Subject: [PATCH] update reconnect process. https://trello.com/c/mgzWnc44 --- .../paho/android/service/MqttConnection.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttConnection.java b/org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttConnection.java index a002cf49..a59ba36d 100644 --- a/org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttConnection.java +++ b/org.eclipse.paho.android.service/org.eclipse.paho.android.service/src/org/eclipse/paho/android/service/MqttConnection.java @@ -197,7 +197,7 @@ public void connect(MqttConnectOptions options, String invocationContext, service.messageStore.clearArrivedMessages(clientHandle); } - service.traceDebug(TAG, "Connecting {" + serverURI + "} as {"+ clientId + "}"); + service.traceDebug(TAG, "Connecting {" + serverURI + "} as {" + clientId + "}"); final Bundle resultBundle = new Bundle(); resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken); @@ -314,7 +314,7 @@ private void doAfterConnectFail(final Bundle resultBundle){ acquireWakeLock(); disconnected = true; setConnectingState(false); - service.callbackToActivity(clientHandle, Status.ERROR,resultBundle); + service.callbackToActivity(clientHandle, Status.ERROR, resultBundle); releaseWakeLock(); } @@ -1018,9 +1018,22 @@ public void onFailure(IMqttToken asyncActionToken, Throwable exception) { Thread.sleep(delay); } catch(InterruptedException e) { } - reconnect(); + + if(isNotAuthenticationFailed(exception)) + { + reconnect(); + } } + + private boolean isNotAuthenticationFailed(Throwable throwable) { + int reasonCode = -1; + if(throwable instanceof MqttException) { + reasonCode = ((MqttException) throwable).getReasonCode(); + } + + return reasonCode != MqttException.REASON_CODE_FAILED_AUTHENTICATION; + } }; myClient.connect(connectOptions, null, listener);