Skip to content

List of casting devices not found sometime why? #137

Description

@nehal-brainvire

: 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)
}

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions