1

I'm attempting to add some functionality to our current app's offering by implementing a custom app on Android Wear.

In an ideal use case I would like to have the user open the wear app and for it to ask the handheld for the current data that it has stored. My first thought was to use an IntentService, but it does not appear that I'm able to get this to work with an implicit Intent. Also, after reading the data syncing section it appears that this is not the preferred manner of getting data for a wearable.

I've read over the Data Layer and Syncing section of the Wear docs. It seems to me that in order to get data syncing to occur between the handheld and wearable that the companion app on must be opened first on the handheld, data sync occurs between the GoogleApiClient, the wear app is opened, the wear app receives the data from the GoogleApiClient.

Does the above flow work? From my reading it seem like this happens in semi real time where the companion app and wear app were open at the same time. If the user closes the companion app on the handheld and then later opens the wear app will the data be there or will the push only occur if both activities are active?

So, is this a design constraint for the wearable model by Google, such that the companion app must have pushed the data before the wearable can request it? Also, repeating my second question, can the companion app close after it has pushed the data and the wearable retrieve the data at another time even if the companion app is not running?

Mike G
  • 1,956
  • 1
  • 30
  • 62

1 Answers1

6

The companion app doesn't have to be opened at all.

I have a wearable app where the companion app simply is a service that extends WearableListenerService and performs all the heavy-duty network functions, and passes everything back to the wearable app via the data layer.

It has no UI and is not launchable on the handheld device. It is solely a proxy for the wearable app. It doesn't have a single Activity.

Synching occurs under the hood and I do not have to worry about it. I believe that is Google's intention.

In an ideal use case I would like to have the user open the wear app and for it to ask the handheld for the current data that it has stored.

Implement a WearableListenerService in your existing app, it will listen for requests from the wear app. In your wear app onCreate send a message via the data layer. Continue the messaging conversation in the service on your existing app. You will need a listener in the wear app too of course.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • I'm assuming that the WearableListenerService is exactly like a regular Service (android.app.Service; it inherits from it) in that it needs to be declared in the same way in the manifest. What handles starting the service? Do I need to fire an intent to start it or just use the data layer stuff and let Google handle the magic? – Mike G Oct 15 '14 at 15:13
  • Check this out: http://android-wear-docs.readthedocs.org/en/latest/sync.html This helped me a lot. AFAIK you don't need to worry about starting the service, seems to "just work" for me :) – Ken Wolf Oct 15 '14 at 15:17
  • 2
    The WearableListenerService is started automatically when needed by Google Play Services. You just declare it and you are good to go. Check out some of the samples included in the SDK, such as DataLayer or FindMyPhone. The FindMyPhone one has no UI, and detects disconnects between the phone and wearable. – Wayne Piekarski Oct 17 '14 at 04:20
  • I know this is old(ish), but could you detail what is contained in the companion app? What listeners, onreceived events etc and how the two modules "talk" to each other? Your answer is helpful but still confusing to me. – Adam Short Jul 15 '16 at 21:45
  • @AdamShort implement a WearableListenerService in both the companion and wear app following the guidelines here: https://developer.android.com/training/wearables/data-layer/events.html. The service is responsible for listening to data events. Send a message following the guidelines here: https://developer.android.com/training/wearables/data-layer/messages.html#SendMessage. At first just send a hello message or something simple. You can send a message from the wear app and the listener in the companion can receive it, and vice versa – Ken Wolf Jul 16 '16 at 06:46
  • @KenWolf thanks! Do I need to initialise the WearableListenerService anywhere in the companion app? Or will the wear app kick this off? – Adam Short Jul 16 '16 at 14:44
  • 1
    @AdamShort Android Wear will know when to kick it off, it's responsible for running it, so you don't need to worry about it – Ken Wolf Jul 16 '16 at 14:57
  • @KenWolf One last thing, I have managed to get this to work how I want with BIND_LISTENER, however this is now deprecated and AS won't let me build without changing it (and I want to anyway). Do you know what actions I need to subscribe to to send AND receive data in the service? I tried MESSAGE_RECEIVED but it never get's hit. I also have multiple paths so do I need to specify all of them? – Adam Short Jul 21 '16 at 12:30
  • @AdamShort make sure you've added the scheme part from here: http://android-developers.blogspot.co.uk/2016/04/deprecation-of-bindlistener.html you don't need a prefix. – Ken Wolf Jul 21 '16 at 12:45
  • @KenWolf what is the 'scheme'? Module name? Or does it just need to be Wear in both manifests? – Adam Short Jul 21 '16 at 12:57
  • @AdamShort wear in both modules – Ken Wolf Jul 21 '16 at 12:58
  • @KenWolf thanks. Unfortunately it's not working now. I have it declared like this: – Adam Short Jul 21 '16 at 13:02
  • 1
    I managed to get this working by adding all of the available ones [here](https://developers.google.com/android/reference/com/google/android/gms/wearable/WearableListenerService) and then removing one at a time until I was left with the minimum – Adam Short Jul 21 '16 at 17:12