gpt4 book ai didi

wear-os - 从 Android Wear 向主机设备发送消息

转载 作者:行者123 更新时间:2023-12-03 10:47:48 26 4
gpt4 key购买 nike

我正在编写一个自定义 Android Wear 应用程序,该应用程序应该向连接的主机设备(电话)发送一次性消息。通过 API 挖掘,我发现以下教程应该可以很好地工作:http://developer.android.com/training/wearables/data-layer/messages.html

我的 Android 应用程序有一个 WearableListenerService,我的 Android Wear 应用程序使用 Message API 触发消息。根据记录以下方法连接模拟器时会调用 Wea​​rableListenerService ,因此我很确定该服务连接良好

@Override
public void onPeerConnected(Node peer) {
super.onPeerConnected(peer);

String id = peer.getId();
String name = peer.getDisplayName();

Log.d(LOG_TAG, "Connected peer name & ID: " + name + "|" + id);
}

日志输出:
/AndroidWearListenerService(19892): Connected peer name & ID: facdc219-37f5-4326-8fa6-1c8b8d3b6669|facdc219-37f5-4326-8fa6-1c8b8d3b6669

但是, onMessageReceived 方法永远不会被触发:
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d(LOG_TAG, "MessageEvent received: " + messageEvent.getData());
//do work
}

这是我的 Android Wear 代码。我已经删除了大部分样板代码,只留下了必要的位
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();

googleApiClient.connect();

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
fireMessage();
}

private void fireMessage() {
// Send the RPC
PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient);
nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult result) {
for (int i = 0; i < result.getNodes().size(); i++) {
Node node = result.getNodes().get(i);
String nName = node.getDisplayName();
String nId = node.getId();
Log.d(TAG, "Node name and ID: " + nName + " | " + nId);

Wearable.MessageApi.addListener(googleApiClient, new MessageApi.MessageListener() {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d(TAG, "Message received: " + messageEvent);
}
});

PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),
PATH, null);
messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
Status status = sendMessageResult.getStatus();
Log.d(TAG, "Status: " + status.toString());
if (status.getStatusCode() != WearableStatusCodes.SUCCESS) {
alertButton.setProgress(-1);
label.setText("Tap to retry. Alert not sent :(");
}
}
});
}
}
});
}
});
}

日志记录似乎表明消息已成功发送,但 Android 应用程序的 WearableListenerService.onMessageReceived 从未触发。
D/MyWearApp MyActivity( 2014): Node name and ID: a2ba665d-a559-4a95-91d2-c16fc7873e28 | a2ba665d-a559-4a95-91d2-c16fc7873e28
D/MyWearApp MyActivity( 2014): Status: Status{statusCode=SUCCESS, resolution=null}

有任何想法吗?

最佳答案

您是否确保两个应用程序的“ applicationId ”相同,即 Android Wear 上的应用程序和手机上的应用程序?

关于wear-os - 从 Android Wear 向主机设备发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24595170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com