This sample demonstrates how to work with a WearableListenerService using:
- DataClient to exchange data events
- MessageClient to send messages
- CapabilityClient to find nodes with specific capabilities
This sample showcases how a phone and a Wear OS app can exchange data. It implements 3 use cases:
- Send a data asset from the phone to the watch In the sample you can take a photo on the phone and send it to the paired watch. The photo is sent as a DataAsset by using DataClient.
val request = PutDataMapRequest.create(IMAGE_PATH).apply {
dataMap.putAsset(IMAGE_KEY, imageAsset)
dataMap.putLong(TIME_KEY, Instant.now().epochSecond)
}
.asPutDataRequest()
.setUrgent()
val result = dataClient.putDataItem(request).await()
This use case is successful if the watch is connected and has the Wear app installed which is implemented by using CapabilityClient.
- Send data from the phone to the watch and ackownledge via a message The phone app increments a counter and send it over a period of 5 seconds as a DataItem by using DataClient. The Wear app receives the DataItem by implementing a WearableListenerService and acknowledge by sending a message via MessageClient
messageClient.sendMessage(
nodeId,
DATA_ITEM_RECEIVED_PATH,
payload
)
.await()
- Launch the Wear app from the phone app The phone app checks if there is a connected node with a specific capability that identifies the correspondent Wear app. The capability is declared in the wear.xml file:
<string-array name="android_wear_capabilities" tools:ignore="UnusedResources" translatable="false">
<!-- declaring the provided capabilities -->
<item>wear</item>
Then the phone app sends a message to the Wear app by specifying the node id of the device and the path of the activity.
nodes.map { node ->
async {
messageClient.sendMessage(node.id, START_ACTIVITY_PATH, byteArrayOf())
.await()
}
}.awaitAll()
The Wearable app is listening to events by implementing a WearableListenerService an upon receiving the message starts the Activity.
This samples is useful to learn about how to use the Wearable API clients and WearableListenerService. Alternatively Horologist provides some API which facilitates some use cases such as like:
- Installing the Wear app on another connected device by opening the Playstore app on the phone
- Starting the Wear app on another connected device
- Finishing sign-in the Wear app on another connected device
- Installing a Wear Tile from the phone by redirecting to the Tile settings editor screen (supported only on some devices)
- Persisting data on the DataStore and model as a proto
- Check if the Wearable API is supported on mobile
- Find connected nodes and understand if the app is already installed
- Android SDK 32
This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.
- Stack Overflow: https://stackoverflow.com/questions/tagged/wear-os
If you've found an error in this sample, please file an issue in the issue tracker.
Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.