-1

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! :)

Dambakk
  • 595
  • 4
  • 20

1 Answers1

0

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

Sterling
  • 6,365
  • 2
  • 32
  • 40
  • Thank you for answering. I have read this article before, but I can try to follow it again. I might come back to you and ask for more help if I get into more trouble. Thanks again @String! :) – Dambakk Apr 03 '16 at 11:37
  • From the example in the article you linked to, could you explain what this line do? ConnectionResult connectionResult = client.blockingConnect(30, TimeUnit.SECONDS); – Dambakk Apr 03 '16 at 11:54
  • That waits 30 seconds for the connection to the Google Play Services client to complete. You need that connection to use the Data API. If this code wasn't in a service, you'd use a (non-blocking) `connect` call instead, but here it's OK for your code to simply wait for the connection. – Sterling Apr 03 '16 at 15:30