
Android Wear Docs, Release 1.1
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleClient).await();
for (Node node : nodes.getNodes()) {
// Construct a DataRequest and send over the data layer
PutDataMapRequest putDMR = PutDataMapRequest.create(path);
putDMR.getDataMap().putAll(dataMap);
PutDataRequest request = putDMR.asPutDataRequest();
DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient,request).await();
if (result.getStatus().isSuccess()) {
Log.v("myTag", "DataMap: " + dataMap + " sent to: " + node.getDisplayName());
} else {
// Log an error
Log.v("myTag", "ERROR: failed to send DataMap");
}
}
}
}
7.1.3 Add a Data Receiver
You can monitor the data layer for new data objects using either a listener service or listener activity. This section
explains how to implement a listener service for data objects.
1. Enable the listener service in the manifest file for the wear application.
<uses-feature android:name="android.hardware.type.watch" />
<application
...
<service android:name=".ListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
</application>
2. Create a listener in the wear application that extends the WearableListenerService and implements onDat-
aChanged. This example filters incoming data events for those of TYPE_CHANGED, checks for a data path of
“/wearable_data”, then logs the data item to the debug output.
public class ListenerService extends WearableListenerService {
private static final String WEARABLE_DATA_PATH = "/wearable_data";
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
DataMap dataMap;
for (DataEvent event : dataEvents) {
// Check the data type
if (event.getType() == DataEvent.TYPE_CHANGED) {
// Check the data path
String path = event.getDataItem().getUri().getPath();
if (path.equals(WEARABLE_DATA_PATH)) {}
dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
Log.v("myTag", "DataMap received on watch: " + dataMap);
34 Chapter 7. Data Layer DataMap Objects
Comentarios a estos manuales