如何知道连接到 chromecast 的设备

How to know that a device connected to chromecast

我正在开发可以通过 remote display 将一些数据投射到 Chromecast 上的应用。在我的应用程序中,我可以有用户可以离开屏幕的行为,一段时间后 return 再有一次。但我必须知道该设备已连接到 Chromecast,因为应用必须以这种方式显示其他数据。下面两个 类 我用来连接 Chromecast(只有这两个 类,我没有使用任何其他东西)。我想也许 MediaRouter 有一些关于连接的数据,但我没有找到任何东西。所以我的问题是,如何检测当前设备是否已连接到 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());
    }


}

检查 MediaRouter.RouteInfo which you get in the onRouteSelected 回调。 getDeviceType() 应该是正确的选择。

终于,我找到了管理它的方法。它也是来自 cast remote 示例的解决方案。不知道为什么之前没看)

private boolean isRemoteDisplaying() {
    return CastRemoteDisplayLocalService.getInstance() != null;
}

如果 CastRemoteDisplayLocalService 正在工作,它 returns 为真。