Search code examples
androidchromecastgoogle-cast

CastCompanionLibrary detecting presence of Chromecast devices on network


When starting the activity that has a MediaRouteButton implemented according to the CastCompanionLibrary docs, the button does not display even when there are Chromecast devices in the network. (Note the MediaRouteButton here is not in the ActionBar; the activity we're talking about here actually has no ActionBar, but it extends ActionBarActivity. Other activities in our application has the Cast Button implemented into the Action Bar according to the docs, which behaves correctly when there are Chromecast devices in the network.)

We observed that onCastAvailabilityChanged (as well as onCastDeviceDetected) in the VideoCastConsumer was not being called. We traced the code and found that onRouteAdded in /cast/CastMediaRouterCallback.java wasn't being called when the activity starts.

In our activity, we create the consumer in onResume

@Override
    protected void onResume() {
        mCastManager = VideoCastManager.getInstance();
        if (mCastManager != null) {
            mVideoCastConsumer = new VideoCastConsumerImpl() {
                .....
                @Override
                public void onCastAvailabilityChanged(boolean castPresent) {
                    mMediaRouteButton.setVisibility(castPresent ? View.VISIBLE : View.GONE);
                }
                .....
            }
            mCastManager.incrementUiCounter();
            mCastManager.addVideoCastConsumer(mVideoCastConsumer);
        }
    …..

and do decrementUiCounter and remove videoCastConsumer from the manager in onPause, as the implementation docs state.

However, we did observe that strangely the onCastAvailabilityChanged and onCastDeviceDetected callbacks always get triggered after we background and foreground the application. Any ideas why that happens? Is there a way to replicate this behaviour to happen on activity load or at least in onResume/onPause?

EDIT: Worth mentioning, we tried calling mCastManager.startCastDiscovery, but the callbacks weren't called until we backgrounded/foregrounded again.


Solution

  • There are two things that need to happen: (1) applications should register themselves to be notified when a change in the availability of routes happen and (2) they should be able to get the current state, i.e. to see if there is any route available at that moment or not. Then in each activity that has a MediaRouterButton, both of these should be used; first register the callback and then make a direct call to see if there is any route available or not. This will set the state current at the start-up and the callback keeps the state current with changes.

    I am making some small changes in this area in CCL soon to make these two steps more robust so if you can wait a few days, you should be able to see the updated version; if you can't, then you might need to do a bit of work on your side.

    Update: in CCL v2.0.1, some changes were made to make this process more robust; you basically need to register to the callback onCastAvailabilityChanged() and also when you start an activity that has the button, call VideoCastManager.isAnyRouteAvailable() to set the initial visibility of the cast button.