Android 12 Developer Preview is here! Try it out, and give us your feedback!

Detect location on Wear OS

A watch's small, glanceable form factor makes Wear OS an ideal platform for apps that record, report, and respond to user location. As examples, you can build apps that give users real-time updates on their distance, speed, and direction, or provide glanceable cues about users' surroundings.

For more information, see Build location-aware apps.

Some watches have a built-in GPS sensor that retrieves location data without requiring a connected phone. When you request location data in a watch app, the system will get location from either the phone or watch using the most power-efficient method. So even without a GPS sensor in the watch, you can still get location information.

As described in the following sections, your app needs to handle the loss of location data when a watch without a sensor becomes disconnected from a phone.

The recommended method of getting location data on a watch is to use the Fused Location Provider (FLP). FLP is a Google Play services API. For when you can't use the FLP, this document describes how to check for on-watch location sensors, receive location data, and use phone-dependent data connections. For watches paired with iPhones, see Location data for watches paired to iPhones.

To reduce the negative impact of location data acquisition on battery life, ensure your app calls setPriority() with the value PRIORITY_BALANCED_POWER_ACCURACY. Different priority settings may optimize chips differently.

When possible, to conserve battery you should ask for location no more frequently than once per minute using setInterval().

Note: This document assumes that you know how to use the FLP to retrieve location data.

Use the Fused Location Provider

On a watch, get location data using the FusedLocationProviderClient. The FLP automatically uses location data from the phone if the watch lacks a GPS sensor. For more information, see Create location services client.

For information about requesting location updates and continuously tracking a user's location, see Request location updates.

Note: When creating a LocationRequest, it's important to consider batching using the setMaxWaitTime() because it can consume less battery and give a more accurate location, depending on the device's hardware capabilities.

Detect on-board GPS

If a user goes jogging with a watch that lacks a built-in GPS sensor, and leaves the paired phone behind, your watch app can't get location data through a connected device. Your app should detect the situation and warn the user that location functionality is unavailable.

To determine if a watch has a built-in GPS sensor, call the hasSystemFeature() method with PackageManager.FEATURE_LOCATION_GPS. The following code detects whether the watch has a built-in GPS sensor when you start an activity:

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)

    if (!hasGps()) {
        Log.d(TAG, "This hardware doesn't have GPS.")
        // Fall back to functionality that doesn't use location or
        // warn the user that location function isn't available.
    }
}

private fun hasGps(): Boolean =
        packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)

Java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    if (!hasGps()) {
        Log.d(TAG, "This hardware doesn't have GPS.");
        // Fall back to functionality that doesn't use location or
        // warn the user that location function isn't available.
    }

    ...
}

private boolean hasGps() {
    return getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
}

Handle disconnection events

If a watch has no built-in GPS sensor and loses connection to a phone, the watch loses its location data stream. If your app expects a constant stream of data, your app must detect the loss of a connection, warn the user, and gracefully degrade in functionality.

As with a mobile device, when you request location updates using FusedLocationProviderClient.requestLocationUpdates(), you pass in either a LocationCallback or a PendingIntent. Both of these include the location information and the LocationAvailability status.

When using the LocationCallback option, override onLocationAvailability() to receive updates regarding location availability status.

When using the PendingIntent option and an Intent is returned, extract the location availability status from the Intent using the LocationAvailability.extractLocationAvailability(Intent).

Handle location not found

When the GPS signal is lost, you can retrieve the last known location of the user's watch. Retrieving the last known location is helpful when you can't get a GPS fix, or when the watch lacks built-in GPS and loses its connection with the phone. For more information, see Get the last known location.

Flush location with batched calls

If you are using batched calls, call flushLocations() when the screen comes back on (or returns from ambient mode) to immediately return any batched locations to all registered LocationListeners, LocationCalllbacks, and Pending Intents.