有什么可以提高Wearable.MessageApi.sendMessage的可靠性的吗?它和猫一样稳定
Is there any to improve the reliability of Wearable.MessageApi.sendMessage? It's about as consistant as a cat
我使用的代码:
client.blockingConnect();
try {
Wearable.MessageApi.sendMessage(client,
nodeId, path, message.getBytes("UTF-16"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
client.disconnect();
变量 path 和 message 是字符串,仅包含它们的命名,client 和 nodeId 使用此代码设置(最新的 Android Wear 版本也需要修改容纳多个设备,但不是我正在处理的当前问题):
client = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.build();
while (nodeId.length() < 1) {
client.blockingConnect();
Wearable.NodeApi.getConnectedNodes(client).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult nodes) {
for (Node node : nodes.getNodes()) {
nodeId = node.getId();
//nodeName = node.getDisplayName();
haveId = true;
status = ConnectionStatus.connected;
}
}
});
client.disconnect();
我遇到的问题是有时有效,有时很快,有时延迟很长时间,有时根本不起作用。潮汐、月相、湿度、蝴蝶在世界的另一端拍打,不知道发生了什么变化。 Android wear 报告设备始终处于连接状态。有时消息是相同的值,但仍需要单独处理,因为当它们发生时,手表或手机响应很重要。
有没有办法提高可靠性?
我试过:
sendMessage(String.valueOf(System.currentTimeMillis()), "wake up!");
但有时也不会通过。
不,MessageApi
本质上是不可靠的。将其视为 UDP。如果你想快速传递消息并且你不介意它会失败,你可以使用它,因为你可以重复它(例如,用户在你的音乐应用程序中切换曲目 - 要么它有效,要么他必须按按钮)。
如果您需要可靠性,请使用DataApi
。它速度较慢,但可以保证最终的一致性。
如果您既想要速度又想要有保证的交付,请使用这两种方法 - 发送一条消息并使用相同的标记设置一个数据项。如果收到消息,则保留令牌并稍后忽略数据项。如果没有,数据项最终会触发动作。
编辑
文档指出,只有当节点已连接时,消息才会传递到该节点:
Messages are delivered to connected network nodes. A message is
considered successful if it has been queued for delivery to the
specified node. A message will only be queued if the specified node is
connected. The DataApi should be used for messages to nodes which are
not currently connected (to be delivered on connection).
我使用的代码:
client.blockingConnect();
try {
Wearable.MessageApi.sendMessage(client,
nodeId, path, message.getBytes("UTF-16"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
client.disconnect();
变量 path 和 message 是字符串,仅包含它们的命名,client 和 nodeId 使用此代码设置(最新的 Android Wear 版本也需要修改容纳多个设备,但不是我正在处理的当前问题):
client = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.build();
while (nodeId.length() < 1) {
client.blockingConnect();
Wearable.NodeApi.getConnectedNodes(client).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult nodes) {
for (Node node : nodes.getNodes()) {
nodeId = node.getId();
//nodeName = node.getDisplayName();
haveId = true;
status = ConnectionStatus.connected;
}
}
});
client.disconnect();
我遇到的问题是有时有效,有时很快,有时延迟很长时间,有时根本不起作用。潮汐、月相、湿度、蝴蝶在世界的另一端拍打,不知道发生了什么变化。 Android wear 报告设备始终处于连接状态。有时消息是相同的值,但仍需要单独处理,因为当它们发生时,手表或手机响应很重要。
有没有办法提高可靠性?
我试过:
sendMessage(String.valueOf(System.currentTimeMillis()), "wake up!");
但有时也不会通过。
不,MessageApi
本质上是不可靠的。将其视为 UDP。如果你想快速传递消息并且你不介意它会失败,你可以使用它,因为你可以重复它(例如,用户在你的音乐应用程序中切换曲目 - 要么它有效,要么他必须按按钮)。
如果您需要可靠性,请使用DataApi
。它速度较慢,但可以保证最终的一致性。
如果您既想要速度又想要有保证的交付,请使用这两种方法 - 发送一条消息并使用相同的标记设置一个数据项。如果收到消息,则保留令牌并稍后忽略数据项。如果没有,数据项最终会触发动作。
编辑 文档指出,只有当节点已连接时,消息才会传递到该节点:
Messages are delivered to connected network nodes. A message is considered successful if it has been queued for delivery to the specified node. A message will only be queued if the specified node is connected. The DataApi should be used for messages to nodes which are not currently connected (to be delivered on connection).