gpt4 book ai didi

java - 手持设备无法连接到 GoogleApiClient

转载 作者:行者123 更新时间:2023-11-30 02:40:54 25 4
gpt4 key购买 nike

我创建了一个名为 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;
}
}

最佳答案

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

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

我将这一行替换为

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

然后就成功了。

关于java - 手持设备无法连接到 GoogleApiClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com