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.