blob: 15e552d75ae9867dfdce611d856f1b2c02f97b39 [file] [log] [blame]
Robert Ly2728c2a2014-06-15 22:00:13 -07001page.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 data items, there is no syncing between the handheld and wearable apps.
26Messages are a one-way communication mechanism that's meant for
27"fire-and-forget" tasks, such as sending a message to the wearable
28to start an activity. You can also use messages in request/response model
29where one side of the connection sends a message, does some work,
30sends back a response message.</p>
31
32<h2 id="SendMessage">Send a Message</h2>
33
34<p>The following example shows how to send a message that indicates to the other
35side of the connect to start an activity.
36This call is made synchronously, which blocks until the message
37is received or when the request times out:
38</p>
39
40<p class="note"><b>Note:</b> Read more about asynchronous and synchronous calls
41to Google Play services and when to use each in
Robert Lycfe75822014-06-29 16:38:21 -070042<a href="{@docRoot}google/auth/api-client.html#Communicating">Communicate with Google Play Services</a>.
Robert Ly2728c2a2014-06-15 22:00:13 -070043</p>
44
45<pre>
46Node node; // the connected device to send the message to
47GoogleApiClient mGoogleApiClient;
48public static final START_ACTIVITY_PATH = "/start/MainActivity";
49...
50
51 SendMessageResult result = Wearable.MessageApi.sendMessage(
52 mGoogleApiClient, node, START_ACTIVITY_PATH, null).await();
53 if (!result.getStatus().isSuccess()) {
54 Log.e(TAG, "ERROR: failed to send Message: " + result.getStatus());
55 }
56</pre>
57
58<p>
59Here's a simple way to get a list of connected nodes that you can potentially
60send messages to:</p>
61
62<pre>
63private Collection&lt;String&gt; getNodes() {
64 HashSet &lt;String&gt;results= new HashSet&lt;String&gt;();
65 NodeApi.GetConnectedNodesResult nodes =
66 Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
67 for (Node node : nodes.getNodes()) {
68 results.add(node.getId());
69 }
70 return results;
71}
72</pre>
73
74<h2 id="ReceiveMessage">Receiving a Message</h2>
75
76<p>
77
78To be notified of received messages, you implement a listener for message events.
79This example shows how you might do this by checking the <code>START_ACTIVITY_PATH</code>
80that the previous example used to send the message. If this condition is <code>true</code>,
81a specific activity is started.
82</p>
83
84<pre>
85&#64;Override
86public void onMessageReceived(MessageEvent messageEvent) {
87 if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
88 Intent startIntent = new Intent(this, MainActivity.class);
89 startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
90 startActivity(startIntent);
91 }
92}
93</pre>
94
95<p>
96This is just a snippet that requires more implementation details. Learn about
97how to implement a full listener service or activity in
Ricardo Cervera321e0302014-07-28 10:50:32 -070098<a href="{@docRoot}training/wearables/data-layer/events.html">Listening for Data Layer Events</a>.
Robert Ly2728c2a2014-06-15 22:00:13 -070099</p>