[go: up one dir, main page]

Skip to content

Latest commit

 

History

History

DataLayer

Android DataLayer Sample

This sample demonstrates how to work with a WearableListenerService using:

Introduction

This sample showcases how a phone and a Wear OS app can exchange data. It implements 3 use cases:

  1. 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.

  1. 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()
  1. 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:

Pre-requisites

  • Android SDK 32

Screenshots

Screenshot Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

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.