Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

android - WearableListenerService onMessageReceived is not called on device

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 :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...