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 with data items, there is no syncing between the handheld and wearable apps. 26 Messages are a one-way communication mechanism that's good for remote procedure calls (RPC), 27 such 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 32 side of the connection to start an activity. 33 This call is synchronous and blocks processing until the message is received or until the request 34 times out:</p> 35 36 <p class="note"><b>Note:</b> Read more about asynchronous and synchronous calls 37 to 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> 42 GoogleApiClient mGoogleApiClient; 43 public static final String START_ACTIVITY_PATH = "/start/MainActivity"; 44 ... 45 46 private void sendStartActivityMessage(String nodeId) { 47 Wearable.MessageApi.sendMessage( 48 mGoogleApiClient, nodeId, START_ACTIVITY_PATH, new byte[0]).setResultCallback( 49 new ResultCallback<SendMessageResult>() { 50 @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> 63 Here's a simple way to get a list of connected nodes that you can potentially 64 send messages to:</p> 65 66 <pre> 67 private Collection<String> getNodes() { 68 HashSet <String>results = new HashSet<String>(); 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> 81 To 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 84 listener 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 87 to check the <code>START_ACTIVITY_PATH</code> that the previous example used to send the message. 88 If this condition is <code>true</code>, a specific activity is started. 89 </p> 90 91 <pre> 92 @Override 93 public 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> 103 This is just a snippet that requires more implementation details. Learn about 104 how to implement a full listener service or activity in 105 <a href="{@docRoot}training/wearables/data-layer/events.html#Listen">Listening for Data Layer 106 Events</a>. 107 </p>