際際滷

際際滷Share a Scribd company logo
Desenvolver 
para 
Chromecast
O que 辿?
O que 辿?
O que 辿?
O que NO 辿?
Cen叩rio t鱈pico 
Image source: Google Developers webpage
Algumas 
Particularidades 
mDNS 
OTA updates autom叩ticos 
Android / ChromeOS 
Pairing com PIN / Ultrassons
Google Cast 
 a tecnologia que 
permite criar 
experi棚ncias multi-ecr達 
onde o utilizador envia 
e controla o conte炭do. 
Chromecast 
 um dispositivo que implementa 
esta tecnologia!
Existem 2 tipos 
de aplica巽探es 
 Sender Applications 
 Receiver Applications
Sender 
Applications
Android Dev 
Setup 
 Habilitiar desenvolvimento no dispositivo 
 No projecto: 
 Incluir bibliotecas de desenvolvimento 
 Algum boilerplate code 
 Cast Button, Conectividade, ...
Chromecast Dev Mode
Bibliotecas necess叩rias 
compile 'com.android.support:appcompat-v7:20.0.0' 
compile 'com.android.support:mediarouter-v7:20.0.0' 
compile 'com.google.android.gms:play-services:6.1.11'
AndroidManifest 
<meta-data 
android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version" /> 
android:theme="@style/Theme.AppCompat"
Sender App lifecycle 
Inicializar API 
Descobrir e conectar 
Iniciar Receiver App 
Receber e enviar msgs 
Desconectar
Design Guidelines 
Mais info em: http://goo.gl/NFoHXa
Google Cast Badge 
http://goo.gl/NFoHXa
Google Cast Store
Sender APP code 
The interesting 
parts :)
Inicializar API 
public class MainActivity extends ActionBarActivity 
@Override onCreate() 
// Configure Cast device discovery 
mMediaRouter = MediaRouter.getInstance(getApplicationContext()); 
mMediaRouteSelector = new MediaRouteSelector.Builder() 
.addControlCategory( 
CastMediaControlIntent.categoryForCast(getResources() 
.getString(R.string.app_id))).build(); 
mMediaRouterCallback = new MyMediaRouterCallback(); 
@Override onResume() 
// Start media router discovery 
mMediaRouter.addCallback(mMediaRouteSelector, 
mMediaRouterCallback, 
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
Cast Button 
<item 
android:id="@+id/menu_item" 
android:title="@string/title" 
app:actionProviderClass="android.support.v7.app. 
MediaRouteActionProvider" 
app:showAsAction="always"/>
Cast Button - Activity 
onCreateOptionsMenu(Menu menu) { 
super.onCreateOptionsMenu(menu); 
getMenuInflater().inflate(R.menu.main, menu); 
MenuItem mediaRouteMenuItem = 
menu.findItem(R.id.media_route_menu_item); 
MediaRouteActionProvider mediaRouteActionProvider = 
(MediaRouteActionProvider) MenuItemCompat 
.getActionProvider(mediaRouteMenuItem); 
// Set the MediaRouteActionProvider selector for device discovery. 
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector); 
return true; 
}
Comunica巽達o 
class MessagesHub implements MessageReceivedCallback { 
public String getNamespace() { 
return getString(R.string.namespace); 
} 
.... 
GoogleApiClient mApiClient = new GoogleApiClient.Builder(this) 
.addApi(Cast.API, apiOptionsBuilder.build()) 
.addConnectionCallbacks(mConnectionCallbacks) 
.addOnConnectionFailedListener(mConnectionFailedListener) 
.build(); 
mApiClient.connect();
Envio de mensagem 
(Sender App) 
Cast.CastApi.sendMessage(mApiClient, 
mMessageHub.getNamespace(), message) 
.setResultCallback(new ResultCallback<Status>() { 
@Override 
public void onResult(Status result) { 
if (!result.isSuccess()) { 
Log.e(TAG, "Sending message failed"); 
} 
} 
});
Recep巽達o da mensagem 
(Receiver App) 
window.castReceiverManager = 
cast.receiver.CastReceiverManager.getInstance(); 
window.messageBus = 
window.castReceiverManager.getCastMessageBus( 
'urn:x-cast:com.android.lx'); 
window.messageBus.onMessage = function(event) { 
document.getElementById("message").innerHTML= event.data; 
window.castReceiverManager.setApplicationState(text); 
window.messageBus.send(event.senderId, event.data); 
}
MAGIC! 
Sources at: http://goo.gl/xuu9b8
Desligar 
try { 
Cast.CastApi.stopApplication(mApiClient, mSessionId); 
if (mMessageHub != null) { 
Cast.CastApi.removeMessageReceivedCallbacks( 
mApiClient, 
mMessageHub.getNamespace()); 
mMessageHub = null; 
} 
} catch (IOException e) { 
Log.e(TAG, "Exception while removing channel", e); 
} 
mApiClient.disconnect();
Por: Pedro Veloso (http://goo.gl/5y4IZQ) 
際際滷s dispon鱈veis em : http://goo.gl/0xvjxr

More Related Content

Desenvolver para Chromecast

  • 5. O que NO 辿?
  • 6. Cen叩rio t鱈pico Image source: Google Developers webpage
  • 7. Algumas Particularidades mDNS OTA updates autom叩ticos Android / ChromeOS Pairing com PIN / Ultrassons
  • 8. Google Cast a tecnologia que permite criar experi棚ncias multi-ecr達 onde o utilizador envia e controla o conte炭do. Chromecast um dispositivo que implementa esta tecnologia!
  • 9. Existem 2 tipos de aplica巽探es Sender Applications Receiver Applications
  • 11. Android Dev Setup Habilitiar desenvolvimento no dispositivo No projecto: Incluir bibliotecas de desenvolvimento Algum boilerplate code Cast Button, Conectividade, ...
  • 13. Bibliotecas necess叩rias compile 'com.android.support:appcompat-v7:20.0.0' compile 'com.android.support:mediarouter-v7:20.0.0' compile 'com.google.android.gms:play-services:6.1.11'
  • 14. AndroidManifest <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> android:theme="@style/Theme.AppCompat"
  • 15. Sender App lifecycle Inicializar API Descobrir e conectar Iniciar Receiver App Receber e enviar msgs Desconectar
  • 16. Design Guidelines Mais info em: http://goo.gl/NFoHXa
  • 17. Google Cast Badge http://goo.gl/NFoHXa
  • 19. Sender APP code The interesting parts :)
  • 20. Inicializar API public class MainActivity extends ActionBarActivity @Override onCreate() // Configure Cast device discovery mMediaRouter = MediaRouter.getInstance(getApplicationContext()); mMediaRouteSelector = new MediaRouteSelector.Builder() .addControlCategory( CastMediaControlIntent.categoryForCast(getResources() .getString(R.string.app_id))).build(); mMediaRouterCallback = new MyMediaRouterCallback(); @Override onResume() // Start media router discovery mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
  • 21. Cast Button <item android:id="@+id/menu_item" android:title="@string/title" app:actionProviderClass="android.support.v7.app. MediaRouteActionProvider" app:showAsAction="always"/>
  • 22. Cast Button - Activity onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.main, menu); MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item); MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat .getActionProvider(mediaRouteMenuItem); // Set the MediaRouteActionProvider selector for device discovery. mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector); return true; }
  • 23. Comunica巽達o class MessagesHub implements MessageReceivedCallback { public String getNamespace() { return getString(R.string.namespace); } .... GoogleApiClient mApiClient = new GoogleApiClient.Builder(this) .addApi(Cast.API, apiOptionsBuilder.build()) .addConnectionCallbacks(mConnectionCallbacks) .addOnConnectionFailedListener(mConnectionFailedListener) .build(); mApiClient.connect();
  • 24. Envio de mensagem (Sender App) Cast.CastApi.sendMessage(mApiClient, mMessageHub.getNamespace(), message) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status result) { if (!result.isSuccess()) { Log.e(TAG, "Sending message failed"); } } });
  • 25. Recep巽達o da mensagem (Receiver App) window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance(); window.messageBus = window.castReceiverManager.getCastMessageBus( 'urn:x-cast:com.android.lx'); window.messageBus.onMessage = function(event) { document.getElementById("message").innerHTML= event.data; window.castReceiverManager.setApplicationState(text); window.messageBus.send(event.senderId, event.data); }
  • 26. MAGIC! Sources at: http://goo.gl/xuu9b8
  • 27. Desligar try { Cast.CastApi.stopApplication(mApiClient, mSessionId); if (mMessageHub != null) { Cast.CastApi.removeMessageReceivedCallbacks( mApiClient, mMessageHub.getNamespace()); mMessageHub = null; } } catch (IOException e) { Log.e(TAG, "Exception while removing channel", e); } mApiClient.disconnect();
  • 28. Por: Pedro Veloso (http://goo.gl/5y4IZQ) 際際滷s dispon鱈veis em : http://goo.gl/0xvjxr