Google 转换 Android 发件人:未调用 ResultCallback
Google Cast Android Sender: ResultCallback not called
我正在完成 Android 的 Google Cast Sender 应用程序教程,但在连接到 Cast CustomReceiver
.
时卡住了
我使用以下 ConnectionCallbacks
实现:
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);
}
}
和 CustomReceiver 代码:
<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>
当我从对话框中选择我的 chromecast 时,我的自定义接收器应用程序在 chromecast 上启动并且应用程序记录
11-05 22:38:00.470: I/MainActivity(8955): ConnectionCallbacks.onConnected
但是,它从不调用 ResultCallback
或 ConnectionFailedListener
的 onResult
或 onConnectionSuspended
。我错过了什么?
已添加日志文件
[ 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
我解决了我的问题。它不是发送者,而是接收者。 Google 没有声明,使用 CustomReceivers 时需要注册 MessageBus。使发送方连接到接收方的最小 ReceiverCode 必须是:
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);
}
不过,我还不知道,到底为什么需要这个。
我正在完成 Android 的 Google Cast Sender 应用程序教程,但在连接到 Cast CustomReceiver
.
我使用以下 ConnectionCallbacks
实现:
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);
}
}
和 CustomReceiver 代码:
<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>
当我从对话框中选择我的 chromecast 时,我的自定义接收器应用程序在 chromecast 上启动并且应用程序记录
11-05 22:38:00.470: I/MainActivity(8955): ConnectionCallbacks.onConnected
但是,它从不调用 ResultCallback
或 ConnectionFailedListener
的 onResult
或 onConnectionSuspended
。我错过了什么?
已添加日志文件
[ 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
我解决了我的问题。它不是发送者,而是接收者。 Google 没有声明,使用 CustomReceivers 时需要注册 MessageBus。使发送方连接到接收方的最小 ReceiverCode 必须是:
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);
}
不过,我还不知道,到底为什么需要这个。