尽管已连接到手持设备,但从未调用过onMessageReceived() [英] onMessageReceived() never called, although connected to handheld

查看:213
本文介绍了尽管已连接到手持设备,但从未调用过onMessageReceived()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在出现消息时将消息从手持设备发送到智能手表(Moto360).

I want to send Messages from a handheld device to a smartwatch (Moto360) only when a message comes up.

我确保两个模块具有相同的程序包名称,debug.key和版本号.

I made sure that both modules have the same package name, debug.key and version number.

将监听器添加到onConnected()中,并且始终调用该函数.

The listener gets added in onConnected() and the function is always called.

这是我的手持gradle.build:

Here is my Handheld gradle.build:

apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion "22.0.0"

defaultConfig {
    applicationId "de.bachelorthesis.important"
    minSdkVersion 9
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}

signingConfigs {
    debug {
        storeFile file("../debug.keystore")
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
  }
}

dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile files('libs/commons-io-2.4.jar')
compile files('libs/mapquest-android-sdk-1.0.5.jar')
compile files('libs/osmdroid-android-4.2.jar')
compile files('libs/slf4j-android-1.7.7.jar')
wearApp project(':wear')

}

在我的掌上电脑活动中,消息应这样发送:

In my handheld Activity the message should be sent like this:

 private void sendToSmartwatch(msg) {
    final String msg_arg = msg;

    new Thread(new Runnable() {
        public void run() {

            mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
                    .addApi(Wearable.API)
                    .build();

            ConnectionResult connectionResult =
                    mGoogleApiClient.blockingConnect(5, TimeUnit.SECONDS);

            NodeApi.GetLocalNodeResult nodes =
                    Wearable.NodeApi.getLocalNode(mGoogleApiClient).await(3, TimeUnit.SECONDS);


            com.google.android.gms.wearable.Node node = nodes.getNode();

            MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                    mGoogleApiClient, node.getId(), CONSTANT, msg_arg.getBytes()).await();
            Log.d("node:result: ", "" + result.getStatus());


            if (result.getStatus().isSuccess()) {
                // Log success
            }
            else {
                // Log an error
            }

            mGoogleApiClient.disconnect();
        }
    }).start();
}

Wearable gradle.build文件如下所示:

The Wearable gradle.build file looks like this:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
    applicationId "de.bachelorthesis.important"
    minSdkVersion 20
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

signingConfigs {
    debug {
        storeFile file("../debug.keystore")
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android.support:wearable:1.1.0'
compile 'com.google.android.gms:play-services-wearable:+'
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:support-v4:22.0.0'
}

最终,消息应该出现在我的手表上

And eventually, the message should come up here on my watch:

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

...

onCreate(){

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

mListener = new MessageApi.MessageListener() {
        @Override
        public void onMessageReceived(MessageEvent messageEvent) {
            if (messageEvent.getPath().equals(CONSTANT)) {
                //do stuff with the message
            }
         }
    };

}


@Override
public void onConnected(Bundle bundle) {
    Wearable.MessageApi.addListener(mGoogleApiClient, mListener);
}

我尝试了几种不同的侦听器,例如 ListenerService (也带有manifest.xml中的BIND_LISTENER)

I have tried several different Listeners like the ListenerService (also with the BIND_LISTENER in the manifest.xml)

我还尝试了从此答案中的方法..

Also I have tried the PhoneService Class from this answer.

有人建议我在做什么错吗?

Any suggestions what I am doing wrong?

我忘了提到消息已成功从掌上电脑发送,但是没有收到.

I forgot to mention that the message gets sent from the handheld successfully, but it is not received.

手持式模块的名称为"app"而不是"mobile"会不会是一个问题?这也是我从eclipse迁移而来的一个项目,以添加可穿戴功能.

EDIT 2: Could it be an issue, that my handheld module has the name "app" instead of "mobile"?! Also it is a project that i migrated from eclipse, to add the wearable feature.

推荐答案

所以,我自己弄清楚了:

So, i figured it out myself:

该项目正在提供帮助.

我认为常量起到了重要作用.这不是路,而是我随机设定的常数.还是不知道,

I think the Constants made the difference. It was no path, but a randomly set constant by me. Still not sure, tho.

这篇关于尽管已连接到手持设备,但从未调用过onMessageReceived()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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