How can I get list of all cast devices using cast

2019-08-21 18:03发布

问题:

I am struggling to get list of all chrome cast devices available in my network, I am able to make it work with there default implementation of adding a media router button in the action bar. What I want to achieve is to get list of all cast devices available including Fire stick and smart TV (basically all the devices which supports DIAL protocol).

My list will contain all chrome cast devices and all DIAL protocol supported devices. I am using cast 3 SDK provided in android.

回答1:

    MediaRouter router = 
    MediaRouter.getInstance(a context);
    List<MediaRouter.RouteInfo> routes = router.getRoutes();

    List<CastDevice> devices = new ArrayList<>();

    for (MediaRouter.RouteInfo routeInfo : routes) {
        CastDevice device = CastDevice.getFromBundle(routeInfo.getExtras());
        if (device != null) {
            devices.add(device);
        }
    }