手持设备无法连接到GoogleApiClient

时间:2014-09-10 11:00:09

标签: java android wear-os

我使用名为SendToWear的静态方法创建了一个名为sendMessage(Context context, String path, @Nullable String data)的类,它找到所有连接的节点并将数据发送到任何找到的节点。

此类与我在名为SendToPhone的Android Wear设备中使用的另一个类完全相同,该设备可以正常运行并向我连接的手机发送“即发即弃”消息,但SendToWear(a具有相同代码的类,如上所述)不会。

问题在于此部分:

GoogleApiClient sClient = new GoogleApiClient.Builder(context)
    .addApi(Wearable.API)
    .build();

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!");

手持设备始终返回“无法连接到sClient”。有人可以解释一下吗?

感谢。

以下完整代码:

import android.content.Context;
import android.support.annotation.Nullable;
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 SendToPhone {

    public static int sendMessage(Context context, String path, @Nullable String data){
        Log.d(TAG, "Path: " + path + "\tData: " + data);
        ArrayList<String> nodes;
        int sent; // amount of nodes which received the message

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

        GoogleApiClient sClient = new GoogleApiClient.Builder(context)
                .addApi(Wearable.API)
                .build();

        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!");

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

            // 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) {

        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;
    }
}

3 个答案:

答案 0 :(得分:1)

管理以解决问题。我的手持设备在build.gradle文件中包含以下内容:

compile 'com.google.android.gms:play-services:5.+'

我用

替换了这一行

compile 'com.google.android.gms:play-services-wearable:+'

然后它奏效了。

答案 1 :(得分:0)

您需要先连接。试试这个:

    private GoogleApiClient mGoogleApiClient;

    @Override
    public void onCreate() {
        super.onCreate();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle connectionHint) {
                        Log.d(TAG, "onConnected: " + connectionHint);
                    }

                    @Override
                    public void onConnectionSuspended(int cause) {
                        Log.d(TAG, "onConnectionSuspended: " + cause);
                        //TODO:let the user knows there was a problem. Google Service may need update or network not available
                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult connectionResult) {
                        Log.d(TAG, "onConnectionFailed: " + connectionResult);
                        //TODO:let the user knows there was a problem. Google Service may need update or network not available
                    }
                })
                .build();
        mGoogleApiClient.connect();
    }

private void doSomething(){
        if (!mGoogleApiClient.isConnected()) {
            ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(TIMEOUT_MS, TimeUnit.MILLISECONDS);
            if (!connectionResult.isSuccess()) {
                Log.e(TAG, "DataLayerListenerService failed to connect to GoogleApiClient.");
                return null;
            }
        }

        //do something here
}

如果仍然无效,请确保您拥有Google Play服务应用的更新版本。

也可以删除以前的任何构建。

在我的情况下,我必须从移动磨损项目中删除 / Build 文件夹并重新启动 - 编译他们......

sudo rm -rf mobile/build
sudo rm -rf wear/build
./gradlew clean
./gradlew build

希望有所帮助

答案 2 :(得分:0)

[解决] 我花了一整天! 真的很奇怪,我不得不改变:

compile 'com.google.android.gms:play-services:10.0.1'

compile 'com.google.android.gms:play-services:+'

我不确定它是如何以及为什么解决了这个问题!