2017-11-27 27 views
0

내 네트워크에서 사용할 수있는 모든 크롬 캐스트 장치 목록을 얻으려고 애 쓰고 있습니다. 작업 표시 줄에 미디어 라우터 단추를 추가하는 기본 구현이 가능하도록 만들 수 있습니다. 필자가 원하는 것은 파이어 스틱과 스마트 TV (기본적으로 DIAL 프로토콜을 지원하는 모든 장치)를 포함한 사용 가능한 모든 캐스트 장치 목록을 얻는 것입니다.캐스트 SDK를 사용하여 모든 전송 장치 목록을 얻으려면 어떻게해야합니까?

내 목록에는 모든 크롬 캐스트 장치 및 모든 DIAL 프로토콜 지원 장치가 포함됩니다. 안드로이드에 제공된 캐스트 3 SDK를 사용하고 있습니다.

답변

0

시도해보십시오. codeSO post에서 제안되었습니다.

/* 
* Copyright (C) 2014 Google Inc. All Rights Reserved. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.example.mediarouter; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v7.app.MediaRouteButton; 
import android.support.v7.media.MediaRouteSelector; 
import android.support.v7.media.MediaRouter; 
import android.support.v7.media.MediaRouter.RouteInfo; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

import com.google.android.gms.cast.CastDevice; 
import com.google.android.gms.cast.CastMediaControlIntent; 

/** 
* Sample activity to demonstrate the use of MediaRouteButton. This activity 
* does not have an action bar. This activity is responsible for setting the 
* visibility of the Cast button. 
* 
* @see http://developer.android.com/guide/topics/media/mediarouter.html 
*/ 
public class MediaRouterButtonActivity extends FragmentActivity { 

    private static final String TAG = MediaRouterButtonActivity.class 
      .getSimpleName(); 

    private MediaRouter mMediaRouter; 
    private MediaRouteSelector mMediaRouteSelector; 
    private MediaRouter.Callback mMediaRouterCallback; 
    private MediaRouteButton mMediaRouteButton; 
    private CastDevice mSelectedDevice; 
    private int mRouteCount = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.media_router_button); 

     mMediaRouter = MediaRouter.getInstance(getApplicationContext()); 
     // Create a MediaRouteSelector for the type of routes your app supports 
     mMediaRouteSelector = new MediaRouteSelector.Builder() 
       .addControlCategory(
         CastMediaControlIntent.categoryForCast(getResources() 
           .getString(R.string.app_id))).build(); 
     // Create a MediaRouter callback for discovery events 
     mMediaRouterCallback = new MyMediaRouterCallback(); 

     // Set the MediaRouteButton selector for device discovery. 
     mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button); 
     mMediaRouteButton.setRouteSelector(mMediaRouteSelector); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // Add the callback to start device discovery 
     mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, 
       MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); 
    } 

    @Override 
    protected void onPause() { 
     // Remove the callback to stop device discovery 
     mMediaRouter.removeCallback(mMediaRouterCallback); 
     super.onPause(); 
    } 

    private class MyMediaRouterCallback extends MediaRouter.Callback { 
     @Override 
     public void onRouteAdded(MediaRouter router, RouteInfo route) { 
      Log.d(TAG, "onRouteAdded"); 
      if (++mRouteCount == 1) { 
       // Show the button when a device is discovered. 
       mMediaRouteButton.setVisibility(View.VISIBLE); 
      } 
     } 

     @Override 
     public void onRouteRemoved(MediaRouter router, RouteInfo route) { 
      Log.d(TAG, "onRouteRemoved"); 
      if (--mRouteCount == 0) { 
       // Hide the button if there are no devices discovered. 
       mMediaRouteButton.setVisibility(View.GONE); 
      } 
     } 

     @Override 
     public void onRouteSelected(MediaRouter router, RouteInfo info) { 
      Log.d(TAG, "onRouteSelected"); 
      // Handle route selection. 
      mSelectedDevice = CastDevice.getFromBundle(info.getExtras()); 

      // Just display a message for now; In a real app this would be the 
      // hook to connect to the selected device and launch the receiver 
      // app 
      Toast.makeText(MediaRouterButtonActivity.this, 
        getString(R.string.todo_connect), Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onRouteUnselected(MediaRouter router, RouteInfo info) { 
      Log.d(TAG, "onRouteUnselected: info=" + info); 
      mSelectedDevice = null; 
     } 
    } 

} 

자세한 내용은 SO post을 확인하십시오.

자세한 정보는 공식 documentation에서 확인할 수 있습니다.

Google Cast SDK에는 응용 프로그램을 크게 활용할 수 있도록 API 라이브러리와 샘플 응용 프로그램 코드 이 포함되어 있습니다. 이러한 API는 API references에 설명되어 있으며 샘플 코드는 Sender ApplicationsReceiver Applications 개요에서 논의됩니다.