Android手持设备和可穿戴设备无法接收消息

时间:2016-10-11 15:02:56

标签: android android-service wear-os

我试图从可穿戴设备向手持设备发送消息,然后从手持设备向可穿戴设备发送消息(两个模块都使用相同的代码和逻辑)。
摇篮

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

我得到了这个输出:

  

D / _wear_message_sender:onConnected:null
  D / _wear_message_sender:节点数1   D / _wear_message_sender:sendMessage(/ test):true

我已经使用了Moto 360和模拟器,邮件已发送但手持设备WearableListenerService onMessageReceived()未被调用。另一方面,我已经从手机发送了一条消息,并且未调用可穿戴设备onMessageReceived
我在这里错过了什么?

1 个答案:

答案 0 :(得分:3)

检查build.gradle for mobile和wear app中applicationID是否相同,否则Android将不知道应该发送哪些消息。您可以查看SO thread。另外,请确保两个应用中的buildTypes部分和signingConfigs部分相同。