Search code examples
androidwear-osandroid-wear-data-api

Send and Receiving Data using DataMap - Android Wearable


I am working on an Android Wearable project and I am trying to send data from the mobile device to the Wearable emulator. I am trying log the received data in the wearable but it is not logging.

These are what I've done so far.

1) Open the Android Wear app to get the Emulator status to be "connected"

2) Connect my mobile device

3) run adb -d forward tcp:5601 tcp:5601 to connect mobile device with emulator

4) Implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener in the sending class in my mobile device and building client in onCreate:

    googleClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

5) In my mobile device, create a dataItem, using 'weatherDescription" that logs perfectly, where I can using the DataApi to send to the wearable using putDataItem:

This is the log for weatherDescription: I/weatherDescription: scattered clouds

            putDataMapReq = PutDataMapRequest.create("/data");
            Log.i("weatherDescription", weatherDescription);
            putDataMapReq.getDataMap().putString("weatherDescription", weatherDescription);

            PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
            Wearable.DataApi.putDataItem(googleClient, putDataReq); 

5)Implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener in the receiving class (My custom watchface class) in my Wearable device and building client in onCreate:

    googleClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

6) Set up the listener in onConnect in the Wearable:

@Override
public void onConnected(Bundle bundle) {

    Wearable.DataApi.addListener(mGoogleApiClient, this).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(Status status) {
            Log.i("myTag", String.valueOf(status));
        }
    });
}

It looks like it's connecting properly on the receiving/wearable end:

I/myTag: Status{statusCode=SUCCESS, resolution=null}

7) In onDataChanged, log the received information:

    public void onDataChanged(DataEventBuffer dataEventBuffer) {
        Log.i("myTag", "in on Data Changed");
        for (DataEvent event : dataEventBuffer){
            if(event.getType() == DataEvent.TYPE_CHANGED){
                DataItem item = event.getDataItem();

                if(item.getUri().getPath().compareTo("/data") == 0 ){
                    DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();

                    dataMap.getString("weatherDescription");

                    Log.i("myTag", dataMap.getString("weatherDescription"));
                }
            }
        }
    }

Solution

  • I finally figured it out. In the watchface side, I declared the Google Api Client instance, mGoogleApiClient, in the WeatherWatchFaceService class as opposed to the Engine class. After I moved it from WeatherWatchFaceService to Engine, the watch side received what I was trying to send from the mobile.

    Cheers.