Denis Loh Denis Loh - 6 months ago 35
Android Question

Google Cast Android Sender: ResultCallback not called

I'm working through the Google Cast Sender application tutorial for Android and I got stuck when it comes to a connection to the Cast

CustomReceiver
.

I use the following
ConnectionCallbacks
implementation:

private class ConnectionCallbacks implements GoogleApiClient.ConnectionCallbacks {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "ConnectionCallbacks.onConnected");

LaunchOptions options = new LaunchOptions.Builder()
.setRelaunchIfRunning(false)
.build();
Cast.CastApi
.launchApplication(mApiClient, getString(R.string.google_cast_key), options)
.setResultCallback(new ResultCallback<Cast.ApplicationConnectionResult>() {
@Override
public void onResult(Cast.ApplicationConnectionResult result) {
Status status = result.getStatus();
if (status.isSuccess()) {
Log.i(TAG, "Success!!! " + result.getApplicationStatus());
} else {
Log.e(TAG, "No success!!! " + result.getApplicationStatus());
}
}
});
}

@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "ConnectionCallbacks.onConnectionSuspended: " + i);
}
}


And the CustomReceiver code:

<script>

window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();

var appConfig = new cast.receiver.CastReceiverManager.Config();

appConfig.statusText = 'Ready to play';

// 100 minutes for testing, use default 10sec in prod by not setting this value
appConfig.maxInactivity = 6000;

window.castReceiverManager.start();
console.log('Starting Receiver Manager');
document.getElementById("message").innerHTML='Starting Receiver Manager';
</script>


When I choose my chromecast from the dialog, my custom receiver app is launched on the chromecast and the app logs

11-05 22:38:00.470: I/MainActivity(8955): ConnectionCallbacks.onConnected


However, it never calls the
onResult
or
onConnectionSuspended
of the
ResultCallback
nor the
ConnectionFailedListener
. What am I missing?

Added log files

[ 0.340s] [cast.receiver.CastReceiverManager] Dispatching CastReceiverManager system ready event
desorona_cast_receiver.html:45 Received Ready event: {"id":"E9198EB3","name":"DESORONA","sessionId":"93EB8C53-4589-4D49-B578-E46D289B57F9","namespaces":[],"launchingSenderId":"40:com.example.test-145"}
cast_receiver.js:40 [ 0.353s] [cast.receiver.IpcChannel] IPC message sent: {"namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender","data":"{\"type\":\"setappstate\",\"statusText\":\"Application status is ready...\"}"}
cast_receiver.js:40 [ 0.373s] [cast.receiver.IpcChannel] Received message: {"data":"{\"level\":1.0,\"muted\":false,\"type\":\"volumechanged\"}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.375s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.378s] [cast.receiver.CastReceiverManager] Dispatching system volume changed event [1, false]
cast_receiver.js:40 [ 0.382s] [cast.receiver.IpcChannel] Received message: {"data":"{\"type\":\"visibilitychanged\",\"visible\":true}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.385s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.388s] [cast.receiver.CastReceiverManager] Dispatching visibility changed event true
cast_receiver.js:40 [ 0.392s] [cast.receiver.IpcChannel] Received message: {"data":"{\"standby\":false,\"type\":\"standbychanged\"}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.393s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.395s] [cast.receiver.CastReceiverManager] Dispatching standby changed event false

Answer

I solved my problem. It was not the sender, it was the receiver. Google does not state, that it is required to register a MessageBus when using CustomReceivers. The minimal ReceiverCode to get the Sender connected to the Receiver has to be:

  var namespace = 'urn:x-cast:com.denis_loh.desorona'

  window.onload = function() {
    cast.receiver.logger.setLevelValue(0);
    window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();       

    // Erstelle MessageBus zum Austauschen von Daten
    window.messageBus = window.castReceiverManager.getCastMessageBus(namespace);

    // Lege onMessage-Callback für den MessageBus fest.
    window.messageBus.onMessage = onMessage;

    // Starte den Receiver.
    window.castReceiverManager.start({statusText: "Application is starting"});
    console.log('Receiver Manager started');
  };

  function onMessage(event) {
    window.messageBus.send(event.senderId, event.data);
  }

However, I don't know yet, why this is required after all.