Sign In Register

How can we help you today?

Start a new topic
Answered

JAVA - How to check SDK initialization is finished when app launches


Hello all.


My dev environment is Android JAVA.


I have this code in my onCreate method of the Activity

(it is pseudo code )


GameSparksAndroidApi.initialise(GS_SERVICE_API, GS_API_SECRET, this.getApplication());

mGSApi = GameSparksAndroidApi.getInstance();

mGSApi.setMessageRecievedListener(mListener);

mGSApi.setSdkConnectedListener(mSDKListener);


Map<String, Object> data = new HashMap<String, Object>();

data.put("@class", ".FacebookConnectRequest");

data.put("accessToken", FB_Access_Token);

data.put("doNotLinkToCurrentPlayer", true);

data.put("switchIfPossible", true);

mGSApi.getSender(data).sendBackground(new GSListener() { ... }



it compiles OK and 70% of the cases it also works great on runtime.

but 30% of the cases mGSApi.getSender(data).sendBackground() responds ERROR saying:


GameSparks SDK not ready to send. SDK state: CONNECTING_TO_LOADBALANCER


so I checked the android log and found out that sometimes GameSparks SDK

 .initialise() method fails(?) and need more times to finish!


This is the log when the sendBackground responds Error: 



D/GameSparks SDK: connect

D/GameSparks SDK: New state: CONNECTING_TO_LOADBALANCER

E/GameSparks SDK: Timeout waiting to connect

D/GameSparks SDK: New state: STOPPED

D/GameSparks SDK: stopThreads

D/GameSparks SDK: onConnectivityChange, network is available

D/GameSparks SDK: New state: CONNECTING_TO_LOADBALANCER

// sendbackground method was called here

ERROR: GameSparks SDK not ready to send. SDK state: CONNECTING_TO_LOADBALANCER

D/GameSparks SDK: WebSocket connected

D/GameSparks SDK: mSocket closed in disconnect

D/GameSparks SDK: stopThreads

D/GameSparks SDK: New state: CONNECTING_TO_SERVICE

D/GameSparks SDK: HybiParser has stopped

D/GameSparks SDK: mThread has stopped

D/GameSparks SDK: mSocket closed in finally block

D/GameSparks SDK: WebSocket connected

D/GameSparks SDK: HybiParser has stopped

D/GameSparks SDK: mThread has stopped

D/GameSparks SDK: mSocket closed in finally block

D/GameSparks SDK: WebSocket connected

D/GameSparks SDK: New state: AUTHORISED

D/GameSparks SDK: mSocket closed in finalize

D/GameSparks SDK: mSocket closed in finalize

D/GameSparks SDK: ping


FYI, network state was always good. I don't think the app fails to connect to the internet.



How can I wait for the SDK to be fully initialised? is there any callback or notification?

or am I doing it wrong? should I manually wait for the SDK to be initialised? (like, update loop)

if so, how can I check the SDK is ready in java?


thanks.



Best Answer
Hi Woov,

I see that you've assigned an SdkConnectedListener to your GameSparksAndroidApi. To ensure you are connected prior to sending your requests you will need to override the SdkConnectedListener's 'onConnected'. Probably the easiest way to do this is to have your Activity implement SdkConnectedListener:

   public class MainActivity implements SdkConnectedListener

Then set your current activity as your GameSparksAndroidApi's SdkConnectedListener:

   mGSApi.setSdkConnectedListener(this);

You then need to implement the SdkConnectedListener's 'onConnected' function somewhere in your Activity. You should palace your initial request within this method. It will trigger once the connection has been established:

    @Override
    public void onConnected(){

        Map<String, Object> data = new HashMap<String, Object>();

        data.put("@class", ".FacebookConnectRequest");

        data.put("accessToken", FB_Access_Token);

        data.put("doNotLinkToCurrentPlayer", true);

        data.put("switchIfPossible", true);

        mGSApi.getSender(data).sendBackground(new GSListener() { ... }

    }

Let me know if this approach works for you, or if you run into any further issues.

Regards,
Vinnie

 


Hi Woov,


Will investigate this behaviour with the SDK and update you.


Thanks,


Oisin

Answer
Hi Woov,

I see that you've assigned an SdkConnectedListener to your GameSparksAndroidApi. To ensure you are connected prior to sending your requests you will need to override the SdkConnectedListener's 'onConnected'. Probably the easiest way to do this is to have your Activity implement SdkConnectedListener:

   public class MainActivity implements SdkConnectedListener

Then set your current activity as your GameSparksAndroidApi's SdkConnectedListener:

   mGSApi.setSdkConnectedListener(this);

You then need to implement the SdkConnectedListener's 'onConnected' function somewhere in your Activity. You should palace your initial request within this method. It will trigger once the connection has been established:

    @Override
    public void onConnected(){

        Map<String, Object> data = new HashMap<String, Object>();

        data.put("@class", ".FacebookConnectRequest");

        data.put("accessToken", FB_Access_Token);

        data.put("doNotLinkToCurrentPlayer", true);

        data.put("switchIfPossible", true);

        mGSApi.getSender(data).sendBackground(new GSListener() { ... }

    }

Let me know if this approach works for you, or if you run into any further issues.

Regards,
Vinnie

 

Login to post a comment