Search code examples
androidwear-osdataitem

OnDataChanged is never called


I am trying the dataitem APi and here is my code

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d("Inside", "onConnected: " + connectionHint);
                    Toast.makeText(getApplicationContext(),"Inside On connected",Toast.LENGTH_SHORT).show();
                    // Now you can use the Data Layer API
                    //Creating Dataitem
                    PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/count");
                    dataMapRequest.setUrgent();
                    DataMap datamap=dataMapRequest.getDataMap();
                    PutDataRequest putDataRequest=dataMapRequest.asPutDataRequest();
                    datamap.putString("key", "Value");
                    PendingResult<DataApi.DataItemResult> pendingResult =
                            Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d("Inside", "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d("Inside", "onConnectionFailed: " + result);
                }
            })
            // Request access only to the Wearable API
            .addApiIfAvailable(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}

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

What am I missing out to trigger on data changed?. I read this and this but I am still unclear what am I supposed to change to enter on data changed


Solution

  • If you are using DataApi.DataListener, then you have to addListener after GoogleApiClient connected. Something like: Wearable.DataApi.addListener(mGoogleApiClient, this) inside your GoogleApiClient.ConnectionCallbacks.

    If you are using WearableListenerService make sure you have similar code in your AndroidManifest.xml on the receiving/listening side as shown below:

    <service android:name=".YourCustomWearableListenerService">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED"/>
                <data android:scheme="wear" 
                      android:host="*" 
                      <!-- I believe pathPrefix is optional, removing it will make the service listener to all data change events -->
                      android:pathPrefix="/YourDataMapPathPrefix"/>
            </intent-filter>
    </service>
    

    Also keep in mind that onDataChanged will get call only if the data is ACTUALLY changed. Let's said that the old data is ("Person", "Bob") and you replace it with same data ("Person", "Bob"), then onDataChanged will not get triggered since the data does not change in fact. But onDataChanged will get triggered if you put ("Person", "Bob2") instead.