Sending messages from Android Wear to host device

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

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?

回答1:

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?



回答2:

I had the same problem when I added Android Wear support for an existing app. However after way to many hours of frustration. I discovered the problem.

I forgot to add the signing parts from the build.gradle in the device app in the wear app. So make sure the buildTypes part and the signingConfigs part are the same in both apps.



回答3:

I have had a similar issue as discussed here https://plus.google.com/116088533685577893615/posts/deCyoF6ALvV. Based on my experiments it seems (although not documented) that the package name of the watch app needs to be the same as of the handheld app. I have created an example project for the message api here https://github.com/petrnalevka/wear.



回答4:

Try add await() at the end of your sendMessage() method.

PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),                                 PATH, null).await(); 

The previous answer below is for sending message only when the Activity in Android device (mobile) is active.

Since you are trying to send message from Android Wear to Android device, the message listener should be added in the Activity in the Android device not in Android Wear, the following codes should be added into MainActivity in Android (mobile)

final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)         .addApi(Wearable.API)         .build();  googleApiClient.connect();  Wearable.MessageApi.addListener(googleApiClient, new MessageApi.MessageListener() {          @Override          public void onMessageReceived(MessageEvent messageEvent) {                   Log.d(TAG, "Message received: " + messageEvent.getPath());          } }); 

You can also try the simpler SendMessage() method

SendMessageResult result = Wearable.MessageApi.sendMessage(         mGoogleApiClient, node.getId(), "STRING TO BE SENT", null).await(); if (!result.getStatus().isSuccess()) {     Log.e(TAG, "ERROR: failed to send Message: " + result.getStatus()); } 


回答5:

I have some sample code where I have the messages working, from both wear to device and device to wear. https://github.com/kentarosu/AndroidWearAndLIFX



回答6:

A few months ago, I had the same problem while working with android wear.My issue was - different signatures(SHA fingerprints generated after signing apks) of both apk's while application keys were same.Refer to link below:

OnMessageReceived not called in WearableListenerService

Hope this helps someone.



回答7:

The usual suspects are:

  • the applicationId as others have mentioned and
  • the signing certificates used

In the basic case, the following parts should be identical, in the gradle configurations of both apps.

defaultConfig {     applicationId = "com.your.domain"  }  signingConfigs {     debug {         storeFile file("../debug.keystore")     }     release {         storeFile file("../release.keystore")     } }  

Both apps need to use the same applicationId and be signed with the same certificate.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!