Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of casting devices not found sometime why? #137

Open
nehal-brainvire opened this issue Oct 9, 2024 · 0 comments
Open

List of casting devices not found sometime why? #137

nehal-brainvire opened this issue Oct 9, 2024 · 0 comments

Comments

@nehal-brainvire
Copy link

: MediaRouter.Callback() not getting called sometime ,discovery not working

class ScanCastingDevices(private val context: Context, private val myPreference: MyPreference) {
private var mRouter: MediaRouter? = null
private var mRoute: MediaRouter.RouteInfo? = null
private var castDevice: CastDevice? = null
var deviceInfo:TvDeviceInfo?=null

// Callback for media router
private val mediaRouterCallback = object : MediaRouter.Callback() {
    override fun onRouteAdded(router: MediaRouter, route: MediaRouter.RouteInfo) {
        Log.e("CastMediaRouter", "Route Added: $route")
        castDevice = CastDevice.getFromBundle(route.extras)

        if ((removeHyphens(deviceInfo?.serviceId.toString()) == castDevice?.deviceId) || (deviceInfo?.ipAddress?.equals(
                castDevice?.inetAddress?.hostAddress
            )) == true
        ) {
            myPreference.setCastingDeviceInfo(CAST_DEVICE_INFO, castDevice)
            Log.e("stored device info",myPreference.getCastingDeviceInfo(CAST_DEVICE_INFO)?.inetAddress?.hostAddress.orEmpty())
            stopDiscovery()
        }
    }

    override fun onRouteRemoved(router: MediaRouter, route: MediaRouter.RouteInfo) {
        Log.e("CastMediaRouter", "onRouteRemoved$route : ")

        castDevice = CastDevice.getFromBundle(route.extras)
    }
}

// Initialize MediaRouter
fun initialize() {
     deviceInfo= Utils.getConnectedDeviceInfo(myPreference)
    Log.e("CastMediaRouter_ConnectedDeviceInfo",deviceInfo.toString())
    Log.e("CastMediaRouter", "initialize")

    mRouter = MediaRouter.getInstance(context)
    Log.e("CastMediaRouter", "initialize_startDiscovery : ")

    startDiscovery()
}

// Start discovery for Cast devices
private fun startDiscovery() {
    Log.e("CastMediaRouter", "startDiscovery : ")

    val selector = MediaRouteSelector.Builder()
        .addControlCategory(CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
        .build()
    mRouter?.addCallback(selector, mediaRouterCallback, MediaRouter.CALLBACK_FLAG_FORCE_DISCOVERY)
}

fun stopDiscovery(){
    Log.e("CastMediaRouter", "stopDiscovery : ")

    mRouter?.removeCallback(mediaRouterCallback)
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant