Something wrong with this line : Wearable.NodeApi.getConnectedNodes(googleApiClient).await()

676 Views Asked by At

I am sending data from my handheld device to emulator wear and following tutorial

Data Layer Messages

Code is not throwing an error, but when I debug and it stops at this line

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

My mobile mainActivity is :

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

Thanx in advance for help !! :)

Edit:

Mobile build.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'
}

what is worng in this file?

1

There are 1 best solutions below

0
On

I just edit compile 'com.google.android.gms:play-services:7.0.0' line

to compile 'com.google.android.gms:play-services:7.5.0' and click on "Sync Project with Gradle files" and android studio itself download and installed google play services add on

Thankx to @br00 for help