-1

我正在将数据从我的手持设备发送到模拟器磨损并遵循教程

数据层消息

代码没有抛出错误,但是当我调试时它停在这一行

 NodeApi.GetConnectedNodesResult nodes= Wearable.NodeApi.getConnectedNodes(googleApiClient).await();

我的移动 mainActivity 是:

public class MainActivity extends ActionBarActivity implements     DataApi.DataListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener{

    String TAG="HandHeld MainActivity";
Button btnShowNotification;
GoogleApiClient googleApiClient;
Context context;
boolean connected=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "in onCreate");
    btnShowNotification=(Button)findViewById(R.id.btnShowNotification);
    context=this;

   googleApiClient =new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

}

@Override
protected void onStart() {
    super.onStart();

    Log.d(TAG, "in onStart before googleApiClient.connect");
    googleApiClient.connect();
    if(googleApiClient.isConnecting()) {
        Log.d(TAG, "in onStart after googleApiClient.isConnecting");
    }
    if(googleApiClient.isConnected()){
        Log.d(TAG, "in onStart after googleApiClient.isConnected");
    }

}

@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "in onResume");

}

public void OnBtnShowNotificationClick(View view){
    //googleApiClient.connect();
    Log.d(TAG, "in onBtnShowNotificationClick");
}

public Collection<String> getNodes(){
    Log.d(TAG, "in getNodes");
    HashSet<String> results=new HashSet<String>();

    NodeApi.GetConnectedNodesResult nodes=Wearable.NodeApi.getConnectedNodes(googleApiClient).await();

    for(Node node:nodes.getNodes()){
        results.add(node.getId());
    }
    Log.i(TAG, "in  node Retrieved " + results.size() + " Nodes");
    Toast.makeText(this,"in node retrieved"+results.size(),Toast.LENGTH_LONG).show();
    return results;
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "in onConnected");
    connected=true;

    String WearableDataPath="/warable_data";
    DataMap dataMap=new DataMap();
    dataMap.putString("myString", "CallStart");
    Log.d(TAG, "in onConnected before calling class SendToDataLayerThread");
    new SendToDataLayerThread(WearableDataPath,dataMap).start();
    Log.d(TAG, "in onConnected after calling class SendToDataLayerThread");
   // Wearable.DataApi.addListener(googleApiClient, this);
}

@Override
protected void onStop() {

    if (null != googleApiClient && googleApiClient.isConnected()) {
        googleApiClient.disconnect();
    }
    super.onStop();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d(TAG, "in onDataChanged");

}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "in onConnectionSuspended");
}

class SendToDataLayerThread extends Thread {

    String path;
    DataMap dataMap;

    SendToDataLayerThread(String p,DataMap d){
        path = p;
        dataMap = d;
    }

    @Override
    public void run() {
        super.run();
        Log.d(TAG, "in SendToDataLayerThread run method");
        NodeApi.GetConnectedNodesResult nodes= Wearable.NodeApi.getConnectedNodes(googleApiClient).await();

        for (Node node:nodes.getNodes()){
            Log.v("SendToDataLayerThread", "in for nodes.getNodes()");
            PutDataMapRequest putDMR=PutDataMapRequest.create(path);
            putDMR.getDataMap().putAll(dataMap);
            Log.v("SendToDataLayerThread", "after putAll method");

            PutDataRequest request = putDMR.asPutDataRequest();
            DataApi.DataItemResult result=Wearable.DataApi.putDataItem(googleApiClient,request).await();
            if(result.getStatus().isSuccess()){
                Log.v("SendToDataLayerThread", "in isSuccess DataMap: " + dataMap + " sent to: " + node.getDisplayName());
            }
            else {
                Log.v("SendToDataLayerThread", "in isFailure DataMap: " + dataMap + " sent to: " + node.getDisplayName());
            }
        }
    }
}
}

提前感谢您的帮助!:)

编辑:

移动构建.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion '21.1.2'

defaultConfig {
    applicationId "com.anuva.myproject.mywearapplication"
    minSdkVersion 19
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:7.0.0'
}

这个文件里有什么?

4

1 回答 1

1

我只是编辑 compile 'com.google.android.gms:play-services:7.0.0' 行

编译'com.google.android.gms:play-services:7.5.0'并点击“Sync Project with Gradle files”和android studio本身下载并安装google play services add on

感谢@br00 的帮助

于 2015-08-04T13:27:07.783 回答