Android掌上电脑和可穿戴设备未收到消息 [英] Android handheld and wearable not receiving messages

查看:109
本文介绍了Android掌上电脑和可穿戴设备未收到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从可穿戴设备向手持设备发送消息,然后从手持设备向可穿戴设备发送响应(两个模块都使用相同的代码和逻辑).
成绩:

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

我得到了这个输出:

D/_wear_message_sender:onConnected:空
D/_wear_message_sender:节点数为1
D/_wear_message_sender:sendMessage(/test):是

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

我使用了Moto 360和仿真器,已发送消息,但未调用手持设备WearableListenerService onMessageReceived().反之亦然,我已经从手机发送了一条消息,但可穿戴设备onMessageReceived没有被调用.
我在这里想念什么?

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?

推荐答案

检查是否 SO线程.还要确保两个应用程序中的buildTypes部分和signingConfigs部分相同.

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.

这篇关于Android掌上电脑和可穿戴设备未收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆