Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
API lớp dữ liệu Wear OS bao gồm nhiều loại ứng dụng, hữu ích cho nhiều loại dữ liệu và trong các điều kiện kết nối khác nhau.
Trang này giới thiệu từng loại ứng dụng và có một bảng so sánh các chức năng của các ứng dụng. Dựa vào thông tin này, bạn có thể chọn tập hợp các loại ứng dụng phù hợp nhất với ứng dụng của mình.
Mỗi DataItem là một đơn vị thông tin được truyền và đồng bộ hoá trên tất cả các thiết bị lân cận mà người dùng sở hữu. DataItem được lưu trữ liên tục và thiết bị của bạn có thể đọc nội dung của DataItem cho đến khi mục dữ liệu bị xoá.
Asset dành cho các tải trọng dữ liệu lớn hơn, chẳng hạn như hình ảnh hoặc tệp nội dung nghe nhìn.
Ứng dụng nhắn tin
Đối tượng MessageClient có thể gửi thông báo và phù hợp với các lệnh gọi quy trình từ xa (RPC), chẳng hạn như dùng thiết bị Wear OS để kiểm soát phiên bản ứng dụng được cài đặt trên thiết bị cầm tay.
Tin nhắn phù hợp với các yêu cầu một chiều bằng cách sử dụng sendMessage() hoặc mô hình giao tiếp yêu cầu và phản hồi bằng cách sử dụng sendRequest(). Không giống như máy khách dữ liệu, máy khách tin nhắn cần các nút được kết nối với mạng để gửi tin nhắn.
Phương thức sendMessage() là phương thức tốt nhất để gửi đến nút từ xa và không chứa cơ chế thử lại tích hợp nào. Nếu thiết bị đích ngắt kết nối trước khi quá trình truyền dữ liệu qua mạng bắt đầu, phương thức này sẽ trả về TARGET_NODE_NOT_CONNECTED.
Ứng dụng kênh
Đối tượng ChannelClient cung cấp hoạt động giao tiếp theo luồng giữa các thiết bị. Kênh là một đường ống giao tiếp hai chiều giữa hai nút, hữu ích cho các trường hợp sử dụng như sau:
Chuyển các tệp dữ liệu giữa hai hoặc nhiều thiết bị được kết nối khi không có Internet. ChannelClient lưu dung lượng ổ đĩa qua DataClient, việc này sẽ tạo bản sao của các thành phần trên thiết bị cục bộ trước khi đồng bộ hoá với các thiết bị đã kết nối.
Gửi một tệp có kích thước quá lớn nên không thể gửi bằng MessageClient.
Chuyển dữ liệu phát trực tuyến, chẳng hạn như dữ liệu thoại từ micrô.
Sau khi mở một kênh, bạn có thể gửi và nhận dữ liệu trong một luồng byte liên tục, thay vì các đơn vị DataItem riêng biệt mà các ứng dụng dữ liệu yêu cầu.
Bạn chịu trách nhiệm quản lý luồng dữ liệu và đảm bảo dữ liệu nhất quán.
Máy khách kênh không cung cấp mức độ đồng bộ hoá dữ liệu tự động như máy khách dữ liệu.
So sánh ứng dụng
Bảng sau đây so sánh các chức năng của các ứng dụng khách:
Ưu tiên Bluetooth. Dữ liệu được sao lưu lên đám mây; nếu có Bluetooth, quá trình sao lưu này sẽ diễn ra không đồng bộ
Có, cho cả hoạt động đọc và ghi
Ứng dụng nhắn tin
Không duy trì và không thử lại
Không
Ưu tiên Bluetooth, nhưng có thể sử dụng Wi-Fi nếu đó là loại kết nối duy nhất hiện có
Không
Ứng dụng kênh
Không có tính liên tục (hướng kết nối)
Có
Ưu tiên Bluetooth, nhưng có thể sử dụng Wi-Fi nếu đó là loại kết nối duy nhất hiện có
Không
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-09-05 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-05 UTC."],[],[],null,["The Wear OS data layer APIs consist of several different types of clients, which\nare useful for different types of data and during different connectivity\nconditions.\n\nThis page introduces each client type, and it includes a table that compares the\ncapabilities of the different clients. Using this information, you can select\nthe set of client types that works best for your app.\n\nData client\n\nA [`DataClient`](https://developers.google.com/android/reference/com/google/android/gms/wearable/DataClient) object lets you read or write to a [`DataItem`](https://developers.google.com/android/reference/com/google/android/gms/wearable/DataItem) or\n[`Asset`](https://developers.google.com/android/reference/com/google/android/gms/wearable/Asset):\n\n- Each `DataItem` is a unit of information that's broadcast and synchronized\n across all nearby devices that a user owns. A `DataItem` is stored persistently,\n and your device can read its contents until the data item is deleted.\n\n- An `Asset` is meant for larger data payloads, such as images or media files.\n\nMessage client\n\nA [`MessageClient`](https://developers.google.com/android/reference/com/google/android/gms/wearable/MessageClient) object can send messages and is good for remote procedure\ncalls (RPC), such as using a Wear OS device to control the version of your app\nthat's installed on a handheld device.\n\nMessages are great for one-way requests using `sendMessage()`, or for a\nrequest-and-response communication model using `sendRequest()`. Unlike with data\nclients, message clients need the nodes to be connected to the network in order\nto send messages.\n\nThe `sendMessage()` method is a best effort to deliver to the remote node, and\nit doesn't contain any built-in retry mechanism. If the target device\ndisconnects before the network transfer starts, the method returns\n`TARGET_NODE_NOT_CONNECTED`.\n| **Note:** To help preserve power, consider sending messages only to nearby devices.\n\nChannel client\n\nA [`ChannelClient`](https://developers.google.com/android/reference/com/google/android/gms/wearable/ChannelClient) object provides stream-oriented communication between\ndevices. A *channel* is a bidirectional communication pipe between two nodes,\nwhich is useful for use cases such as the following:\n\n- Transfer data files between two or more connected devices when the internet isn't available. `ChannelClient` saves disk space over `DataClient`, which creates a copy of the assets on the local device before synchronizing with connected devices.\n- Reliably send a file that's too large to send using a `MessageClient`.\n- Transfer streamed data, such as voice data from the microphone.\n\nAfter you open a channel, you can send and receive data in a continuous byte\nstream, rather than the discrete `DataItem` units that data clients require.\n\nYou're responsible for managing the data flow and keeping data consistent.\nChannel clients don't offer the same level of automatic data synchronization\nthat data clients do.\n\nClient comparison\n\nThe following table compares the capabilities of the different clients:\n\n| Client type | Data persistence | Supports data larger than 100 KB? | Network to use | Works offline? |\n|--------------------|--------------------------------------|--------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------|------------------------------|\n| **Data client** | Data is persisted indefinitely | Yes (use [`Asset`](https://developers.google.com/android/reference/com/google/android/gms/wearable/Asset) objects) | Bluetooth preferred. Data is backed up to the cloud; if Bluetooth is available, this backup is done asynchronously | Yes, for both read and write |\n| **Message client** | No persistence and no retry | No | Bluetooth preferred, but can use Wi-Fi if it's the only type of connection available | No |\n| **Channel client** | No persistence (connection-oriented) | Yes | Bluetooth preferred, but can use Wi-Fi if it's the only type of connection available | No |"]]