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

Communication from wearable service to handheld service [Android]


I'm creating a sensor logger-app for an Android Wearable. Logging is happening in a service, sending data from the wearable is done by a service, but after reading the Android Developer Guide and done some testing it seems like the preferred way to receive a DataMap is to use an Activity. I read about the WearableListenerService and I used it between phone and wearable, but then I sent a message and now I want to send a DataMap, which is, as far as I can see, not easy to implement using the WearableListenerService.

The illustration (thank you for approving my paint-skills) shows what kind of setup I want, using services. I want to send a DataMap on the arrow at the bottom, and make a service to receive the DataMap and send the data to another service to be written to file.

This is the service sending the DataMap from the wearable:

public class SendDataMapToHandheldDataLayer_Thread extends Thread {

private String path;
private DataMap dataMap;
private GoogleApiClient googleClient;

public SendDataMapToHandheldDataLayer_Thread(String cPath, DataMap cDataMap, GoogleApiClient cGoogleClient){
    path = cPath;
    dataMap = cDataMap;
    googleClient = cGoogleClient;
}

public void run(){
    PutDataMapRequest putDMR = PutDataMapRequest.create(path);
    putDMR.getDataMap().putAll(dataMap);
    PutDataRequest request = putDMR.asPutDataRequest();
    DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient, request).await();
    if(result.getStatus().isSuccess()){
        Log.v("dataMapSender_Wear", "DataMap successfully sent!");
    }else{
        Log.v("dataMapSender_Wear", "ERROR: Failed to send DataMap to data layer");
    }
}

}

What will the receiver look like?

Thanks to all wonderful people answering! :)


Solution

  • With a service-based architecture like you've outlined, your receiver will extend WearableListenerService, as shown here: http://developer.android.com/training/wearables/data-layer/events.html#Listen