Android handheld and wearable not receiving messag

2019-07-25 02:56发布

问题:

I'm trying to send a message from a wearable to a handheld and then a response from the handheld to the wearable (both modules use the same code and logic).
Gradle:

compile 'com.google.android.support:wearable:2.0.0-alpha2'
compile 'com.google.android.gms:play-services-wearable:9.6.1'

...

classpath 'com.android.tools.build:gradle:2.2.1'
classpath 'com.google.gms:google-services:3.0.0'

AndroidManifest:

    <application>
    ...
        <service android:name=".ReceiverService"
                 android:enabled="true"
                 android:exported="true">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED"/>
                <data android:scheme="wear" android:host="*"/>
            </intent-filter>
        </service>    
    </application>

ReceiverService:

public class ReceiverService extends WearableListenerService {
    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        Log.v(TAG, "onMessageReceived: " + messageEvent);
    }
}

MessageSender:

public class MessageSender implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "_wear_message_sender";
    private final GoogleApiClient mGoogleApiClient;

    private static class InstanceHolder {
        private static final MessageSender sInstance = new MessageSender();
    }

    private MessageSender() {
        mGoogleApiClient = new GoogleApiClient.Builder(WearApp.getInstance().getContext())
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    public static MessageSender getInstance() {
        return InstanceHolder.sInstance;
    }

    public void sendMessage(final String path, final String message) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.blockingConnect(Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);
                }    
                List<Node> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await().getNodes();
                Log.d(TAG, "Nodes count " + nodes.size());

                for (Node node : nodes) {
                    Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                            Log.d(TAG, "sendMessage(" + path + "): " + sendMessageResult.getStatus().isSuccess());
                        }
                    });
                }
                mGoogleApiClient.disconnect();
            }
        }).start();
    }
    ...
}

And I got this output:

D/_wear_message_sender: onConnected: null
D/_wear_message_sender: Nodes count 1
D/_wear_message_sender: sendMessage(/test): true

I've used a Moto 360 and Emulator, the message is sent but the handheld WearableListenerService onMessageReceived() is not called. Same goes the other way, I've sent a message from a mobile phone and the wearable onMessageReceived is not called.
What am I missing here?

回答1:

Check if the applicationID were the same in the build.gradle for mobile and wear app because otherwise Android wouldn't know to which the messages should be sent. You can check this SO thread. Also make sure that the buildTypes part and the signingConfigs part are the same in both apps.