Adam Styrc
Adam Styrc

Reputation: 1537

Detect usage of Google Cast

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?

Upvotes: 14

Views: 2446

Answers (2)

Friesgaard
Friesgaard

Reputation: 3204

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.

Upvotes: 0

Leon Nicholls
Leon Nicholls

Reputation: 4656

  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.

Upvotes: 1

Related Questions