Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"GoogleApiClient is not connected yet" exception in Cast application

I'm developing an Android application that casts content to Chromecast. Sometimes in my com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks implementation in the onConnected method, I get a

java.lang.IllegalStateException: GoogleApiClient is not connected yet.

exception.

Here is the stack trace:

    FATAL EXCEPTION: main
 Process: com.joaomgcd.autocast, PID: 13771
 java.lang.IllegalStateException: GoogleApiClient is not connected yet.
    at com.google.android.gms.internal.eg.a(Unknown Source)
    at com.google.android.gms.common.api.GoogleApiClient.b(Unknown Source)
    at com.google.android.gms.cast.Cast$CastApi$a.launchApplication(Unknown Source)
    at com.joaomgcd.autocast.media.MediaConnectionCallbacks.onConnected(MediaConnectionCallbacks.java:37)
    at com.google.android.gms.internal.dx.b(Unknown Source)
    at com.google.android.gms.common.api.GoogleApiClient.bn(Unknown Source)
    at com.google.android.gms.common.api.GoogleApiClient.f(Unknown Source)
    at com.google.android.gms.common.api.GoogleApiClient$2.onConnected(Unknown Source)
    at com.google.android.gms.internal.dx.b(Unknown Source)
    at com.google.android.gms.internal.dx.bT(Unknown Source)
    at com.google.android.gms.internal.dw$h.b(Unknown Source)
    at com.google.android.gms.internal.dw$h.b(Unknown Source)
    at com.google.android.gms.internal.dw$b.bR(Unknown Source)
    at com.google.android.gms.internal.dw$a.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

This only seems to happen if I had already connected to the GoogleApiClient before and am connecting for a second time. Between the 2 calls I disconnect from the api client with the code below.

My guess is that this is a bug. Am I correct? Since I'm in the onConnected method, the GoogleApiClient should already be connected.

What can I do to get around it? Should I just wait for a while until the GoogleApiClient is really connected?

I am doing this in a service and here are the relevant bits:

when the service starts:

mMediaRouter.addCallback(mMediaRouteSelector, mediaCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);

mediaCallback has this code:

@Override
    public void onRouteAdded(MediaRouter router, RouteInfo route) {
        super.onRouteAdded(router, route);
        if (route.getDescription().equals("Chromecast")) {
            ...
            mSelectedDevice = com.google.android.gms.cast.CastDevice.getFromBundle(route.getExtras());

            ...
                castClientListener = new CastListener(context, apiClient);

                Cast.CastOptions.Builder apiOptionsBuilder = Cast.CastOptions.builder(mSelectedDevice, castClientListener);
                ...
                apiClient.set(new GoogleApiClient.Builder(context).addApi(Cast.API, apiOptionsBuilder.build()).addConnectionCallbacks(connectionCallback).addOnConnectionFailedListener(new MediaConnectionFailedListener(context)).build());
                apiClient.get().connect();
        }

    }

connectionCallback has this code:

@Override
    public void onConnected(final Bundle arg0) {
        ...
            Cast.CastApi.launchApplication(apiClient, UtilAutoCast.CHROMECAST_APP_ID, false).setResultCallback(connectionCallback);
        ...
    }

The code above is the part where the crash happens.

And when I stop the service I run this code:

if (mMediaRouter != null) {
    mMediaRouter.removeCallback(mediaCallback);
    mMediaRouter = null;
}
if (apiClient != null) {
    Cast.CastApi.stopApplication(apiClient);
    if (apiClient.isConnected()) {
        apiClient.disconnect();
        apiClient = null;
    }
}

Thanks in advance.

like image 535
joaomgcd Avatar asked Feb 17 '14 14:02

joaomgcd


2 Answers

Google APIs for Android > GoogleApiClient

You should instantiate a client object in your Activity's onCreate(Bundle) method and then call connect() in onStart() and disconnect() in onStop(), regardless of the state.

The implementation of the GoogleApiClient appears designed for only a single instance. It's best to instantiate it only once in onCreate, then perform connections and disconnections using the single instance.

I would guess that only one GoogleApiClient can be actually be connected, but multiple instances are receiving the onConnected callback.

In your case it's probably fine to not call connect in onStart, but only in onRouteAdded.

I think this issue is very similar to Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet

like image 110
mattm Avatar answered Oct 30 '22 09:10

mattm


Have you declare following <meta> tag

<application ...> 
... 
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
... 
</application>

I just forgot to write so may you also stuck with this reason.

Thank you.

like image 35
Pratik Butani Avatar answered Oct 30 '22 09:10

Pratik Butani