Wearable Data Layer API 是 Google Play 服務的一部分,可在穿戴式裝置 (例如智慧手錶) 和連線手持裝置 (通常是智慧型手機) 之間提供通訊管道。這是在裝置之間同步處理及傳輸資料的方式。
注意:這個 API 僅適用於 Wear OS 手錶和配對的 Android 裝置。Wear OS 手錶與 iOS 手機配對時,在有網際網路連線的情況下,應用程式可以查詢其他雲端式 API。如要進一步瞭解其他 API,請參閱「在 Wear OS 上存取網路和進行同步處理」。
注意:由於資料層 API 是專為手持裝置與穿戴式裝置之間的通訊而設計,因此只有這些 API 可用於設定這類裝置間的通訊。舉例來說,請勿嘗試開啟低階通訊端來建立通訊管道。
常見用途
Data Layer API 特別適合健身和媒體用途。
健身應用程式
將運動資料從 Wear OS 應用程式傳送至行動應用程式 健身應用程式通常需要將手錶擷取的運動資料寫入行動應用程式或 Health Connect。如果使用 Data Layer API 傳輸資料,請使用訊息用戶端,將運動資料從 Wear OS 應用程式傳送至行動應用程式,以便寫入 Health Connect。
在家運動時,將即時資料串流至行動裝置
常見的居家健身情境是將心率資料從 Wear OS 裝置串流至行動裝置,並在使用者的行動裝置螢幕上顯示最新的心率資訊。如要串流這類資料,請使用管道用戶端。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-17 (世界標準時間)。"],[],[],null,["# Overview of Data Layer API\n\n**Figure 1.** A sample of network of nodes with handheld and Wear OS devices.\n\nThe Wearable Data Layer API, which is part of Google Play services, provides a\ncommunication channel between wearable devices (like smart watches) and\nconnected handheld devices (usually smartphones). It is a way to synchronize and\ntransfer data between the devices. \n**Note:** This API is only available on Wear OS watches and\npaired Android devices. For Wear OS watches paired with iOS phones, apps can\nquery other cloud-based APIs if internet connectivity is available. For more\ninformation on these other APIs, visit\n[Network access and sync on\nWear OS](/training/wearables/data/network-access). \n**Caution:** Because the data layer APIs are designed for\ncommunication between handhelds and wearables, these are the only APIs you can\nuse to set up communication between these devices. For example, don't try to\nopen low-level sockets to create a communication channel.\n\nCommon use cases\n----------------\n\nThe Data Layer API is particularly useful for fitness and media use cases.\n\n### Fitness apps\n\nSending exercise data from Wear OS app to mobile app Fitness apps often need to\nwrite the exercise data captured by a watch to a mobile app or to [Health\nConnect](/health-and-fitness/guides/health-connect). If using the Data Layer API to transfer data, use a\n[message client](/training/wearables/data/client-types#message-client) to send exercise data from the Wear OS app to the mobile app\nin order to write to Health Connect.\n\n#### Stream live data to the mobile device during a home workout\n\nA [common home workout scenario](https://medium.com/androiddevelopers/wear-os-home-workouts-with-health-services-b9951fa9e0dc) is streaming heart rate data from a Wear OS\ndevice to a mobile device and showing the user up-to-date heart rate information\non their mobile device's screen. To stream this data, use a [channel client](/training/wearables/data/client-types#channel-client).\n\n### Media apps\n\nTo control a media player through the action of pause/resume/start/end from the\nwatch to the phone, use a [message client](/training/wearables/data/client-types#message-client).\n\nOptions for communication\n-------------------------\n\nData is transferred in one of the following ways:\n\n1. **Directly**, when there is an established Bluetooth connection between the Wear OS device and another device.\n2. **Over an available network**, such as LTE or Wi-Fi, using a network node on Google's servers as an intermediary.\n\nAll Data Layer clients may exchange data either using Bluetooth or using the\ncloud, depending on connections available to the devices. Assume that data\ntransmitted using Data Layer may at some point use Google-owned servers.\n\n### Bluetooth\n\nWhen devices are connected using Bluetooth, Data Layer uses this connection.\nThere is a single encrypted channel between the devices, using standard\nBluetooth encryption, managed by Google Play services.\n\n### Cloud\n\nData is automatically routed through Google Cloud when Bluetooth is unavailable.\nAll data transferred through Google Cloud is end-to-end encrypted.\n\nSecurity of communications\n--------------------------\n\nGoogle Play services enforces the following restrictions to provide more secure\ncommunication between the app installed on a Wear OS device and the same app\ninstalled on a nearby handheld device:\n\n- The package name must match across devices.\n- The signature of the package must match across devices.\n\nNo other apps have access to the data regardless of connection type.\n\nSetup\n-----\n\nThe Wearable Data Layer API has the following dependencies:\n\n- The latest version of [Google Play services](https://developers.google.com/android).\n- A Wear OS device or Wear OS emulator.\n\nInclude the following dependency in the build.gradle file of your Wear module: \n\n dependencies {\n ...\n implementation(\"com.google.android.gms:play-services-wearable:19.0.0\")\n }\n\n### Facilitate the initial pairing process\n\n[Horologist](https://github.com/google/horologist) provides several helper libraries on top of platform APIs.\nIt includes a [data layer library](https://google.github.io/horologist/datalayer-helpers-guide/) that helps establish a connection between\na mobile device and a Wear OS device. Additionally, it provides convenient APIs\nto do the following:\n\n- Install the app on the other device.\n- Launch the app on the other device.\n- Launch a specific activity on the other device.\n- Launch the companion app.\n\nAccess the data layer\n---------------------\n\nTo call the Data Layer API, use the [`Wearable`](https://developers.google.com/android/reference/com/google/android/gms/wearable/Wearable) class to get instances of\nthe various client classes, such as [`DataClient`](https://developers.google.com/android/reference/com/google/android/gms/wearable/DataClient) and [`MessageClient`](https://developers.google.com/android/reference/com/google/android/gms/wearable/MessageClient).\n\nFor more information, refer to the [DataLayer sample](https://github.com/android/wear-os-samples/tree/main/DataLayer).\n\nUse a minimal client\n--------------------\n\nTo create a client, see the following example code: \n\n### Kotlin\n\n```kotlin\nval dataClient: DataClient = Wearable.getDataClient(context)\n```\n\n### Java\n\n```java\nDataClient dataClient = Wearable.getDataClient(context);\n```\n| **Caution:** Before using the Wearable Data Layer API, check that it's available on a device; otherwise, an exception occurs. Use the [`GoogleApiAvailability`](https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability) class, as implemented in [Horologist](https://github.com/google/horologist/blob/release-0.5.x/datalayer/core/src/main/java/com/google/android/horologist/data/WearableApiAvailability.kt#L29).\n\nThe context can be any valid Android context. If you are using the API within\nthe scope of an `Activity`, use the `getDataClient()` method of the `Wearable`\nclass. This lets certain interactions appear as dialogs rather than as\nnotifications, such as when the user is asked to update their version of Google\nPlay services.\n\nBy default, callbacks to listeners are made on the app's main UI thread. To have\ncallbacks made on a different thread, use a [`WearableOptions`](https://developers.google.com/android/reference/com/google/android/gms/wearable/Wearable.WearableOptions) object to\nspecify a custom `Looper`: \n\n### Kotlin\n\n```kotlin\nrunBlocking {\n Wearable.getDataClient(context, options)\n}\n```\n\n### Java\n\n```java\nWearableOptions options = new WearableOptions.Builder().setLooper(myLooper).build();\nDataClient dataClient = Wearable.getDataClient(context, options);\n```\n\nFor more information, see the [`WearableOptions.Builder`](https://developers.google.com/android/reference/com/google/android/gms/wearable/Wearable.WearableOptions.Builder) reference.\n\nRecreate client instances as necessary\n--------------------------------------\n\nWearable API clients, such as [`DataClient`](https://developers.google.com/android/reference/com/google/android/gms/wearable/DataClient) and [`MessageClient`](https://developers.google.com/android/reference/com/google/android/gms/wearable/MessageClient), are\ninexpensive to create. So instead of holding onto the clients, recreate them as\nyou need them, using the style that suits your app.\n\nThe client state, such as the set of registered listeners, is shared across all\nclients and is preserved if Google Play services is updated while an app is\nrunning."]]