androidwear-osgoogle-api-clientdataitem

onDataChanged() not being called on android wear


I'm trying to use Data Items to send a few strings through to my wear, but my wear never seems to receive any signal, because onDataChanged() is never called. I even set a time stamp to ensure the data is always different whenever it is sent.

Is there a specific way I have to install the app onto both devices to get it to work? I'm just clicking run and selecting my phone, then switching modules and doing the same for my wear device.

Here is the code from my main activity on my phone:

public class HomeActivity extends Activity{

public static String TAG = "HomeActivity";
private GoogleApiClient mGoogleApiClient;


@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_view);
    Button mButton = (Button) findViewById(R.id.send_button);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendData();
        }
    });

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {

                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            }).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {

                }
            }).addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}

private void sendData(){
    if (mGoogleApiClient!=null){
        return;
    }

    final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE");
    final DataMap map = putRequest.getDataMap();
    map.putInt("color", Color.RED);
    map.putLong("date", new Date().getTime());
    Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}

}

And here is the code on my wearable:

public class LayoutFaceService extends CanvasWatchFaceService implements DataApi.DataListener{

@Override
public void onCreate(){
    super.onCreate();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.i(TAG, "Connected");
                    Wearable.DataApi.addListener(mGoogleApiClient, LayoutFaceService.this);
                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {

                }
            })
            .build();
    mGoogleApiClient.connect();
}


@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_DELETED) {
            Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri());
        }
    }

}

And my wear manifest :

<uses-feature android:name="android.hardware.type.watch" />

<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >
    <service
        android:name=".LayoutFaceService"
        android:label="@string/my_digital_name"
        android:permission="android.permission.BIND_WALLPAPER" >
        <meta-data
            android:name="android.service.wallpaper"
            android:resource="@xml/watch_face" />
        <meta-data
            android:name="com.google.android.wearable.watchface.preview"
            android:resource="@drawable/preview_digital" />
        <meta-data
            android:name="com.google.android.wearable.watchface.preview_circular"
            android:resource="@drawable/preview_digital_circular" />

        <intent-filter>
            <action android:name="android.service.wallpaper.WallpaperService" />
            <category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

Solution

  • In sendData(), your conditional seems to be incorrect; mGoogleApiClient != null is true so you exit right there. Address that and see whether you can get any further; there might be other issues but that is the first obvious one. If that didn't completely fix your issue, then make sure you also include the manifest on your phone in your post.