Sending messages from Android Wear to host device

前端 未结 7 1886
忘了有多久
忘了有多久 2020-12-02 19:27

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 foll

相关标签:
7条回答
  • 2020-12-02 19:33

    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?

    0 讨论(0)
  • 2020-12-02 19:35

    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.

    0 讨论(0)
  • 2020-12-02 19:37

    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.

    0 讨论(0)
  • 2020-12-02 19:38

    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.

    0 讨论(0)
  • 2020-12-02 19:41

    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.

    0 讨论(0)
  • 2020-12-02 19:54

    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());
    }
    
    0 讨论(0)
提交回复
热议问题