Search code examples
androidgoogle-castchromecast

Chromecast Sender App Doesn't Detect Disconnection/Reconnection


I previously developed an android sender app with the preview SDK and found that when a Chromecast was turned on or off, the app very quickly discovered this and changed the state of the media route button.

However with the new SDK, if a Chromecast is unplugged while the app isn't connected to it, MediaRouter.Callback.onRouteUnselected() is never called and the Cast icon remains visible. In some cases even if the app is closed and opened again, onRouteAdded is called on startup with the details of the previously connected Chromecast, even though it's not plugged in anymore and hence not on the network, which suggests that there's some kind of cache somewhere.

I'm noticing this both with my own app (which uses the companion library) and the example CastVideos-android app on github. This sets up the callback like so:

mMediaRouter = MediaRouter.getInstance(context);
mMediaRouteSelector = new MediaRouteSelector.Builder().addControlCategory(
    CastMediaControlIntent.categoryForCast(mApplicationId)).build();

mMediaRouterCallback = new CastMediaRouterCallback(this, context);
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
    MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);

This results in a pretty rough user experience - is there any way I can improve it? Perhaps some flag that I can pass that'll perform an even more active scan than CALLBACK_FLAG_PERFORM_ACTIVE_SCAN, hence detecting Chromecasts that are no longer on the network? Or perhaps some method that I can hit at intervals to scan again and make sure the chromecast is still there? I've had a look through the API but I can't see anything.

EDIT: Worse still, I've also noticed that if a Chromecast is unplugged then you attempt to cast to it, it will disconnect (good) but won't detect the Chromecast when it's plugged back in. This is true even if you get out of the app and start it up again.


Solution

  • The behavior you have outlined is caused by a bug in the SDK that is going to be addressed in the next release of the Google Play services.