"GoogleApiClient not yet connected" exception in Cast application

I am developing an Android application that transfers content to Chromecast. Sometimes in my implementation of com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks in the onConnected method I get

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

an 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$hb(Unknown Source) at com.google.android.gms.internal.dw$hb(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 happens if I have already connected to GoogleApiClient and reconnected. Between two calls, I disconnect from the api client with the code below.

I assume this is a mistake. I'm right? Since I use the onConnected method, GoogleApiClient should already be connected.

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

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

when starting the service:

 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 failure occurs.

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.

+12
android chromecast google-cast
Feb 17 '14 at 2:08
source share
4 answers

https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html

You must create an instance of the client object in the Activity onCreate (Bundle) method, and then call connect () in onStart () and disconnect () in onStop () regardless of state.

The GoogleApiClient implementation appears for only one instance. It is best to create an instance only once in onCreate , and then perform connections and disconnections using a single instance.

I would suggest that in fact you can only connect one GoogleApiClient , but multiple instances receive an onConnected .

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

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

+6
May 8 '15 at 20:55
source share

You declare the following <meta>

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

I just forgot to write, so you can stick to this reason too.

Thank.

+5
Jul 29 '15 at 5:30
source share

From the showcase application ( https://github.com/googlecast/CastHelloText-android ), the receiver application starts onRouteSelected (and not onRouteAdded, as you do in your code). I will try to change this. If it does not work, I would add log lines to each method associated with the connection and session, and see what happens.

One more tip: I had a crash when the application stopped (in case the chrome device is physically disconnected from the power supply). The solution is to put Cast.CastApi.stopApplication(apiClient); inside if (apiClient.isConnected()) .

+2
Mar 03 '14 at 15:58
source share

It seems that you are calling GoogleApiClient before it is connected.

move this line to 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 this helps you.

0
Jan 10 '17 at 12:03 on
source share



All Articles