Network access and sync on Wear OS

With Wear OS by Google, a watch can communicate with a network directly, without access to an Android or iOS phone. You shouldn't use the Data Layer API for connecting to a network.

Network access

Wear OS apps can make network requests. When a watch has a Bluetooth connection to a phone, the watch's network traffic generally is proxied through the phone.

When a phone is unavailable, Wi-Fi and cellular networks are used, depending on the watch hardware. The Wear OS platform handles transitions between networks.

You can use protocols such as HTTP, TCP, and UDP. However, the android.webkit APIs (including the CookieManager class) are not available. You can use cookies by reading and writing headers on requests and responses.

We also recommend using the WorkManager API for asynchronous requests, including polling at regular intervals.

If you need to connect to specific network types, see Read network state.

High-bandwidth network access

The Wear OS platform manages network connectivity with the goal of providing the best overall user experience. The platform chooses the default active network by balancing two factors:

  • The need for long battery life.
  • The need for network bandwidth.

When battery preservation is prioritized, the active network may have insufficient bandwidth to do network tasks that require high bandwidth, such as transporting large files or streaming media.

This section provides guidance on using the ConnectivityManager class to ensure that needed network bandwidth is available for your app. For general information about fine-grained control over network resources, see Manage network usage.

Requesting Wi-Fi connectivity

For use cases that require high-bandwidth network access, such as transporting large files or streaming media, request connectivity with a high-bandwidth transport, such as Wi-Fi.

Kotlin

val callback = object : ConnectivityManager.NetworkCallback() {
    override fun onAvailable(network: Network) {
        super.onAvailable(network)
        // The Wi-Fi network has been acquired, bind it to use this network by default
        connectivityManager.bindProcessToNetwork(network)
    }

    override fun onLost(network: Network) {
        super.onLost(network)
        // The Wi-Fi network has been disconnected
    }
}
connectivityManager.requestNetwork(
    NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build(),
    callback
)

Java

ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback() {
    public void onAvailable(Network network) {
        super.onAvailable(network);
        // The Wi-Fi network has been acquired, bind it to use this network by default
        connectivityManager.bindProcessToNetwork(network);
    }

    public void onLost(Network network) {
        super.onLost(network);
        // The Wi-Fi network has been disconnected
    }
};
connectivityManager.requestNetwork(
        new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build(),
        callback
);

Acquiring a network may not be instantaneous because a watch's Wi-Fi or cellular radio may be off to preserve battery. Additionally, if a watch cannot connect to a network, the onAvailable() method of your NetworkCallback instance is not called.

Once onAvailable() is called, the device will attempt to remain connected to the Wi-Fi network until the NetworkCallback is released. Release the callback when you no longer need a Wi-Fi network to preserve battery life.

Kotlin

connectivityManager.bindProcessToNetwork(null)
connectivityManager.unregisterNetworkCallback(callback)

Java

connectivityManager.bindProcessToNetwork(null);
connectivityManager.unregisterNetworkCallback(callback);

Launch the Wi-Fi settings activity

When requesting a Wi-Fi network, the system tries to connect to a saved network if a saved network is configured and in range. However, if no saved Wi-Fi network is available, the onAvailable() callback method of your NetworkCallback instance isn't called.

If you are using a Handler to time-out the network request, you can direct the user to add a Wi-Fi network when the timeout has occurred. Send the user directly to the activity for adding a Wi-Fi network using the following intent:

Kotlin

context.startActivity(Intent("com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS"))

Java

context.startActivity(new Intent("com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS"));

To launch the settings activity, your app must have the following permission: android.permission.CHANGE_WIFI_STATE

User interface considerations

If your app requires a connection to a new Wi-Fi network for a high-bandwidth operation, ensure that the reason for connecting is clear to the user before you launch the Wi-Fi settings. Only request that a user add a new Wi-Fi network when the high-bandwidth network is required. Do not block a user from accessing app features that do not require a high-bandwidth network.

Figure 1 shows a music app. The app should allow a user to browse music and should only require the user to add a new Wi-Fi network if they want to download or stream music.

Music downloading

Figure 1. A music app flow for downloading music.

Cloud messaging

For sending notifications, apps can directly use Firebase Cloud Messaging (FCM).

No APIs for network access or FCM are specific to Wear OS. Refer to the existing documentation about connecting to a network and cloud messaging.

FCM works well with Doze and is the recommended way to send notifications to a watch.

Provide for messages from FCM by collecting a registration token for a device when your Wear OS app runs. Then include the token as part of the destination when your server sends messages to the FCM REST endpoint. FCM sends messages to the device identified by the token.

An FCM message is in JSON format and can include one or both of the following payloads:

  • Notification payload. When a notification payload is received by a watch, the data is displayed to a user directly in the notification stream. When the user taps the notification, your app is launched.
  • Data payload. The payload has a set of custom key/value pairs. The payload and is delivered as data to your Wear OS app.

For more information and examples of payloads, see About FCM messages.

By default, notifications are bridged (shared) from a phone app to a watch. If you have a standalone Wear OS app and a corresponding phone app, duplicate notifications can occur. For example, the same notification from FCM, received by both a phone and a watch, could be displayed by both devices independently. You can prevent this by using bridging APIs.

Use background services

To ensure that background tasks are correctly executed, they must account for Doze and App Standby.

When a screen turns off or enters ambient mode for a long enough time, a subset of Doze can occur and background tasks may be deferred for certain periods. Later, when a device is stationary for an extended time, regular Doze occurs. You should schedule requests with the WorkManager API, which enables your app to register for Doze-safe code execution.

Schedule with constraints

You can use constraints to configure requests in a way that preserves battery life. Select one or more of the following constraints to include in your requests:

  • Schedule a request that requires networking. Specify if the NetworkType is CONNECTED or UNMETERED. UNMETERED is for large data transfers while CONNECTED is for small transfers.
  • Schedule a request while charging.
  • Schedule a request while the device is idle. This is useful for lower-priority background work or synchronization, especially when the device is charging.

Note that some low-bandwidth networks, such as Bluetooth LE, are considered metered.

For more information, review the WorkManager's Effect of constraints on periodic work guide.