1page.title=Sending and Receiving Messages
2
3@jd:body
4
5<div id="tb-wrapper">
6<div id="tb">
7
8<h2>This lesson teaches you to</h2>
9<ol>
10  <li><a href="#SendMessage">Send a Message</a></li>
11  <li><a href="#ReceiveMessage">Receive a Message</a></li>
12</ol>
13</div>
14</div>
15
16<p>You send messages using the
17<a href="{@docRoot}reference/com/google/android/gms/wearable/MessageApi.html"><code>MessageApi</code></a>
18and attach the following items to the message:</p>
19
20<ul>
21  <li>An arbitrary payload (optional)</li>
22  <li>A path that uniquely identifies the message's action</li>
23</ul>
24<p>
25Unlike with data items, there is no syncing between the handheld and wearable apps.
26Messages are a one-way communication mechanism that's good for remote procedure calls (RPC),
27such as sending a message to the wearable to start an activity.</p>
28
29<h2 id="SendMessage">Send a Message</h2>
30
31<p>The following example shows how to send a message that indicates to the other
32side of the connection to start an activity.
33This call is synchronous and blocks processing until the message is received or until the request
34times out:</p>
35
36<p class="note"><b>Note:</b> Read more about asynchronous and synchronous calls
37to Google Play services and when to use each in
38<a href="{@docRoot}google/auth/api-client.html#Communicating">Communicate with Google Play Services</a>.
39</p>
40
41<pre>
42GoogleApiClient mGoogleApiClient;
43public static final String START_ACTIVITY_PATH = "/start/MainActivity";
44...
45
46private void sendStartActivityMessage(String nodeId) {
47    Wearable.MessageApi.sendMessage(
48      mGoogleApiClient, nodeId, START_ACTIVITY_PATH, new byte[0]).setResultCallback(
49          new ResultCallback&lt;SendMessageResult>() {
50              &#64;Override
51              public void onResult(SendMessageResult sendMessageResult) {
52                  if (!sendMessageResult.getStatus().isSuccess()) {
53                      Log.e(TAG, "Failed to send message with status code: "
54                              + sendMessageResult.getStatus().getStatusCode());
55                  }
56              }
57          }
58      );
59}
60</pre>
61
62<p>
63Here's a simple way to get a list of connected nodes that you can potentially
64send messages to:</p>
65
66<pre>
67private Collection&lt;String&gt; getNodes() {
68    HashSet &lt;String&gt;results = new HashSet&lt;String&gt;();
69    NodeApi.GetConnectedNodesResult nodes =
70            Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
71    for (Node node : nodes.getNodes()) {
72        results.add(node.getId());
73    }
74    return results;
75}
76</pre>
77
78<h2 id="ReceiveMessage">Receive a Message</h2>
79
80<p>
81To be notified of received messages, you implement the
82<a href="{@docRoot}reference/com/google/android/gms/wearable/MessageApi.MessageListener.html">
83<code>MessageListener</code></a> interface to provide a listener for message events. Then you register your
84listener with the
85<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)">
86<code>MessageApi.addListener()</code></a> method. This example shows how you might implement the listener
87to check the <code>START_ACTIVITY_PATH</code> that the previous example used to send the message.
88If this condition is <code>true</code>, a specific activity is started.
89</p>
90
91<pre>
92&#64;Override
93public void onMessageReceived(MessageEvent messageEvent) {
94    if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
95        Intent startIntent = new Intent(this, MainActivity.class);
96        startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
97        startActivity(startIntent);
98    }
99}
100</pre>
101
102<p>
103This is just a snippet that requires more implementation details. Learn about
104how to implement a full listener service or activity in
105<a href="{@docRoot}training/wearables/data-layer/events.html#Listen">Listening for Data Layer
106Events</a>.
107</p>