WearableListenerService - 智能手机无法找到磨损作为节点?

时间:2014-09-09 10:08:33

标签: java android smartphone wear-os

我将智能手机连接到磨损模拟器,我使用Android Studio。

模拟器和我的手机应用都使用WearableListenerService并重写了onMessageReceived(),而模拟器能够使用MessageApi成功向手机发送消息,但设备无法发送一回 - 尽管模拟器将我的手机视为一个节点,但仍然找不到任何节点。我最初使用MessageEvent.getSourceNodeId()方法和MessageEvent提供的onMessageReceived()参数,但这也不起作用。

我的计划是发送信息给我的手机并让手机在电话后立即传回信息。

为了给我的磨损设备发消息,我是否需要在手机上做些什么?我按照许多指南的说明进行操作:两个applicationId都是一样的,目录是一样的,清单很好,我用Generate Signed 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;
}
}

0 个答案:

没有答案