【问题标题】:Handheld device cannot connect to GoogleApiClient手持设备无法连接 GoogleApiClient
【发布时间】:2017-04-15 05:46:28
【问题描述】:

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

这个类与我在一个名为SendToPhone 的 Android Wear 设备中使用的另一个类相同,它可以成功运行,并且会向我连接的手机发送一条即发即弃的消息,但 SendToWear(一个带有 相同代码,如上所述)不会。

问题出在这部分:

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

【问题讨论】:

    标签: java android wear-os


    【解决方案1】:

    设法解决了问题。我的手持设备的 build.gradle 文件中有以下内容:

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

    我将这一行替换为

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

    然后它起作用了。

    【讨论】:

      【解决方案2】:

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

          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 服务应用的更新版本。

      删除您之前可能拥有的任何构建也很好。

      就我而言,我必须从 MobileWear 项目中删除 /Build 文件夹并重新-编译它们....

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

      希望对你有帮助

      【讨论】:

      • 您不需要事先连接即可进行同步连接(就像在方法 doSomething() 中所做的那样)。不过,您的代码仍然可以工作。
      【解决方案3】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2019-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-17
        • 1970-01-01
        • 1970-01-01
        • 2019-02-12
        • 1970-01-01
        相关资源
        最近更新 更多