我想开始一项活动,检测我的WLAN中的所有chromecast设备。
首先,我实现了一个扩展Activity的自己的活动。
onCreate()-Method
看起来像这样:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_cast);
try
{
// get the parameters from intent
Bundle extras = getIntent().getExtras();
if (extras == null)
{
// this is just for testing purposes --> set a sample directory name
ms_ImageBasePath = "/storage/extSdCard/Bilder/2014/2014_08/2014_08_09";
ms_ImageFilename = "";
}
else
{
// get the data from the calling activity
ms_ImageBasePath = extras.getString(FWCastActivity.IMAGEPATH2CAST);
ms_ImageFilename = extras.getString(FWCastActivity.IMAGE2CAST);
}
// create the controls
mo_Button_left = (ImageButton)this.findViewById(R.id.cast_left_imageButton);
mo_Button_right = (ImageButton)this.findViewById(R.id.cast_right_imageButton);
mo_Button_stop = (ImageButton)this.findViewById(R.id.cast_stop_imageButton);
mo_SelectDeviceButton = (ImageButton)findViewById(R.id.cast_select_device_imageButton);
mo_SelectDeviceButton.setBackgroundColor(Color.WHITE);
mo_Filename = (TextView)findViewById(R.id.cast_filename_textView);
mo_Filename.setText("-");
mo_Headline = (TextView)findViewById(R.id.cast_headline_textView);
mo_ImageView = (ImageView)findViewById(R.id.cast_currentview_imageView);
// prepare the spinner which collects the routes
mo_CastDeviceSpinner = (Spinner) findViewById(R.id.cast_device_selection_spinner);
updateAdapter();
// prepare all the images for casting
if (prepareImages()==false)
{
// an error happened
throw new Exception("No images found in " + ms_ImageBasePath + " to cast");
}
// get the ip address --> important for casting images
ms_IPAddress = getLocalIpv4Address();
// create the handlers for the controls
createHandler();
// create the webserver which servers the images to chromecast
createWebServer(ms_ImageBasePath);
// create the mediarouter --> this starts searching for available devices
createMediaRouter();
}
catch (Exception e)
{
// problem in onCreate
doLog("Exception in onCreate: " + e.toString());
// to do: make sure process stops here...
}
}
createMediaRouter()-method
看起来像这样:
private void createMediaRouter()
{
try
{
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
// Create a MediaRouteSelector for the type of routes your app supports
//mMediaRouteSelector = new MediaRouteSelector.Builder().addControlCategory(CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)).build();
mMediaRouteSelector = new MediaRouteSelector.Builder().addControlCategory(CastMediaControlIntent.categoryForCast(APPID)).build();
// Create a MediaRouter callback for discovery events
mMediaRouterCallback = new CastFWMediaRouterCallback();
}
catch (Exception e)
{
doLog("Exception in createMediaRouter: " + e.toString());
}
}
回调类如下所示:
private class CastFWMediaRouterCallback extends MediaRouter.Callback
{
@Override
public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo info)
{
try
{
doLog("onRouteAdded: info.getName = " + info.getName());
// Add route to list of discovered routes
synchronized (this)
{
mRouteInfos.add(info);
mRouteNames.add(info.getName() + " (" + info.getDescription()+ ")");
updateAdapter();
doLog(" --> Found route: Now you can select route from list and start casting");
}
}
catch (Exception e)
{
doLog("Ex in MyMediaRouterCallback.onRouteAdded: " + e.toString());
}
}
@Override
public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo info)
{
try
{
doLog("onRouteRemoved: info=" + info);
// Remove route from list of routes
synchronized (this)
{
for (int i = 0; i < mRouteInfos.size(); i++) {
MediaRouter.RouteInfo routeInfo = mRouteInfos.get(i);
if (routeInfo.equals(info))
{
mRouteInfos.remove(i);
mRouteNames.remove(i);
updateAdapter();
return;
}
}
}
}
catch (Exception e)
{
doLog("Ex in MyMediaRouterCallback.onRouteRemoved: " + e.toString());
}
}
@Override
public void onRouteSelected(MediaRouter router, RouteInfo info)
{
try
{
doLog("onRouteSelected: info=" + info.getName());
mSelectedDevice = CastDevice.getFromBundle(info.getExtras());
String routeId = info.getId();
doLog("route with id " + routeId + " selected --> now preparing API Client");
prepareApiClient();
}
catch (Exception e)
{
doLog("Exception in MyMediaRouterCallback.onRouteSelected: " + e.toString());
}
}
@Override
public void onRouteUnselected(MediaRouter router, RouteInfo info) {
doLog("onRouteUnselected: info=" + info);
teardown();
mSelectedDevice = null;
}
} // end of inner class
callback-class
中添加了onResume-method
:
@Override
protected void onResume()
{
super.onResume();
if (mMediaRouter!=null)
{
// Add the callback to start device discovery
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}
}
此代码首次使用时正常工作。它启动后,我的chromecast设备被检测到,我可以通过集成的Web服务器在我的智能手机上发送图像链接。图像显示在chromecast设备上 - 很棒。如果我完成活动并再次创建它,则不会再找到路线。
我已经在Google+上讨论了一些内容,但那里的人告诉我在StackOverflow上发布我的代码。得到一些帮助会很棒。我还要感谢一段代码,其中手动检测chromecast设备。