Robert Ly | 2728c2a | 2014-06-15 22:00:13 -0700 | [diff] [blame] | 1 | page.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> |
| 18 | and 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> |
| 25 | Unlike data items, there is no syncing between the handheld and wearable apps. |
| 26 | Messages are a one-way communication mechanism that's meant for |
| 27 | "fire-and-forget" tasks, such as sending a message to the wearable |
| 28 | to start an activity. You can also use messages in request/response model |
| 29 | where one side of the connection sends a message, does some work, |
| 30 | sends 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 |
| 35 | side of the connect to start an activity. |
| 36 | This call is made synchronously, which blocks until the message |
| 37 | is received or when the request times out: |
| 38 | </p> |
| 39 | |
| 40 | <p class="note"><b>Note:</b> Read more about asynchronous and synchronous calls |
| 41 | to Google Play services and when to use each in |
Robert Ly | cfe7582 | 2014-06-29 16:38:21 -0700 | [diff] [blame] | 42 | <a href="{@docRoot}google/auth/api-client.html#Communicating">Communicate with Google Play Services</a>. |
Robert Ly | 2728c2a | 2014-06-15 22:00:13 -0700 | [diff] [blame] | 43 | </p> |
| 44 | |
| 45 | <pre> |
| 46 | Node node; // the connected device to send the message to |
| 47 | GoogleApiClient mGoogleApiClient; |
| 48 | public 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> |
| 59 | Here's a simple way to get a list of connected nodes that you can potentially |
| 60 | send messages to:</p> |
| 61 | |
| 62 | <pre> |
| 63 | private Collection<String> getNodes() { |
| 64 | HashSet <String>results= new HashSet<String>(); |
| 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 | |
| 78 | To be notified of received messages, you implement a listener for message events. |
| 79 | This example shows how you might do this by checking the <code>START_ACTIVITY_PATH</code> |
| 80 | that the previous example used to send the message. If this condition is <code>true</code>, |
| 81 | a specific activity is started. |
| 82 | </p> |
| 83 | |
| 84 | <pre> |
| 85 | @Override |
| 86 | public 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> |
| 96 | This is just a snippet that requires more implementation details. Learn about |
| 97 | how to implement a full listener service or activity in |
Ricardo Cervera | 321e030 | 2014-07-28 10:50:32 -0700 | [diff] [blame] | 98 | <a href="{@docRoot}training/wearables/data-layer/events.html">Listening for Data Layer Events</a>. |
Robert Ly | 2728c2a | 2014-06-15 22:00:13 -0700 | [diff] [blame] | 99 | </p> |