To call the Data Layer API, use the
Wearable
class to get instances of the various client classes,
such as
DataClient
and
MessageClient
.
Refer to the following related resources:
Note: The Data Layer API can only send messages and synchronize
data with Android devices or Wear OS watches. That means if your Wear OS device is paired with an
iOS device, the Data Layer API won't work.
For this reason, don't
use the Data Layer API as the primary way to communicate with a network. Instead,
follow the same pattern as in mobile apps,
with some minor differences.
Use a minimal client
A minimal client, as shown in the following example, is enough to begin. See Access Google APIs for additional information.
Kotlin
val dataClient: DataClient = Wearable.getDataClient(context)
Java
DataClient dataClient = Wearable.getDataClient(context);
The context can be any valid Android context. If you are using the API
within the scope of an Activity
, use the
getDataClient(activity)
method of the
Wearable
class. This lets certain interactions appear
as dialogs rather than as notifications, such as when the user is asked to
update their version of Google Play services.
By default, callbacks to listeners are made on the app's main UI thread.
To have callbacks made on a different thread, use a
WearableOptions
object to specify a custom Looper
:
Kotlin
val dataClient: DataClient = Wearable.WearableOptions.Builder().setLooper(myLooper).build().let { options -> Wearable.getDataClient(context, options) }
Java
WearableOptions options = new WearableOptions.Builder().setLooper(myLooper).build(); DataClient dataClient = Wearable.getDataClient(context, options);
See the
WearableOptions.Builder
reference for more information.
Wearable API clients, such as
DataClient
and
MessageClient
, are inexpensive to create. The API clients don't need to be
held onto, so you can create them as many times as you need. Use the style that suits your app. The client
state, such as the set of registered listeners, is shared across all clients
and is preserved if Google Play services is updated while an app is
running.