4

I'm writing a chromecast receiver application that will (hopefully) allow me to remotely put alert messages up on my TV to serve as reminders.

My plan was to have a dedicated wireless device on my home network that would constantly poll for new messages from a centralized server. When a new message was found, it would connect to a chromecast route, turning on the TV and displaying the new message.

But as far as I can tell, the only way to activate a chromecast route is by manually clicking the chromecast icon on my Chrome browser or wireless device.

Is there a way, programmatically, to activate the chromecast? Can it be done in the sender?

Donnie
  • 43
  • 1
  • 3

1 Answers1

6

You can programmatically scan for cast devices and connect to them if needed. Steps are:

  1. Get an instance of the MediaRouter singleton from the system: mMediaRouter
  2. Build a selector: mMediaRouteSelector = new MediaRouteSelector.Builder() .addControlCategory( CastMediaControlIntent .categoryForCast(YOUR_APP_ID)).build();
  3. Add a callback to initiate scan: mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
  4. The onRouteAdded() and onRouteRemoved() of your callback (i.e. mMediaRouterCallback) will be called as routes are discovered or removed. You can maintain a list of routes in your app and keep them up to date by using these two callbacks.
  5. You can select a route by calling mMediaRouter.selectRoute(aRouteInfo). Then the onRouteSelected() of your callback will be called and you can extract the cast device as usual and do as you please.

These said, remember that if you want to show a notification to users on TV your app should be running on the chromecast at the time you want to send the notification.

Ali Naddaf
  • 16,951
  • 2
  • 21
  • 28
  • 1
    Apologies for the delay in marking as answer. When I originally asked this question, I hadn't actually tried making an _android_ sender (only chrome) so I didn't know whether to mark it or not. After finally making an Android sender, your solution does appear to work for Android. By any chance are you aware of a way to programmatically select a route in a chrome sender? – Donnie Mar 04 '14 at 00:34