Sending messages from Android Wear to host device

三世轮回 提交于 2019-11-27 11:56:23

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?

ui-jakob

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.

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.

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());
}

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

Lakhan Sharma

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.

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.

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