My Samsung device running KitKat(19) has a "Screen Mirroring" option which finds and syncs with nearby compatible displays. I would like to implement this feature programatically.
I have experimented with both the MediaRoute and DisplayManager classes. Both succeed in detecting nearby displays, but only AFTER I manually turn on screen mirroring. I would like them to detect displays automatically. Is there something I'm missing to initiate a scan or to kick WifiDisplayController into gear?
Here is my implementation of MediaRouter:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
output = (TextView) findViewById(R.id.output);
mMediaRouter = MediaRouter.getInstance(this);
mSelector = new MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build();
}
@Override
public void onResume() {
super.onResume();
mMediaRouter.addCallback(mSelector, mMRCallback,
MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
}
@Override
public void onPause() {
super.onPause();
mMediaRouter.removeCallback(mMRCallback);
}
protected void onDestroy() {
super.onDestroy();
}
private MediaRouter.Callback mMRCallback = new MediaRouter.Callback() {
@Override
public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteAdded(router, route);
String outputText = router.getRoutes().size() + " Routes Detected\n";
List routes = router.getRoutes();
for (int i=0;i<routes.size(); i++) {
outputText = outputText + routes.get(i) + "\n";
}
output.setText(outputText);
}
@Override
public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteRemoved(router, route);
output.setText("Route Removed: " + route + "\n"
+ router.getRoutes().size() + " routes detected.");
}
@Override
public void onProviderAdded(MediaRouter router, MediaRouter.ProviderInfo provider) {
super.onProviderAdded(router, provider);
String outputText = router.getProviders().size() + " Providers Detected\n";
List providers = router.getProviders();
for (int i=0;i<providers.size(); i++) {
outputText = outputText + providers.get(i) + "\n";
}
output.setText(outputText);
}
@Override
public void onProviderRemoved(MediaRouter router, MediaRouter.ProviderInfo provider) {
super.onProviderRemoved(router, provider);
output.setText("Provider removed: " + provider + "\n"
+ router.getProviders().size() + " providers detected.");
}
@Override
public void onProviderChanged(MediaRouter router, MediaRouter.ProviderInfo provider) {
super.onProviderChanged(router, provider);
}
};
And here is my implementation of DisplayManager:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
displayManager = (DisplayManager)
getSystemService(DISPLAY_SERVICE); }
@Override
public void onResume() {
super.onResume();
displayManager.registerDisplayListener(displayListener,handler);
displayManager.getDisplays();
}
@Override
public void onPause() {
super.onPause();
displayManager.unregisterDisplayListener(displayListener);
}
private DisplayManager.DisplayListener displayListener = new DisplayManager.DisplayListener() {
@Override
public void onDisplayAdded(int displayId) {
output.setText("Display added: " + displayId);
}
@Override
public void onDisplayRemoved(int displayId) {
output.setText("Display removed: " + displayId);
}
@Override
public void onDisplayChanged(int displayId) {
}
};
I hope this will help you, done screen mirroring with the cast settings, it uses your device's cast service.
public void enablingWiFiDisplay() {
try {
startActivity(new Intent("android.settings.WIFI_DISPLAY_SETTINGS"));
return;
} catch (ActivityNotFoundException activitynotfoundexception) {
activitynotfoundexception.printStackTrace();
}
try {
startActivity(new Intent("android.settings.CAST_SETTINGS"));
return;
} catch (Exception exception1) {
Toast.makeText(getApplicationContext(), "Device not supported", Toast.LENGTH_LONG).show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With