"GoogleApiClient is not connected yet" exception in Cast application
Asked Answered
C

4

12

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.

Crosshatch answered 17/2, 2014 at 14:8 Comment(3)
can you add some code?Lickspittle
Connecting for a second time? Have you disconnected from the first one? Need more code from your side to see what is happening.Cavender
Please show us where you are connecting GoogleApiClient...Wing
G
7

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

Godolphin answered 8/5, 2015 at 20:55 Comment(1)
Also from the examples you can use .enableAutoManage(this /* FragmentActivity /, this / OnConnectionFailedListener */), which takes care of the connect() and disconnect() or so it seems.Unpolled
D
5

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.

Duffy answered 29/7, 2015 at 5:30 Comment(0)
G
2

From showcase app (Googlecast Github Sample CastHelloText-android ) receiver app is launched onRouteSelected (not onRouteAdded as you are doing in your code). I would try to change that. In case it does not work, I would add log lines in every method related to connection & session, and see what is happening.

Another tip: I have had crash with stopping the application (in case chromecast device is physically plugged out from power). Solution is to putCast.CastApi.stopApplication(apiClient); inside if (apiClient.isConnected()).

Girt answered 3/3, 2014 at 15:58 Comment(1)
Thanks for your answer, but I'm running my app in a background service so I really need to connect to my Chromecast on Route Added. The user doesn't manually select a route in this case. :)Crosshatch
E
0

It seems like you are calling GoogleApiClient before it is connected

move this line in onCreate()

// First we need to check availability of play services
    if (checkPlayServices()) {

        // Building the GoogleApi client
        //buildGoogleApiClient();
        try {
            mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();

            mGoogleApiClient.connect();
        }catch (IllegalStateException e)
        {
            Log.e("IllegalStateException", e.toString());
        }
        createLocationRequest();
    }

Hope it helps you.

Ebonize answered 10/1, 2017 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.