4

Ok, so I am trying to figure at how to get an up-to-date list of available chromecast devices, I'm doing this so that my app can check when the chromecast is not in use and then open my receiver app.

I am having some unexpected behaviour from the code below:

public class MainActivity extends Activity {

...

@Override
public void onCreate(Bundle savedInstanceState) {

    ...   

    mMediaRouterCallback = new MyMediaRouterCallback(); 

    mMediaRouter = MediaRouter.getInstance(context);

    mMediaRouteSelector = new MediaRouteSelector.Builder()
       .addControlCategory(CastMediaControlIntent.categoryForCast(context.getString(R.string.app_id)))
       .build();

    mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,

    MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
}

This adds a MediaRouter callback to the MediaRouter. I have chosen to use the active scan flag.

private class MyMediaRouterCallback extends MediaRouter.Callback {

    ...

    @Override
    public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo info) {

        Log.d(TAG, "Description 1 " + info.getDescription());

        mSelectedDevice = CastDevice.getFromBundle(info.getExtras());

        Log.d(TAG, "Description 2 " + mSelectedDevice.toString());

        if(info.getDescription().equals("Chromecast")) {
           // code to launch chromecast receiver app here.
        }

    }
}

My implementation of the MediaRouter.Callback overrides onRouteAdded, it simply prints some information about the devices it has found Description 1 describes the receiver app the device is using, description 2 gives its name.

However when this code is run initially the same device is discovered twice printing:

07-05 21:01:12.270: D/MainActivity(9730): Description 1 Casting HelloText
07-05 21:01:12.280: D/MainActivity(9730): Description 2 "Downstairs" 
07-05 21:01:12.280: D/MainActivity(9730): Description 1 Casting HelloText
07-05 21:01:12.280: D/MainActivity(9730): Description 2 "Downstairs"

Then periodically the onRouteAdded callback is called sometimes only listing the device once, other times listing the device twice. My understanding however is that this callback should only be called when a new route is added.

I want to find all the available devices on command, not at random intervals that I can't control, what do I need to be doing? I can't find a callback that seems to be appropriate for this situation (such as whenever devices update/change), nor can I find a way to list them without using callbacks, so I'm a bit stuck.

(I have been basing these tests of the HelloText-Android example found here https://github.com/googlecast/CastHelloText-android, also I started this (my first android project) only a couple of days ago, so I apologise if I am missing something horrendously obvious)

Thanks in advance.

Xanderite
  • 179
  • 1
  • 6

2 Answers2

1

Call getRoutes() to get the list of known routes at the point in time that you desire. Iterate over them. Call matchesSelector() on each to filter out those that match your desired control category.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Hi thanks for responding, using getRoutes() doesn't seem to actually update the MediaRoute object, it just serves up the old information. For example, I have a loop that waits 30 seconds between each call of getRoutes, but if I switch between receiver apps (from google music to youtube) it only occasionally notices, and then sometimes the chromecast disappears from the list entirely, but I know it is still working as an Airplay device on my network continues to show up. Any ideas? – Xanderite Jul 05 '14 at 22:34
  • @user84348: Whether that is an issue with `MediaRouter`, or whether that is an issue with the Chromecast `MediaRouteProvider`, I cannot say. – CommonsWare Jul 05 '14 at 23:32
  • Its strange, the chromecast icon dissapears from my app at the same time getRoutes() no longer finds a chromecast, but if I were to switch to a different chromecast app, my chromecast is right there. – Xanderite Jul 06 '14 at 14:10
  • What do you expect to happen when you switch from Google Music to YouTube in the response you get from getRoutes()? – Ali Naddaf Jul 07 '14 at 14:31
  • Well originally I thought it would give me the name of the app that was running on the chrome-cast when I called getDescription on the route, but that is obviously not the case. I ended up implementing your suggestion from http://stackoverflow.com/questions/21879873/chromecast-sdk-android-getapplicationmetadata-returns-null to poll for the current chromecast app that was running. It works perfectly except that calling Cast.CastApi.joinApplication(mApiClient) when the receiver app is youtube gives 2005 APPLICATION_NOT_RUNNING, every time, every other receiver app I can successfully join – Xanderite Jul 08 '14 at 14:23
  • Is that intentional? How can I detect when the chromecast is on the home screen? that is basically all I want to do. – Xanderite Jul 08 '14 at 14:24
0

If you are listening for "onRouteAdded()", you would also need to listen to "onRouteRemoved()" to do a correct bookkeeping; if a device is added, it can be removed and added again so if you just listen to onRouteAdded(), it may seem it is being added multiple times. Getting the list from MedaiaRoute.getRoutes() might be easier if you don't want to be notified immediately and only want to know the list at certain points on demand.

Ali Naddaf
  • 16,951
  • 2
  • 21
  • 28
  • Thanks for answering, i am continuing to have problems with getting up to date information from my chromecast like [here](http://stackoverflow.com/questions/21879873/chromecast-sdk-android-getapplicationmetadata-returns-null) and [here](http://stackoverflow.com/questions/22137467/chromecast-sender-app-doesnt-detect-disconnection-reconnection?rq=1) have google play services been fixed yet? – Xanderite Jul 07 '14 at 04:09