Android Wear / MobileCompanion / WatchFace

Something O’Clock – Part 3

On 1st April 2016 I published Something O’Clock, a watch face app for Android Wear, to Play Store. The app is lighthearted in nature (because of the date of publication), it allows the user to set the time to “beer o’clock”, or “sleep o’clock”, or even “burger o’clock”. Although the app itself is quite lighthearted the code behind it is worthy of study and, in this series we’ll take a look at various aspects of developing custom watch faces for Android Wear. In this article we’ll look creating a companion app to run on the phone connected to the Wear device.

The heart of the companion app is a pretty simple Activity which hosts a RecyclerView instance containing the list of possible words. In order to stay on topic I’m not going to give an explanation of the RecyclerView implementation, but the only aspect of it worthy of explanation is how we obtain the list of words to display. We want the same set of words to be available to both phone & wear apps, so I have created a common module upon which both the wear and mobile modes depend. This common module contains the list of words as a string array resource. This makes our code easier to maintain because we only have to change things in one place and the changes will get picked up buy both mobile and wear modules when we build.

There is something worth a slight digression: The string array itself contains references to strings for each value:

The strings themselves are defined separately:

The reason for this is to future-proof things a little. If we ever wanted to translate the app in to different languages, all that would be required would be to translate strings.xml and not the array itself. The benefit here can be seen from how I have implemented an alternate spelling for “whisky” if the locale us set to US:

I only need to override the one word to get the desired behaviour. Moreover for me, as a developer who only speaks English, the array itslef would be more difficult for me to understand of the translations were directly in there, but by using indirection in this way the arrays definition remains understandable to me.

So, back to the mobile companion app, the first thing worthy of mention is that the wear watchface app and its mobile companion app must share the same package name. Also, the mobile companion app needs declaring a little differently in the Manifest in order to associate it with the watchface within the Wear app on the mobile device:

Registering using this intent-filter adds the ‘cog’ to the watchface within the Wear mobile app which will launch our settings:

wear app

The next thing that we need to do is set up the connection between the wear and mobile apps. In this case we’ll use the Wear Data API which is part of the Play Services Wearable library. We need to include the relevant dependency in our build.gradle for the mobile app:

Nest we have DataClientLoader which is responsible for setting up and tearing down the Play Services connection:

This is the public API of this class. It will do everything asynchronously and there’s a Callback interface which the client must implement which we’ll call once the connection is complete.

There are some protections in place to prevent multiple simultaneous connections – if one is already in progress we simple add the new client to a list to get notified when the existing connection attempt completes. The inner workings look like this:

I won’t go in to huge detail here because it is the standard mechanism for connecting to Play Services as is already documented here. We are wrapping it so that our Activity is completely agnostic of the Play Services connection stuff meaning that we could re-use it if we had more than one Activity. When we build the GoogleApiClient using GoogleApiClient.Builder we request the Wearable API and calling enableAutoManage(true) should ensure that Play Services will manage things for us (prompting the user, as required) if the required components are not already installed. Calling googleApiClient.connect(); kicks of the async connection attempt and we’ll get callbacks on our ConnectionCallbacks or onConnectionFailed() once the attempt is complete.

In the connection is successful (and this is a local connection to Play Services on the mobile device, not the connection to the wear app itself) we construct a DataClient object and return it via callback.

The DataClient is responsible for sending data to the wear app:

This is actually pretty simple. Whenever the user taps on a word from the list setWord() will be called. This will asynchronously send the data and the caller will get a callback once the write is complete. Once again, this doesn’t actually require a connection to the wear device – Play Services will queue it and send once a connection is available if one isn’t immediately available. Also, Play Services can route the sed differently depending on the connectivity to the wear device. Im many cases the connection will be over Bluetooth, but if the mobile and wear devices are too far apart, some wear devices support wifi connection and the data can be sent via that mechanism instead. However this is all handled for us by Play Services so we can just fire and forget!

The sendData() method is where all of the work is done. We first create a PutDataMapRequest using a key defined in our common module – we’ll need this same value on the wear side hence putting it in common). Specifying setUrgent() means that Play Services will try and deliver it as soon as possible rather than waiting until the next scheduled batch. We need to do this otherwise the user will have to wait a while before the face updates after they select a new word.

We then build a DataMap (which is a simple key / value pair map) containing the data to be sent – once again using key names from common. We convert this in to a PutDataRequest which simply bundles our DataMap in to a single item. Finally we call putDataItem() which kicks off the async send.

The more observant will have noticed that we send the list of words as well as the selected word. This hints at a future enhancement where we may support customisation of the list of words…

That’s it on the mobile side. When the user taps on a word from the list it gets packed up and sent to the wear app:

However we only have half of the solution here as we now need to receive this data on the wear app and update the watch-face accordingly. In the next article we’ll take a look at that.

The source code for this article is available here.

Many thanks to Daniele Bonaldo, Sebastiano Poggi, Erik Hellman, Hasan Hosgel, Said Tahsin Dane & Murat Yener – my beta testers.

Get it on Google Play

© 2016, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Something O’Clock – Part 3 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

Leave a Reply

Your email address will not be published. Required fields are marked *