Search code examples
androidgoogle-castchromecast

What flag should I use for Chromecast discovery?


I have an Android app which controls UPnP MediaRenderers, and I now want to modify it to use Chromecasts too.

As I have to mix UPnP and Chromecast and my app is not using ActionBarCompat, I can't use the Cast Companion Library, so I'm trying to integrate the Cast SDK by myself, building a custom button.

For device discovery, I'm using MediaRouter.addCallback but I don't really know if I should specify a flag. According to Javadoc, the third parameter is:

Flags to control the behavior of the callback. May be zero or a combination of CALLBACK_FLAG_PERFORM_ACTIVE_SCAN and CALLBACK_FLAG_UNFILTERED_EVENTS.

But the documentation of the flag says:

public static final int CALLBACK_FLAG_PERFORM_ACTIVE_SCAN

Flag for addCallback(MediaRouteSelector, MediaRouter.Callback): Actively scan for routes while this callback is registered.

When this flag is specified, the media router will actively scan for new routes. Certain routes, such as wifi display routes, may not be discoverable except when actively scanning. This flag is typically used when the route picker dialog has been opened by the user to ensure that the route information is up to date.

Active scanning may consume a significant amount of power and may have intrusive effects on wireless connectivity. **Therefore it is important that active scanning only be requested when it is actually needed to satisfy a user request to discover and select a new route**.

But according to the Cast UX Guidelines, I shouldn't show the Cast button in the ActionBar unless I know there are devices in the network. So I can't wait for an user request to use an active scan, because if the app doesn't discover the devices there would be no button for the user to touch and request routes!

This are my doubts:

-Should I use CALLBACK_FLAG_PERFORM_ACTIVE_SCAN before showing the and risk battery depletion but be sure that the button is going to be shown when it has to be? Is this what the standard apps do?

-Should I NOT use the flag and risk the button not appearing even when there are available Chromecasts?

-Should I use CALLBACK_FLAG_REQUEST_DISCOVERY instead? Is there much difference?

Thanks!


Solution

  • You can use the CALLBACK_FLAG_PERFORM_ACTIVE_SCAN flag and register a callback when you start your activity. Then framework will continue to re-scan every 30 seconds or so (I don't recall the exact number). The process is highly optimized so you shouldn't worry too much about battery consumption. When you are leaving your application, you can remove the callback.

    Edit: Due to the expansion of cast and introduction of mirroring, etc., it is now recommended to use the more conservative flag CALLBACK_FLAG_REQUEST_DISCOVERY