sendMessage(Context context, String path, @Nullable String data)
이라는 정적 메서드를 사용하여 SendToWear
이라는 클래스를 만들었습니다. 연결된 모든 노드를 찾고 데이터를 찾은 노드로 보냅니다.핸드 헬드 장치가 GoogleApiClient에 연결할 수 없습니다.
이 클래스는 내가 성공적으로 작동 SendToPhone
라는 Android Wear 장치에 사용하고 보내드립니다 다른 클래스와 동일한 수있는 파이어 앤 포겟 내 연결된 휴대 전화에 메시지 만 SendToWear
(동일한 코드와 클래스를, 명시된대로)하지 않습니다.
문제는이 섹션에있다 : 핸드 헬드 장치가 항상 반환 'sClient에 연결하지 못했습니다'
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!");
. 아무도 이것을 설명 할 수 있습니까?
감사합니다. 아래
전체 코드 : 문제를 해결하기 위해 관리
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;
}
}
('doSomething()'메소드에서와 같이) 이전에는 동기 연결을 위해 연결할 필요가 없습니다. 그래도 코드가 작동합니다. –