什么是使用PutDataMa prequest后的URI Wearable.DataApi.getDataItem()?URI、prequest、PutDataMa、getDataItem

2023-09-05 05:41:49 作者:我是纯爷们

我测试的耐磨的数据层API 的作为的 Android的教程。

有围绕DataItem,这只能有一个字节数组作为有效载荷,因此培训推荐使用PutDataMa$p$pquest,这似乎使用意图时要基本等同于一个捆绑(即序列化的地图)。你基本上创建这个类的一个实例,然后填充值,并将其发送。

There is a low level API based around DataItem, which can have only a byte array as payload, so the training recommends using PutDataMapRequest, which seems to be basically equivalent to a Bundle (i.e. a serializable map) when using Intents. You basically create an instance of this class, then fill the values, and send it.

private final static String DATA_PATH = "/testdata";

PutDataMapRequest dataMap = PutDataMapRequest.create(DATA_PATH);
dataMap.getDataMap().putInt(...);

PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, request);
pendingResult.setResultCallback(...);

现在,我要检查,如果这个数据是正确存放(用于测试,在手持设备本身,我并不关心耐磨现在)。适当的方法为这个位于 DataApi 类,这样我就可以拨打:

Now, I want to check if this data was stored correctly (for testing, on the handheld itself, I'm not concerned about the wearable right now). The appropriate methods for this are in the DataApi class, so I can call:

PendingResult<DataApi.DataItemResult> pending;
pending = Wearable.DataApi.getDataItem(mGoogleApiClient, uri);
pending.setResultCallback(...);

然后用 DataMapItem.fromDataItem()回调来获取里面的值。

and then use DataMapItem.fromDataItem() inside the callback to get the value.

现在的问题是:?什么是实际的URI请求DataItemResult

中的数据存储,因为如果我使用Wearable.DataApi.getDataItems(mGoogleApiClient)迭代的所有的存储的数据,它确实有,而且URI是:

The data is stored, because if I use Wearable.DataApi.getDataItems(mGoogleApiClient) to iterate over all stored data, it's indeed there, and the Uri is:

"wear://<some guid here>/testdata"

和使用此URI与 DataApi.getDataItem()返回正确的结果。但我无能,如何为生成它,因为我只使用了 / TESTDATA 部分创建 PutDataRequest ...

And using this Uri with DataApi.getDataItem() returns the correct result. But I'm clueless as to how to generate it, since I only used the /testdata part to create the PutDataRequest...

还是我做的事情不正确?

Or am I doing things incorrectly?

推荐答案

的URI的授权(被描述为&lt;在这里有些GUID&gt;在您的文章)是节点ID 它通过可< STRONG>节点API 。总之,你可以构造的URI如下。

The uri's authority (which is described as <some guid here> in your post) is Node Id which is available via Node API. In summary, you can construct the Uri as following.

private Uri getUriForDataItem() {
    // If you've put data on the local node
    String nodeId = getLocalNodeId();
    // Or if you've put data on the remote node
    // String nodeId = getRemoteNodeId();
    // Or If you already know the node id
    // String nodeId = "some_node_id";
    return new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).authority(nodeId).path("/path_to_data").build();
}

private String getLocalNodeId() {
    NodeApi.GetLocalNodeResult nodeResult = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
    return nodeResult.getNode().getId();
}

private String getRemoteNodeId() {
    HashSet<String> results = new HashSet<String>();
    NodeApi.GetConnectedNodesResult nodesResult =
            Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
    List<Node> nodes = nodesResult.getNodes();
    if (nodes.size() > 0) {
        return nodes.get(0).getId();
    }
    return null;
}