Java WearableListenerService-智能手机无法找到wear作为节点?

Java WearableListenerService-智能手机无法找到wear作为节点?,java,android,smartphone,wear-os,Java,Android,Smartphone,Wear Os,我把我的智能手机连接到一个磨损模拟器,我使用Android Studio emulator和my phone应用程序都使用了覆盖的onMessageReceived()的WearableListenerService,虽然emulator能够使用MessageApi成功地向我的手机发送消息,设备无法发回一个节点-尽管模拟器将我的手机作为节点查找,但根本找不到任何节点。我最初使用了MessageEvent.getSourceNodeId()方法,并将MessageEvent参数提供给onMessa

我把我的智能手机连接到一个磨损模拟器,我使用Android Studio

emulator和my phone应用程序都使用了覆盖的
onMessageReceived()
WearableListenerService
,虽然emulator能够使用
MessageApi
成功地向我的手机发送消息,设备无法发回一个节点-尽管模拟器将我的手机作为节点查找,但根本找不到任何节点。我最初使用了
MessageEvent.getSourceNodeId()
方法,并将
MessageEvent
参数提供给
onMessageReceived()
,但这也不起作用

我的计划是用向手机发送消息,并让手机在发送消息后立即将消息转发回手机

我是否需要在手机端做些什么来向我的穿戴设备发送信息?我遵循了许多指南的说明:两个应用程序ID相同,目录相同,清单很好,我使用了生成签名APK。。。还是没什么

谢谢

用于向手机发送信息的代码:

import android.content.Context;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class SendToWear {

public static final String TAG = "DEMO_WEAR_LOG";

private static GoogleApiClient sClient = null;

public static int sendMessage(Context context, String path, String data){
    Log.d(TAG, "Path: " + path);
    ArrayList<String> nodes;
    int sent, connectCount = 0;

    if(context == null || path == null){
        Log.d(TAG, "Context or Path is null.");
        return 0;
    }

    if(sClient == null) {
        sClient = new GoogleApiClient.Builder(context)
                .addApi(Wearable.API)
                .build();
    }
    sClient.connect();

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

    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "Failed to connect to sClient.");
        return 0;
    } else Log.d(TAG, "sClient connected!");

    // no nodes, loop useless
    nodes = getNodes(sClient);
    if(nodes.size() == 0) return 0;

    for(int i = sent = 0; i < nodes.size(); i++) {
        MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                sClient, nodes.get(i), path, data.getBytes()).await(3, TimeUnit.SECONDS);

        // was not able to send message
        if(result.getStatus().isSuccess())
            ++sent;
    }

    sClient.disconnect();

    Log.d(TAG, "SENT: " + sent);

    return sent;

}

private static ArrayList<String> getNodes(GoogleApiClient client) {
    Log.d(TAG, "in getNodes");
    ArrayList<String> results = new ArrayList<String>();

    NodeApi.GetConnectedNodesResult nodes =
            Wearable.NodeApi.getConnectedNodes(client).await(3, TimeUnit.SECONDS);

    if(nodes == null || !nodes.getStatus().isSuccess())
        return results;

    for (Node node : nodes.getNodes()) {
        results.add(node.getId());
    }

    return results;
}
}
导入android.content.Context;
导入android.util.Log;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.wearable.MessageApi;
导入com.google.android.gms.wearable.Node;
导入com.google.android.gms.wearable.NodeApi;
导入com.google.android.gms.wearable.wearable;
导入java.util.ArrayList;
导入java.util.concurrent.TimeUnit;
公共类SendToWear{
公共静态最终字符串TAG=“DEMO\u WEAR\u LOG”;
私有静态GoogleAppClient sClient=null;
公共静态int sendMessage(上下文上下文、字符串路径、字符串数据){
Log.d(标签,“路径:”+Path);
阵列列表节点;
已发送int,connectCount=0;
if(上下文==null | |路径==null){
d(标记“Context或Path为null”);
返回0;
}
if(sClient==null){
sClient=新的GoogleAppClient.Builder(上下文)
.addApi(可穿戴的.API)
.build();
}
sClient.connect();
连接结果连接结果=
客户端阻塞连接(5,时间单位秒);
如果(!connectionResult.isSuccess()){
Log.e(标记“未能连接到sClient”);
返回0;
}else Log.d(标记“sClient connected!”);
//没有节点,循环无效
nodes=getNodes(sClient);
如果(nodes.size()==0)返回0;
for(int i=sent=0;i
现在就用代码编辑。重申一下:当Wear向设备发送消息时,这个相同的代码会起作用,但设备不会使用上面的代码立即发送消息。如果仍然不能,请尝试Android SDK文件夹中提供的Wear示例。