android-studiogoogle-data-apiandroid-wear-data-api

OnDataChanged is not called


I have a problem with comunication between my phone and wear device. I decided to add wear module to my app. Wear app has just one class (MainActivity)

package cz.johrusk.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;


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

GoogleApiClient mGoogleApiClient;
private TextView mTextView;
static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);
                    sendNumber(1);
                    Log.d(TAG,"BBBBBBBB");
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
    Log.d(TAG,"mGoogleApiClient connected;");
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG,"FAILE" + connectionResult);
}
public void sendNumber(int number) {
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/number");

    putDataMapRequest.getDataMap().putInt("number",number);
    putDataMapRequest.getDataMap().putLong("Time",System.currentTimeMillis());
    PutDataRequest putDataReq = putDataMapRequest.asPutDataRequest();
    putDataReq.setUrgent();
    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)
            .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                    if (!dataItemResult.getStatus().isSuccess()) {
                        Log.d(TAG,"Fail");
                    }
                    else{
                        Log.d(TAG,"Succes");
                    }
                }
            });
}
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d(TAG,"TEST");
}

}

I quess that "Wearable.DataApi.putDataItem" should call WearableListenerService in my phone app. Here is that service:

package cz.johrusk.showsmscode.service;


import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;


 public class WatchListener_service extends WearableListenerService {
@Override
public void onCreate() {
    super.onCreate();
   GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d("prijato","number is: ");

    for (DataEvent dataEvent : dataEventBuffer) {
    if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
        DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
        String path = dataEvent.getDataItem().getUri().getPath();
        if (path.equals("/number")){
            int number = dataMap.getInt("numa");
            long time = dataMap.getInt("timestamp");
            Log.d("received","number is: " + number);

        }
    }
}
}

}

However, onDataChanged method in WatchListener_service isn't called. onResult method inside ResultCallbacks print "Succes" so it seems that DataItem is send correctly.

I already find many similar problems on Stackoverlflow so I checked all these things:

  1. Both modules has same applicationId
  2. Both modules use 'com.google.android.gms:play-services-wearable:9.0.0'
  3. SetUrgent is used to putDataRequest so there shouldnt be any delay.
  4. WearableListenerService has declared correct intent filter in Manifest :

    action android:name="com.google.android.gms.wearable.DATA_CHANGED" data android:scheme="wear" android:host="*"

Both phone and Wear app run on physical device. My question is... What should I do to fix this issue? Thanks


Solution

  • Check if both apps have the same debug key. I had some problems with that and problem was different keys for both apps (wear and mobile).

    PS: The Android Wear API is ridiculous. I quit developing for Wear because of how terrible is that API that doest not work as they say this should.