WearableListenerService onMessageReceived is not called on device
Asked Answered
D

4

9

I am trying to send a simple message from my Android wear app to my phone app using the Wearable.MessageApi.

This is my onConnected callback from GoogleApiClient on the Wear device.

final PendingResult<Status> status = Wearable.DataApi.addListener(googleApiClient, this);
status.setResultCallback(new ResultCallback<Status>() {
    @Override
    public void onResult(Status status) {
        if (!status.isSuccess()) {
            return;
        }

        NodeApi.GetConnectedNodesResult nodes =
                Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
        for (Node node : nodes.getNodes()) {
            System.out.println("Sending message: " + node.getDisplayName());
            final MessageApi.SendMessageResult result =
                    Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),
                            "request", "12345".getBytes())
                            .await();
            System.out.println("sent: " + result.getStatus().isSuccess());
        }
    }
});

And this is displaying the following when ran

Sending message: Nexus 6P
sent: true

And this is my registered service on my app:

public class MyWearableListenerService extends WearableListenerService {

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Toast.makeText(this, "Received message", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onPeerConnected(Node peer) {
        Toast.makeText(this, "Peer connected", Toast.LENGTH_LONG).show();
    }
}

I properly verified that the Peer connected toast is showing up when the emulator is connected to my device. I properly did the port forwarding to debug on wear emulator. I checked that my applicationId and package names are consistent across my app and wear app. However, I never get the onMessageReceived callback on my device.

Any suggestions are greatly appreciated! I've been debugging this for a whole day now :(

Davie answered 20/11, 2015 at 1:46 Comment(6)
Please have a look at this gist. It may help you to fix your issue gist.github.com/schwiz/84f14c94d4a95c3b77beGauvin
Thanks for sharing. I've checked everything against your gist, the only difference is that I am trying to send message from wear to device whereas you are trying to send it from device to wear. I don't think that would impact anything though.Davie
Alright. Let's do some magic. Try MANUALLY uninstall your app apk on both devices .. and test it againGauvin
tried, no luck :( uninstalled and reinstalled (app first, then wear) on both devices.. gah, pretty sure its just some configuration thats messed up which is causing the message to be dropped. i wish there is more logging :\Davie
Please checkout this library.. It's kinda thin layer for WearbleApi github.com/Mariuxtheone/Teleport Its source may be helpful for youGauvin
Thanks for all the help @ProblemSlover! I figured out my problem -___- stupid dev builds..Davie
D
14

Oh, good god.. I figured out my problem. I THOUGHT the applicationId was the same, but it turned out that I never set up build flavors on the wear module, so the two applicationIds were actually com.example.android and com.example.android.dev..

Hope this helps other people who ran into the same problem as me :\

Davie answered 20/11, 2015 at 18:43 Comment(0)
R
6

I was having the same issue, due to (very) poor and outdated documentation on the Android Developer website. I was adding a Wear app to an existing app that's been around for years. As such, I have been using a custom debug.keystore for years now in my main app.

When I made the Wear app, I did not update the build.gradle to use the same debug.keystore file as the regular app - once I did that, I started receiving messages from the Watch -> Phone!

Here is a checklist to review if you're having the same issue as me and the OP:

  1. Wear and Phone apps need same applicationId
  2. Same versionNumber and versionName between apps
  3. Signed by the same key (this was what fixed my issue)

I just copied the "signingConfigs" section from my app's build.gradle to the wear app's build.gradle

signingConfigs { debug { storeFile file('../app/debug.keystore') } }

This issue cost me an entire day, hopefully someone else finds this useful.

Roaring answered 22/6, 2018 at 20:43 Comment(3)
Thanks a ton. The documentation really needs to be more clear on this.Villa
Not all heroes wear capes, thank you so much!!Lowney
In my case, it was the version code and version name not matching. Many hours checking what could be wrong and finally it was simply this. Thanks a lot!Kernel
I
2

also struggeld with the same problem..

Make sure that you use at least version 18.0.0 in the wear module!

build.gradle:

   implementation 'com.google.android.gms:play-services-wearable:18.0.0'
Iives answered 27/2, 2023 at 21:12 Comment(0)
D
1

Another possible trap, in your WearableListenerService, don't forget the call to super:

@Override
public void onCreate() {
    super.onCreate(); // <-- don't forget this
    ...
}
Downhaul answered 21/2, 2020 at 4:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.