1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
page.title=Syncing Data Items
@jd:body
<div id="tb-wrapper">
<div id="tb">
<h2>This lesson teaches you to</h2>
<ol>
<li><a href="#SyncData">Sync Data with a Data Map</a></li>
<li><a href="#ListenEvents">Listen for Data Item Events</a></li>
</ol>
</div>
</div>
<p>
A <a href="{@docRoot}reference/com/google/android/gms/wearable/DataItem.html"><code>DataItem</code></a>
defines the data interface that the system uses to synchronize data between handhelds
and wearables. A <a href="{@docRoot}reference/com/google/android/gms/wearable/DataItem.html"><code>DataItem</code></a> generally
consists of the following items:</p>
<ul>
<li><b>Payload</b> - A byte array, which you can set with whatever data you wish, allowing you
to do your own object serialization and deserialization. The size of the payload is limited
to 100KB.</li>
<li><b>Path</b> - A unique string that must start with a forward slash (for instance,
<code>"/path/to/data"</code>)</li>
</ul>
<p>
You normally don't implement <a href="{@docRoot}reference/com/google/android/gms/wearable/DataItem.html"><code>DataItem</code></a>
directly. Instead, you:
<ol>
<li>Create a <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html"><code>PutDataRequest</code></a> object,
specifying a string path to uniquely identify the item.
</li>
<li>Call <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html#setData(byte[])">
<code>setData()</code></a> to set the payload.
</li>
<li>Call <a href="{@docRoot}reference/com/google/android/gms/wearable/DataApi.html#putDataItem(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.wearable.PutDataRequest)"><code>DataApi.putDataItem()</code></a> to request the system to create the data item.
</li>
<li>When requesting data items, the system returns objects
that properly implement the <a href="{@docRoot}reference/com/google/android/gms/wearable/DataItem.html"><code>DataItem</code></a> interface.
</li>
</ol>
<p>
However, instead of working with raw bytes using <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html#setData(byte[])"><code>setData()</code></a>,
we recommend you <a href="#SyncData">use a data map</a>, which exposes
a data item in an easy-to-use {@link android.os.Bundle}-like interface.
</p>
<h2 id="SyncData">Sync Data with a Data Map</h2>
<p>
When possible, use the <a href="{@docRoot}reference/com/google/android/gms/wearable/DataMap.html"><code>DataMap</code></a> class.
This approach lets you work with data items in the form of an Android {@link android.os.Bundle},
so object serialization and de-serialization is done for you, and you can manipulate data with key-value pairs.
</p>
<p>To use a data map:</p>
<ol>
<li>Create a
<a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataMapRequest.html"><code>PutDataMapRequest</code></a>
object, setting the path of the data item.
<p class="note"><b>Note:</b> The path string is a unique identifier for the
data item that allows you to access it from either side of the connection. The path must begin
with a forward slash. If you're using hierarchical data in your
app, you should create a path scheme that matches the structure of the data.
</p>
</li>
<li>Call
<a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataMapRequest.html#getDataMap()"><code>PutDataMapRequest.getDataMap()</code></a>
</a> to obtain a data map that you can set values on.</li>
<li>Set any desired values for the data map using the <code>put...()</code> methods, such as
<a href="{@docRoot}reference/com/google/android/gms/wearable/DataMap.html#putString(java.lang.String, java.lang.String)"><code>putString()</code></a>.
</li>
<li>Call <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataMapRequest.html#asPutDataRequest()"><code>PutDataMapRequest.asPutDataRequest()</code></a>
to obtain a <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html"><code>PutDataRequest</code></a> object.
</li>
<li>Call <a href="{@docRoot}reference/com/google/android/gms/wearable/DataApi.html#putDataItem(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.wearable.PutDataRequest)"><code>DataApi.putDataItem()</code></a> to request the system to create the data item.
<p class="note"><b>Note:</b>
If the handset and wearable devices are disconnected,
the data is buffered and synced when the connection is re-established.
</p>
</li>
</ol>
<p>The <code>increaseCounter()</code> method in the following example shows how to create a
data map and put data in it:</p>
<pre>
public class MainActivity extends Activity implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String COUNT_KEY = "com.example.key.count";
private GoogleApiClient mGoogleApiClient;
private int count = 0;
...
// Create a data map and put data in it
private void <strong>increaseCounter</strong>() {
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
}
...
}
</pre>
<p>For more information about handling the
<a href="{@docRoot}reference/com/google/android/gms/common/api/PendingResult.html">
<code>PendingResult</code></a> object, see
<a href="{@docRoot}training/wearables/data-layer/events.html#Wait">Wait for the Status of Data
Layer Calls</a>.</p>
<h2 id="ListenEvents">Listen for Data Item Events</h2>
<p>If one side of the data layer connection changes a data item, you probably want
to be notified of any changes on the other side of the connection.
You can do this by implementing a listener for data item events.</p>
<p>The code snippet in the following example notifies your app when the value of the
counter defined in the previous example changes:</p>
<pre>
public class MainActivity extends Activity implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String COUNT_KEY = "com.example.key.count";
private GoogleApiClient mGoogleApiClient;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onResume() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
<strong>Wearable.DataApi.addListener</strong>(mGoogleApiClient, this);
}
@Override
protected void onPause() {
super.onPause();
<strong>Wearable.DataApi.removeListener</strong>(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
@Override
public void <strong>onDataChanged</strong>(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/count") == 0) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
updateCount(dataMap.getInt(COUNT_KEY));
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
// DataItem deleted
}
}
}
// Our method to update the count
private void updateCount(int c) { ... }
...
}
</pre>
<p>This activity implements the
<a href="{@docRoot}reference/com/google/android/gms/wearable/DataApi.DataListener.html">
<code>DataItem.DataListener</code></a> interface. This activity adds itself as a listener
for data item events inside the <code>onConnected()</code> method and removes the listener
in the <code>onPause()</code> method.</p>
<p>You can also implement the listener as a service. For more information, see
<a href="{@docRoot}training/wearables/data-layer/events.html#Listen">Listen for Data Layer
Events</a>.</p>
|