package org.deviceconnect.android.deviceplugin.alljoyn;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;

public class OneShotSessionHandler {
    private OneShotSessionHandler() {

    }

    public static void run(@NonNull final Context context, @NonNull final String busName,
                           final short port, @NonNull final SessionJoinCallback callback) {
        final AllJoynDeviceApplication app =
                (AllJoynDeviceApplication) context.getApplicationContext();

        app.joinSession(busName, port, app.new ResultReceiver() {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                if (resultCode != AllJoynDeviceApplication.RESULT_OK) {
                    callback.onSessionFailed(busName, port);
                    return;
                }
                int sessionId = resultData.getInt(AllJoynDeviceApplication.PARAM_SESSION_ID);

                callback.onSessionJoined(busName, port, sessionId);

                app.leaveSession(sessionId, app.new ResultReceiver());
            }
        });
    }

    public interface SessionJoinCallback {
        void onSessionJoined(@NonNull String busName, short port, int sessionId);

        void onSessionFailed(@NonNull String busName, short port);
    }
}

@@ -189,16 +189,67 @@
 ndler %7B%0A
+%0A    private static final int JOIN_RETRY_MAX = 5;%0A%0A
     priv
@@ -602,65 +602,133 @@
     
-app.joinSession(busName, port, app.new ResultReceiver() %7B
+AllJoynDeviceApplication.ResultReceiver resultReceiver = app.new ResultReceiver() %7B%0A            private int failedCount = 0;%0A
 %0A   
@@ -894,24 +894,84 @@
 ESULT_OK) %7B%0A
+                    if (failedCount %3E JOIN_RETRY_MAX) %7B%0A    
             
@@ -1015,24 +1015,176 @@
 ame, port);%0A
+                    %7D else %7B%0A                        ++failedCount;%0A                        app.joinSession(busName, port, this);%0A                    %7D%0A
             
@@ -1458,32 +1458,32 @@
 ;%0A            %7D%0A
-
         %7D);%0A    
@@ -1475,16 +1475,72 @@
        %7D
+;%0A%0A        app.joinSession(busName, port, resultReceiver
 );%0A    %7D

Update a helper class to perform a one-shot task. Add retry logic.