Among others, you will need to add the wearable module to your build.gradle file for both wearable app project and phone app project:
Then you should include your wearable app project into your phone app project:
Basic wearable app
Basically, you can run any Android application on your watch, though it has a small display and limited hardware support (e.g. some watch has no GPS support). You can also use the support library provided by Google for some common UI widgets:
This library also provides the handy way to load a different layout for square or round watches. In your main activity’s layout file:
The WatchViewStub view will load the corresponding layout based on the shape of the watch. Then in your activity:
Send and sync data
There are two ways to share data between your phone and watch:
Send a specific message to a certain node using the MessageApi.
Share data among all nodes using the DataApi. With this API, the data sent will be synchronized across all connected devices, which means the data will be pushed to a disconnected device if it gets connected later.
With both methods, the data are private to the application, so the develop doesn’t need to worry about the privacy nor security.
Send data with MessageApi
Once you have a connected GoogleApiClient, you can use the following code to send a message to a connected node:
Sync data with DataApi
Once you have a connected GoogleApiClient, you can use the following code to sync data across all connected nodes:
Receive data with WearableListenerService
To receive messages or data updates from other nodes, you must extend the WearableListenerService, whose life cycle is managed by the phone or the watch:
And register it in your AndroidManifest.xml:
Alternatively, for an activity or a fragment, you can use DataApi or MessageApi to register a temporary listener.
Yep, that’s it. Quite straight forward to get your app running on the watch, happy hacking!