Fix declaration of var and incorrect method call (18617393) bug: 18864041 bug: 18617393 Change-Id: Ic31e44acfe7338f56acdbbd97ba8cc1e87042b53
107 lines
3.8 KiB
Plaintext
107 lines
3.8 KiB
Plaintext
page.title=Sending and Receiving Messages
|
|
|
|
@jd:body
|
|
|
|
<div id="tb-wrapper">
|
|
<div id="tb">
|
|
|
|
<h2>This lesson teaches you to</h2>
|
|
<ol>
|
|
<li><a href="#SendMessage">Send a Message</a></li>
|
|
<li><a href="#ReceiveMessage">Receive a Message</a></li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
|
|
<p>You send messages using the
|
|
<a href="{@docRoot}reference/com/google/android/gms/wearable/MessageApi.html"><code>MessageApi</code></a>
|
|
and attach the following items to the message:</p>
|
|
|
|
<ul>
|
|
<li>An arbitrary payload (optional)</li>
|
|
<li>A path that uniquely identifies the message's action</li>
|
|
</ul>
|
|
<p>
|
|
Unlike with data items, there is no syncing between the handheld and wearable apps.
|
|
Messages are a one-way communication mechanism that's good for remote procedure calls (RPC),
|
|
such as sending a message to the wearable to start an activity.</p>
|
|
|
|
<h2 id="SendMessage">Send a Message</h2>
|
|
|
|
<p>The following example shows how to send a message that indicates to the other
|
|
side of the connection to start an activity.
|
|
This call is synchronous and blocks processing until the message is received or until the request
|
|
times out:</p>
|
|
|
|
<p class="note"><b>Note:</b> Read more about asynchronous and synchronous calls
|
|
to Google Play services and when to use each in
|
|
<a href="{@docRoot}google/auth/api-client.html#Communicating">Communicate with Google Play Services</a>.
|
|
</p>
|
|
|
|
<pre>
|
|
GoogleApiClient mGoogleApiClient;
|
|
public static final String START_ACTIVITY_PATH = "/start/MainActivity";
|
|
...
|
|
|
|
private void sendStartActivityMessage(String nodeId) {
|
|
Wearable.MessageApi.sendMessage(
|
|
mGoogleApiClient, nodeId, START_ACTIVITY_PATH, new byte[0]).setResultCallback(
|
|
new ResultCallback<SendMessageResult>() {
|
|
@Override
|
|
public void onResult(SendMessageResult sendMessageResult) {
|
|
if (!sendMessageResult.getStatus().isSuccess()) {
|
|
Log.e(TAG, "Failed to send message with status code: "
|
|
+ sendMessageResult.getStatus().getStatusCode());
|
|
}
|
|
}
|
|
}
|
|
);
|
|
}
|
|
</pre>
|
|
|
|
<p>
|
|
Here's a simple way to get a list of connected nodes that you can potentially
|
|
send messages to:</p>
|
|
|
|
<pre>
|
|
private Collection<String> getNodes() {
|
|
HashSet <String>results = new HashSet<String>();
|
|
NodeApi.GetConnectedNodesResult nodes =
|
|
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
|
|
for (Node node : nodes.getNodes()) {
|
|
results.add(node.getId());
|
|
}
|
|
return results;
|
|
}
|
|
</pre>
|
|
|
|
<h2 id="ReceiveMessage">Receive a Message</h2>
|
|
|
|
<p>
|
|
To be notified of received messages, you implement the
|
|
<a href="{@docRoot}reference/com/google/android/gms/wearable/MessageApi.MessageListener.html">
|
|
<code>MessageListener</code></a> interface to provide a listener for message events. Then you register your
|
|
listener with the
|
|
<a href="{@docRoot}reference/com/google/android/gms/wearable/MessageApi.html#addListener(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.wearable.MessageApi.MessageListener)">
|
|
<code>MessageApi.addListener()</code></a> method. This example shows how you might implement the listener
|
|
to check the <code>START_ACTIVITY_PATH</code> that the previous example used to send the message.
|
|
If this condition is <code>true</code>, a specific activity is started.
|
|
</p>
|
|
|
|
<pre>
|
|
@Override
|
|
public void onMessageReceived(MessageEvent messageEvent) {
|
|
if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
|
|
Intent startIntent = new Intent(this, MainActivity.class);
|
|
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
startActivity(startIntent);
|
|
}
|
|
}
|
|
</pre>
|
|
|
|
<p>
|
|
This is just a snippet that requires more implementation details. Learn about
|
|
how to implement a full listener service or activity in
|
|
<a href="{@docRoot}training/wearables/data-layer/events.html#Listen">Listening for Data Layer
|
|
Events</a>.
|
|
</p> |