Search code examples
androidchromecast

How to know that a device connected to chromecast


I'm working on app that can cast some data on Chromecast via remote display . In my app I can have behavior that user can leave screen and after some time return there one more time. But I have to know that device is connected to Chromecast, because app has to show another data in this way. Two classes below I use to connect to Chromecast(only this two classes, I don't use anything else). I thought maybe MediaRouter has some data about connection, but I didn't find anything. So my question is, How to detect that current device is connected to chromecast&

ChromecastController.java

public class ChromecastController implements MediaRouterCallback.IChromecastCallback {

    private MediaRouterCallback mMediaRouterCallback;
    private MediaRouteSelector mMediaRouteSelector;
    private MediaRouter mMediaRouter;

    public void initMediaRouter(MediaRouteButton mediaRouteButton, FragmentActivity activity, MediaRouterCallback.IChromecastCallback callback) {
        // Configure Cast device discovery
        mMediaRouter = MediaRouter.getInstance(M2MAppImpl.getContext());
        mMediaRouteSelector = new MediaRouteSelector.Builder()
                .addControlCategory(
                        CastMediaControlIntent.categoryForCast(CAST_ID ) )
                .build();
        mediaRouteButton.setRouteSelector(mMediaRouteSelector);
        mMediaRouterCallback = new MediaRouterCallback(activity, M2MApp.getContext());
    }

    public void onStart() {
        mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
    }

    public void onStop() {
        mMediaRouter.removeCallback(mMediaRouterCallback);
    }

}

MediaRouterCallback.java

public class MediaRouterCallback extends MediaRouter.Callback {

    public static final String TAG = MediaRouterCallback.class.getName();

    private CastDevice mSelectedDevice;
    private FragmentActivity activity;
    private Context mContext;

    public MediaRouterCallback(FragmentActivity activity, Context context){
        this.activity = activity;
        mContext = context;
    }

    @Override
    public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
        Log.d(TAG, "onRouteSelected " + info.getName());

        mSelectedDevice = CastDevice.getFromBundle( info.getExtras() );

        Intent intent = new Intent(mContext,
                activity.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent notificationPendingIntent = PendingIntent.getActivity(
                mContext, 0, intent, 0);

        CastRemoteDisplayLocalService.NotificationSettings settings =
                new CastRemoteDisplayLocalService.NotificationSettings.Builder()
                        .setNotificationPendingIntent(notificationPendingIntent).build();

        CastRemoteDisplayLocalService.startService(
                M2MApp.getContext(),
                PresentationService.class, ChromecastController.CAST_ID,
                mSelectedDevice, settings,
                new CastRemoteDisplayLocalService.Callbacks() {
                    @Override
                    public void onServiceCreated(CastRemoteDisplayLocalService castRemoteDisplayLocalService) {
                        Log.d(TAG, "CHROMECAST onServiceCreated");
                    }

                    @Override
                    public void onRemoteDisplaySessionStarted(
                            CastRemoteDisplayLocalService service) {
                        Log.d(TAG, "CHROMECAST onRemoteDisplaySessionStarted");
                        // initialize sender UI
                    }

                    @Override
                    public void onRemoteDisplaySessionError(
                            Status errorReason){
                        Log.d(TAG, "CHROMECAST onRemoteDisplaySessionError " + errorReason);
                    }
                });
    }

    @Override
    public void onRouteUnselected( MediaRouter router, MediaRouter.RouteInfo info ) {
        Log.d(TAG, "onRouteUnselected " + info.getName());
    }


}

Solution

  • Check the MediaRouter.RouteInfo which you get in the onRouteSelected callback. getDeviceType() should be the way to go.