bisonfute
bisonfute

Reputation: 101

android wear : how to store data in WearableListenerService and access them?

I am a java/android beginner, trying to develop a simple wear app.

I am using a WearableListenerService to listen for data change from the handset and display these data on the wearable. These data represent the user context, let's say the number of beers he has drunk in the past 5 minutes. So, I have a int beerContext on the Handset and on the Watch that I'm trying to synchronize so that display on the watch and on the handset is the same.

As long as the number of beers is set from the handset only, everything is fine. Thanks to the data events gathered by the WearableListenerService.

BUT : I would like to make a little UI on the watch so that the user can update its number of beers drank.

My question is : the numberOfBeers is a private variable of the WearableListenerService. How can a watch activity update this variable as I didn't find any way to get a reference to the WearableListenerService? (I tried to override the onBind method, but it is final, so that's impossible.

I would very appreciate some precious advices as I might have done mistake in my app design.

A big thank!

Upvotes: 1

Views: 1409

Answers (1)

athor
athor

Reputation: 6928

I think you are misunderstanding how the data communication between the wear/phone works.

Each app will need to implement its own WearableListenerService, and each app will need to send data to the other app through the data api. Your private variable has no effect - theres no way to access this from the other app.

Have a read through Syncing Data items.

When sending the beer count from either the phone app or the wear app, you will need to send it with the data api:

e.g.

PutDataMapRequest dataMap = PutDataMapRequest.create("/beerCount");
dataMap.getDataMap().putInt(BEER_COUNT_KEY, beerCount);
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
    .putDataItem(mGoogleApiClient, request);

You will then get notified in the WearableListenerService when that data is received.

Upvotes: 0

Related Questions