14

How to detect if my AndroidTV is currently an Google Cast receiver from mobile/desktop ?

How to detect who is currently an Google Cast sender from Google Cast Receiver perspective?

How to detect if my Android Phone is currently an Google Cast sender?

Adam Styrc
  • 1,517
  • 2
  • 23
  • 38
  • Please, if possible, explain why you would need to have that information, what is your use case? – Ali Naddaf Feb 11 '16 at 16:07
  • I'd like to make stats what way is android tv used and is android phone used as remote control. – Adam Styrc Feb 11 '16 at 16:20
  • To add to this, I'd like this functionality, to run a watcher app that turns on my tv/speakers via Harmony when it detects a cast has begun – Dermot Aug 09 '17 at 09:58

2 Answers2

1
  1. You could use the Android Media Router API to request all available routes to Cast devices. Then you would then have to connect to each Cast device and then you can determine the app ID or even if media is playing. If you only want to know if your own app is running, then simply filter the Media Router request by that.

  2. Only the receiver would know which senders are connected to it. In your own custom receiver you could keep track of that for your own app.

  3. You might be able to use the Media Router to determine if there is an active route. Not sure if you will be able to tell if it is a Cast route, since these routes could also go to other devices like bluetooth speakers.

Leon Nicholls
  • 4,623
  • 2
  • 16
  • 17
0

To add to Leons answer, we have a callback on the MediaRouter like so to get callback on when our chromecast receiver app is selected

val mMediaRouterCallback = object : MediaRouter.Callback() {
    override fun onRouteSelected(router: MediaRouter?, route: MediaRouter.RouteInfo?) {
        routeInfo = route
    }
    override // override appropriate methods here!!
}
val mMediaRouteSelector = MediaRouteSelector.Builder()
    .addControlCategory(
        CastMediaControlIntent
            .categoryForCast(BuildConfig.CHROMECAST_RECEIVER_APP_ID)
    ).build()

MediaRouter.getInstance(context).addCallback(
    mMediaRouteSelector, mMediaRouterCallback,
    MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN
)

Build selector to work for more than one app. And it is possible to change MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN to broaden your scan.

Friesgaard
  • 2,975
  • 2
  • 17
  • 13
  • Isn't it is the API for the **sender** to select which **receiver** to be cast to? The OP want to opposite one. He is the receiver and would like to know weather a sender is connected to or what the sender is. – Yeung Oct 08 '21 at 09:50
  • I think it is possible to automatically connect to a receiver if that is what you mean. The receiver software is completely different, take a look here: https://developers.google.com/cast/docs/web_receiver – Friesgaard Oct 11 '21 at 16:19