Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending messages from Android Wear to host device

Tags:

I'm writing a custom Android Wear application that's supposed to fire a one-off message to the connected host device (the phone). Digging through the API, I found the following tutorial that should work well: http://developer.android.com/training/wearables/data-layer/messages.html

My Android app has a WearableListenerService and my Android Wear app fires off messages using the Message API. The WearableListenerService get's called when the emulator gets connected based on logging the following method, so I'm pretty sure the service is wired up fine

@Override public void onPeerConnected(Node peer) {     super.onPeerConnected(peer);      String id = peer.getId();     String name = peer.getDisplayName();      Log.d(LOG_TAG, "Connected peer name & ID: " + name + "|" + id); } 

Log output:

/AndroidWearListenerService(19892): Connected peer name & ID: facdc219-37f5-4326-8fa6-1c8b8d3b6669|facdc219-37f5-4326-8fa6-1c8b8d3b6669 

However, the onMessageReceived method never gets triggered:

@Override public void onMessageReceived(MessageEvent messageEvent) {     Log.d(LOG_TAG, "MessageEvent received: " + messageEvent.getData());     //do work } 

Here's my Android Wear code. I've removed most of the boiler plate code leaving only the necessary bits

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_my);      final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)             .addApi(Wearable.API)             .build();      googleApiClient.connect();      final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);     stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {         @Override         public void onLayoutInflated(WatchViewStub stub) {             fireMessage();         }          private void fireMessage() {             // Send the RPC             PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient);             nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {                 @Override                 public void onResult(NodeApi.GetConnectedNodesResult result) {                     for (int i = 0; i < result.getNodes().size(); i++) {                         Node node = result.getNodes().get(i);                         String nName = node.getDisplayName();                         String nId = node.getId();                         Log.d(TAG, "Node name and ID: " + nName + " | " + nId);                          Wearable.MessageApi.addListener(googleApiClient, new MessageApi.MessageListener() {                             @Override                             public void onMessageReceived(MessageEvent messageEvent) {                                 Log.d(TAG, "Message received: " + messageEvent);                             }                         });                          PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),                                 PATH, null);                         messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {                             @Override                             public void onResult(MessageApi.SendMessageResult sendMessageResult) {                                 Status status = sendMessageResult.getStatus();                                 Log.d(TAG, "Status: " + status.toString());                                 if (status.getStatusCode() != WearableStatusCodes.SUCCESS) {                                     alertButton.setProgress(-1);                                     label.setText("Tap to retry. Alert not sent :(");                                 }                             }                         });                     }                 }             });         }     }); } 

Logging seems to indicate the message was sent successfully, but the Android app's WearableListenerService.onMessageReceived never fires.

D/MyWearApp MyActivity( 2014): Node name and ID: a2ba665d-a559-4a95-91d2-c16fc7873e28 | a2ba665d-a559-4a95-91d2-c16fc7873e28 D/MyWearApp MyActivity( 2014): Status: Status{statusCode=SUCCESS, resolution=null} 

Any ideas?

like image 651
ebernie Avatar asked Jul 06 '14 11:07

ebernie


People also ask

How do you reply to a message on a smartwatch?

On your watch, swipe right on the Notification panel and then tap a message to view it. Swipe up or down and then tap a Quick response.

What are Android Developer messages?

Developer's message publishes notices about exceptions that affect user experience to respond to users promptly. The content is displayed under Comments on the game details page.


1 Answers

Did you ensure that the "applicationId" is the same for both apps, i.e. for the app on the Android Wear and the app on the phone?

like image 193
Dominique Avatar answered Sep 18 '22 13:09

Dominique