I'm switching my app away from the CastCompanionLibrary because I need to support more streaming devices and a few of my users are having trouble with Chromecast discovery. I stripped out all of the unnecessary code and made an app just to see what was going on during discovery and sent it to one of those users and the code is never getting any of the callbacks from the media router. At the same time they can run my old app with the CastCompanionLibrary and it will find the Chromecast just fine.
Here is the code I sent the user, it is a self contained class and layout:
public class MainActivity extends AppCompatActivity {
private static SimpleDateFormat TIME_ONLY = new SimpleDateFormat("HH:mm:ss.SSS");
private static Handler handler;
protected MediaRouter.Callback mMediaRouterCallback;
boolean isRunning = false;
private TextView logView;
private MediaRouter mMediaRouter;
private MediaRouteSelector mMediaRouteSelector;
private Timer removeRoutesTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logView = (TextView) findViewById(R.id.log);
Button sendLog = (Button)findViewById(R.id.send_log);
sendLog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("myemail") +
"?subject=" + Uri.encode("Logs") +
"&body=" + Uri.encode(logView.getText().toString());
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send logs..."));
}
});
mMediaRouter = createMediaRouter(getApplicationContext());
mMediaRouterCallback = new MediaRouterCallback();
mMediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(
CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
.build();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
log("Discovery callback requested");
}
private void printRoutes() {
List<MediaRouter.RouteInfo> routes = mMediaRouter.getRoutes();
log("has routes " + routes.size() );
for(MediaRouter.RouteInfo info : routes){
log("has routes " + info.getName() +" - " + info.getDescription());
}
}
public static void runOnUI(Runnable runnable) {
if (handler == null) {
handler = new Handler(Looper.getMainLooper());
}
handler.post(runnable);
}
protected MediaRouter createMediaRouter(Context context) {
return MediaRouter.getInstance(context);
}
private void log(String start) {
Calendar cal = new GregorianCalendar();
String currentLog = logView.getText().toString();
logView.setText(currentLog + "\n" + TIME_ONLY.format(cal.getTime()) + " - " + start);
}
private class MediaRouterCallback extends MediaRouter.Callback {
@Override
public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
log("onRouteAdded - " + route.getName());
super.onRouteAdded(router, route);
}
@Override
public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo route) {
log("onRouteChanged - " + route.getName());
super.onRouteChanged(router, route);
}
@Override
public void onRoutePresentationDisplayChanged(MediaRouter router,
MediaRouter.RouteInfo route) {
log("onRoutePresentationDisplayChanged - [" + route.getName() + "] ["
+ route.getDescription() + "]");
super.onRoutePresentationDisplayChanged(router, route);
}
@Override
public void onRouteRemoved(final MediaRouter router, final MediaRouter.RouteInfo route) {
log("onRouteRemoved - " + route.getName());
super.onRouteRemoved(router, route);
}
@Override
public void onRouteVolumeChanged(MediaRouter router, final MediaRouter.RouteInfo route) {
super.onRouteVolumeChanged(router, route);
}
private void removeServices(final MediaRouter.RouteInfo route) {
log("removeServices - " + route.getName());
}
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/send_log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Send log"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/send_log">
<TextView
android:id="@+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</ScrollView>
</RelativeLayout>
Here is the log she sent me:
08:06:52.542 - Discovery callback requested
So basically no calls to the callback methods.
Here is a log from my network where it works just fine (exact same apk):
16:56:06.085 - Discovery callback requested
16:56:07.193 - onRouteAdded - Master bedroom
16:56:07.268 - onRouteAdded - Living room chromecast
16:56:07.274 - onRouteChanged - Master bedroom
16:56:07.332 - onRouteAdded - Gym
16:56:07.337 - onRouteChanged - Living room chromecast
16:56:07.341 - onRouteChanged - Master bedroom
16:56:07.405 - onRouteChanged - Gym
16:56:07.409 - onRouteChanged - Living room chromecast
16:56:07.413 - onRouteAdded - Basement east bedroom
16:56:07.417 - onRouteChanged - Master bedroom
16:56:07.517 - onRouteChanged - Gym
16:56:07.522 - onRouteChanged - Living room chromecast
16:56:07.526 - onRouteChanged - Basement east bedroom
16:56:07.530 - onRouteChanged - Master bedroom
As you can see the code for discovering a Chromecast is ridiculously simple, I don't see where the mistake could be. As far as I know these are all the lines needed:
mMediaRouter = createMediaRouter(getApplicationContext());
mMediaRouterCallback = new MediaRouterCallback();
mMediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(
CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
.build();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
I should add that for this particular user if I change MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY
to MediaRouter.CALLBACK_FLAG_FORCE_DISCOVERY
then it works fine.
So what is wrong with my discovery code?
Thanks.