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?

3
  • Please, if possible, explain why you would need to have that information, what is your use case?
    – Ali Naddaf
    Commented Feb 11, 2016 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
    Commented Feb 11, 2016 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
    Commented Aug 9, 2017 at 9:58

2 Answers 2

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.

2
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.

2
  • 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
    Commented Oct 8, 2021 at 9: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: developers.google.com/cast/docs/web_receiver
    – Friesgaard
    Commented Oct 11, 2021 at 16:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.