2014-09-10 2 views
1

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

답변

1

.

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

내가

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

와 함께이 라인을 교체 한 후 근무 : 내 핸드 헬드 장치는 그 build.gradle 파일에 다음했다.

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 서비스 앱의 업데이트 된 버전을 사용하고 있는지 확인하십시오.

이전 빌드를 삭제해도 좋습니다. 내 경우

나는

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

그것이

을 도움이되기를 바랍니다 .... /를 을 삭제 모바일착용에서 프로젝트를 폴더를 구축하고이를 다시 컴파일했다
+1

('doSomething()'메소드에서와 같이) 이전에는 동기 연결을 위해 연결할 필요가 없습니다. 그래도 코드가 작동합니다. –

0

[SOLVED] 하루 종일 걸렸습니다! 그것은 내가 변경했다, 정말 이상해 :

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

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

에 나는하지만, 문제를 해결하는 방법과 이유 확실하지 않다!

관련 문제