What is the Uri for Wearable.DataApi.getDataItem() after using PutDataMapRequest?
Asked Answered
M

1

16

I'm testing the Wearable Data Layer Api as described in the Android tutorial.

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(...);

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(...);

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

The problem is: what is the actual Uri to request the DataItemResult?

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"

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?

Marianomaribel answered 6/7, 2014 at 23:21 Comment(0)
S
25

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;
}
Scraper answered 7/7, 2014 at 9:20 Comment(5)
Perfect! I hadn't seen this. And I assume each connected device has its own node id?Marianomaribel
Yes it is. node id and item url are mentioned a little bit in this video as well. google.com/events/io/schedule/session/…Scraper
Yes, I saw that video after I read your answer. Thanks again!Marianomaribel
Actually the node can be omitted. If you use only the scheme and the path you will retrieve a DataItemBuffer containing all the DataItem elements at the given path. You can then iterate through them.Edholm
This doesnt work for me on the remote device... on the device the item is created it works but not on the other. BUT I am reciving onDataChanged events on the remote device. My Uris seem to be correct (the one I use to get the DataItem is the same as I get from the event in onDataChanged). Any suggestions? Additional info: Since I wasnt able to get Android Studio to work for a mobile project I'm developing the smartphone part using eclipse but for the wear part I use Android Studio.Schwerin

© 2022 - 2024 — McMap. All rights reserved.