1

I am trying to use DataApi and DataApi.DataListener to send data from phone to watches, but onDataChanged is not called for all requests (only some are delivered) send from phone Wearable.DataApi.putDataItem(mGoogleApiClient, request) although ResultCallback<DataApi.DataItemResult>() result is successful for all requests

To be sure data are always different (onDataChanged() is called only when data has changed) I am adding time stamp to DataMap for each request like this: myRequestDataMap.putLong("timeStamp", System.currentTimeMillis()) and to be called immediately using setUrgent()

For debug purposes I also send message using MessageApi right after request and all messages are delivered.

Notes:

  • applicationId is same for both phone and watch apps
  • play-services-wearable version is also same and 9.8.0
  • com.google.android.wearable:wearable version is 2.0.3
  • tested with LG G Watch and emulator

EDIT: watch app manifest

<manifest
package="app.package"
xmlns:android="http://schemas.android.com/apk/res/android">

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault.Light">

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

    <meta-data
        android:name="com.google.android.wearable.standalone"
        android:value="false" />

    <activity
        android:name="app.package.Activity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

</application>

EDIT 2:

Phone app:

public class App extends Application implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

@Override
public void onCreate() {
    super.onCreate();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onMessageReceived(final MessageEvent messageEvent) {
    //as response to data request
    ...
    //load data from DB
    ...
    while (!cursor.isAfterLast()) {
        ...

        PutDataMapRequest dataMapRequest = myObject.generateDataMap();
        dataMapRequest.getDataMap().putLong("timeStamp", System.currentTimeMillis());
        PutDataRequest request = dataMapRequest.asPutDataRequest();
        PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
            .putDataItem(mGoogleApiClient, request);

        pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
            @Override
            public void onResult(final DataApi.DataItemResult result) {
                if (result.getStatus().isSuccess()) {
                    Log.d(TAG, "Success");
                } else {
                    Log.d(TAG, "Failed");
                }
            }
        });

        cursor.moveToNext();
    }

}   

}

Wear app:

public class WearActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, DataApi.DataListener, NodeApi.NodeListener, 
    MessageApi.MessageListener {

@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
    Wearable.DataApi.addListener(mGoogleApiClient, this);
    Wearable.MessageApi.addListener(mGoogleApiClient, this);
    Wearable.NodeApi.addListener(mGoogleApiClient, this);
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
    for (DataEvent event : events) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
            ...
        }
    }
}

 @Override
public void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        Wearable.DataApi.removeListener(mGoogleApiClient, this);
        Wearable.MessageApi.removeListener(mGoogleApiClient, this);
        Wearable.NodeApi.removeListener(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }

    super.onStop();
}

}

Can anybody help?

majov
  • 545
  • 3
  • 16
  • You may want to check [SO post1](https://stackoverflow.com/questions/37268174/ondatachanged-is-never-called) and [SO post2](https://stackoverflow.com/questions/32420606/ondatachanged-not-being-called-on-android-wear). See if it will help you. Otherwise, I suggest that you also share what you've already done such as codes in main activity for both phone and wearable. – Teyam Jul 14 '17 at 11:42
  • I've updated my question – majov Jul 19 '17 at 11:56

0 Answers0