[英]Handheld device cannot connect to GoogleApiClient
我用一个名为sendMessage(Context context, String path, @Nullable String data)
的静态方法制作了一个名为SendToWear
的类,该方法查找所有连接的节点并将数据发送到任何找到的节点。
该类与我在Android Wear设备中使用的另一个类(称为SendToPhone
)相同,该类成功运行并会向我连接的手机发送即发即忘消息,但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文件中包含以下内容:
compile 'com.google.android.gms:play-services:5.+'
我用这行代替
compile 'com.google.android.gms:play-services-wearable:+'
然后它起作用了。
您需要先连接。 尝试这个:
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服务应用的更新版本。
删除以前可能拥有的任何版本也是不错的选择。
就我而言,我必须从Mobile and Wear项目中删除/ Build文件夹,然后重新编译它们。
sudo rm -rf mobile/build
sudo rm -rf wear/build
./gradlew clean
./gradlew build
希望能帮助到你
[解决]我花了一整天! 我不得不改变,这真的很奇怪:
compile 'com.google.android.gms:play-services:10.0.1'
至
compile 'com.google.android.gms:play-services:+'
不过,我不确定如何以及为什么能解决问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.