Azhar Bandri
Azhar Bandri

Reputation: 892

Broadcast Receiver for Cast Connectivity in Android

I don't want to use google cast code to connect devices from my app but needed to just check whether the phone is connected to any cast Device.

Do we get any Receiver/listener for this check?

Thanks!

EDIT: I have tried to get Cast State as following as mentioned by Chris,

  1. Add following in Manifest
<meta-data
            android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
            android:value=".CastOptionsProvider" />
  1. Create CastOptionProvider Class
public class CastOptionsProvider implements OptionsProvider {
    @Override
    public CastOptions getCastOptions(Context context) {
        return new CastOptions.Builder()
                .build();
    }
    @Override
    public List<SessionProvider> getAdditionalSessionProviders(Context context) {
        return null;
    }
}
  1. Reading state in MainActivity as,

CastContext mCastContext = CastContext.getSharedInstance(context); if (mCastContext != null) { int x = mCastContext.getCastState();

    }

Here Value of x is always "1" i.e. no Devices Available, eventhough the phone is connected to any Miracast Device.

Upvotes: 0

Views: 801

Answers (1)

Chris
Chris

Reputation: 2362

CastContext:

https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastContext

getCastState will tell you whether you're connected or you can call addCastStateListener to detect when the state changes.

Upvotes: 1

Related Questions