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 retrieves the location from either the phone or the watch using the most power-efficient method. So even without a GPS sensor in the watch, you can still get location information.
To reduce the effect of location data acquisition on battery life,
call
setPriority()
with the value
PRIORITY_BALANCED_POWER_ACCURACY
.
Different priority settings may
optimize chips differently.
When possible, conserve battery by asking for location no more
than once per minute using
setInterval()
.
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.
Choose your method
There are a couple of ways to provide location data to a Wear OS app. You can use the Fused Location Provider (FLP) or Wear Health Services (WHS). FLP is a Google Play services API.
Use FLP in the following circumstances:
- You want location data in the moment but not continuously, such as marking the location of a parked car.
- You want location continuously but don't need the location history.
Use WHS in the following circumstances:
- You want data from other sensors or are likely to want data from other sensors in the future.
- Your app is a workout or exercise app that needs to track location data over the course of a specific time interval.
For watches paired with iPhones, see Location data for watches paired to iPhones.
Use the Fused Location Provider
On a watch, get location data using the
FusedLocationProviderClient
.
The FLP may use location data from the phone. 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.
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 the connected device. Detect this situation in your app and warn the user that location capabilities are unavailable.
To determine whether 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)
method.
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 and 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
,
LocationCallbacks
, and Pending Intents
.
Recommended for you
- Note: link text is displayed when JavaScript is off
- Optimize location for battery
- Create a notification {:#notification}
- Detect when users start or end an activity