【发布时间】:2015-05-24 08:05:20
【问题描述】:
我只想在消息出现时从手持设备向智能手表 (Moto360) 发送消息。
我确保两个模块具有相同的包名、debug.key 和版本号。
监听器被添加到 onConnected() 中并且函数总是被调用。
这是我的手持式 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')
}
在我的手持活动中,消息应该这样发送:
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 文件如下所示:
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'
}
最终,这条消息应该会出现在我的手表上:
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)
我也尝试过 PhoneService 类from this answer。
任何建议我做错了什么?
编辑:我忘了提到消息已从手持设备成功发送,但未收到。
编辑 2:我的手持模块的名称是“app”而不是“mobile”,这可能是个问题吗?!这也是我从 eclipse 迁移的一个项目,用于添加可穿戴功能。
【问题讨论】:
-
我有同样的问题。你解决了吗?
标签: android events listener call message