**Deprecation notice: Awareness API is deprecated.**

> [!WARNING]
> **Warning:** The Awareness API is deprecated.
>
> - Google Play services will stop supporting the Awareness API in a future release, as early as January 2027.
> - There is no direct replacement.

This section shows how to use the [Snapshot API](https://developers.google.com/android/reference/com/google/android/gms/awareness/Awareness.md.txt#getSnapshotClient(android.app.Activity))
to get the current state for each of the supported context types. For
more information, see [Get started](https://developers.google.com/awareness/android-api/get-started.md.txt). For
details on deprecated contextual signals, open the following expandable notice:

## Get the current activity

To get the user's current activity, call [`getDetectedActivity()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getDetectedActivity()), which returns an [`ActivityRecognitionResult`](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionResult.md.txt)
that contains information about the user's most recent activities.

The [`getDetectedActivity()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getDetectedActivity()) method requires the
`com.google.android.gms.permission.ACTIVITY_RECOGNITION` permission. Add this
permission to [`AndroidManifest.xml`](https://developers.google.com/awareness/android-api/get-started.md.txt#declare_android_permissions_in_androidmanifestxml).

To get the user's current activity, perform the following steps:

1. Call [`getSnapshotClient()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/Awareness.md.txt#getSnapshotClient(android.app.Activity)) to create an instance of the [`SnapshotClient`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt).
2. Use [`addOnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task.md.txt#addOnSuccessListener(java.util.concurrent.Executor,%20com.google.android.gms.tasks.OnSuccessListener%3C?%20super%20TResult%3E)) to create an [`OnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/OnSuccessListener.md.txt) that can listen for a [`DetectedActivityResponse`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/DetectedActivityResponse.md.txt).
3. Call [`getStatus()`](https://developers.google.com/android/reference/com/google/android/gms/common/api/Result.md.txt#getStatus()) to ensure that the result is valid.
4. Call [`DetectedActivityResponse.getActivityRecognitionResult()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/DetectedActivityResponse.md.txt#getActivityRecognitionResult())
   to return an [`ActivityRecognitionResult`](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionResult.md.txt).
   You can use this to get many aspects of the user's current activity. For
   example:

   - Call [`getMostProbableActivity()`](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionResult.md.txt#getMostProbableActivity()) to get only the most probable activity.
   - Call [`getProbableActivities()`](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionResult.md.txt#getProbableActivities()) to get a list of recent activities ranked by probability.
   - Call [`getActivityConfidence()`](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionResult.md.txt#getActivityConfidence(int)) to return the confidence value for a given activity type.
   - Call [`hasResult()`](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionResult.md.txt#hasResult(android.content.Intent)) to detect whether an `Intent` contains an [`ActivityRecognitionResult`](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionResult.md.txt).

The following code example uses [`getMostProbableActivity()`](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionResult.md.txt#getMostProbableActivity())
to get the most probable detected activity, and to log the result to the
console:

    Awareness.getSnapshotClient(this).getDetectedActivity()
        .addOnSuccessListener(new OnSuccessListener<DetectedActivityResponse>() {
            @Override
            public void onSuccess(DetectedActivityResponse dar) {
                ActivityRecognitionResult arr = dar.getActivityRecognitionResult();
                DetectedActivity probableActivity = arr.getMostProbableActivity();

                int confidence = probableActivity.getConfidence();
                String activityStr = probableActivity.toString();
                mLogFragment.getLogView().println("Activity: " + activityStr
                    + ", Confidence: " + confidence + "/100");
            }
        })

## Get nearby beacons

To get information about nearby [beacons](https://developers.google.com/beacons), call [`getBeaconState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getBeaconState(com.google.android.gms.awareness.state.BeaconState.TypeFilter...)).
Beacon data consists of the content, type, and namespace of any attachments.

The [`getBeaconState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getBeaconState(com.google.android.gms.awareness.state.BeaconState.TypeFilter...)) method requires the `android.permission.ACCESS_FINE_LOCATION`
permission. Add this permission to [`AndroidManifest.xml`](https://developers.google.com/awareness/android-api/get-started.md.txt#declare_android_permissions_in_androidmanifestxml).
In addition, you must activate the Nearby Messages API for your Google Developers Console
project. For more information, see [Signup and API Keys](https://developers.google.com/awareness/android-api/get-a-key.md.txt#get_an_api_key_from_the)
and [Get started](https://developers.google.com/awareness/android-api/get-started.md.txt#add_your_api_key).

> [!CAUTION]
> **Caution:** `getBeaconState()` is only supported on devices that run API level 18 (Jelly Bean) or higher. If you call from a device that runs an earlier version, `getStatus()` returns the status code `API_NOT_AVAILABLE`.

To get information about nearby beacons, perform the following steps:

1. Check whether the user has granted the required permissions. The following
   example checks to see whether the `android.permission.ACCESS_FINE_LOCATION`
   permission is granted. If not, the user is prompted for consent.

       if (ContextCompat.checkSelfPermission(
                   MainActivity.this,
                   Manifest.permission.ACCESS_FINE_LOCATION) !=
                   PackageManager.PERMISSION_GRANTED) {
               ActivityCompat.requestPermissions(
                       MainActivity.this,
                       new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                       MY_PERMISSION_LOCATION
               );
               return;
           }

2. Define a [`BeaconState.TypeFilter`](https://developers.google.com/android/reference/com/google/android/gms/awareness/state/BeaconState.TypeFilter.md.txt).
   This returns only beacons with attachments that are registered with the
   specified namespace and type. You can also filter based on a byte-for-byte match
   on the attachment content. The following example shows how to create a type
   filter:

       private static final List<BeaconState.TypeFilter> BEACON_TYPE_FILTERS = Arrays.asList(
               BeaconState.TypeFilter.with(
                   "my.beacon.namespace",
                   "my-attachment-type"),
               BeaconState.TypeFilter.with(
                   "my.other.namespace",
                   "my-attachment-type"));

3. Call [`getSnapshotClient.getBeaconState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#public-taskbeaconstateresponse-getbeaconstate-collectionbeaconstate.typefilter-beacontypes).

4. Use [`addOnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task.md.txt#addOnSuccessListener(java.util.concurrent.Executor,%20com.google.android.gms.tasks.OnSuccessListener%3C?%20super%20TResult%3E))
   to create an [`OnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/OnSuccessListener.md.txt) that can listen for a [`BeaconStateResponse`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/BeaconStateResponse.md.txt).

5. Call [`getStatus()`](https://developers.google.com/android/reference/com/google/android/gms/common/api/Result.md.txt#getStatus())
   to ensure that the result is valid.

6. Call [`BeaconStateResponse.getBeaconState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/BeaconStateResponse.md.txt#getBeaconState())
   to return the beacon state.

7. Call [`BeaconState.getBeaconInfo()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/state/BeaconState.md.txt#getBeaconInfo())
   to get a [`BeaconState.BeaconInfo`](https://developers.google.com/android/reference/com/google/android/gms/awareness/state/BeaconState.BeaconInfo.md.txt).

The following example shows how to get beacon info:

    Awareness.getSnapshotClient(this).getBeaconState(BEACON_TYPE_FILTERS)
        .addOnSuccessListener(new OnSuccessListener<BeaconStateResponse>() {
            @Override
            public void onSuccess(BeaconStateResponse beaconStateResponse) {
                BeaconStateResult beaconStateResult = beaconStateResponse.getBeaconState();
                BeaconState.BeaconInfo beaconInfo = beaconStateResponse.getBeaconInfo();
            }
        })

## Get headphone state

To detect whether headphones are plugged into the device, call [`getHeadphoneState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getHeadphoneState()),
which creates a [`HeadphoneStateResponse`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/HeadphoneStateResponse.md.txt)
detect state with [`OnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/OnSuccessListener.md.txt) set to detect.
You can then call [`getHeadphoneState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getHeadphoneState()) to get the [`HeadphoneState`](https://developers.google.com/android/reference/com/google/android/gms/awareness/state/HeadphoneState.md.txt).

To get the current headphone state, perform the following steps:

1. Call [`getSnapshotClient.getHeadphoneState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getHeadphoneState()).
2. Use [`addOnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task.md.txt#addOnSuccessListener(java.util.concurrent.Executor,%20com.google.android.gms.tasks.OnSuccessListener%3C?%20super%20TResult%3E)) to create an [`OnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/OnSuccessListener.md.txt) that can listen for a [`HeadphoneStateResponse`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/HeadphoneStateResponse.md.txt).
3. Call [`getStatus()`](https://developers.google.com/android/reference/com/google/android/gms/common/api/Result.md.txt#getStatus()) to ensure that the result is valid.
4. On success, call [`HeadphoneStateResponse.getHeadphoneState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/HeadphoneStateResponse.md.txt#getHeadphoneState()) to return the headphone state. This value is either `PLUGGED_IN` or `UNPLUGGED`.

The following code example shows how to use [`getHeadphoneState()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getHeadphoneState()):

    Awareness.getSnapshotClient(this).getHeadphoneState()
        .addOnSuccessListener(new OnSuccessListener<HeadphoneStateResponse>() {
            @Override
            public void onSuccess(HeadphoneStateResponse headphoneStateResponse) {
                HeadphoneState headphoneState = headphoneStateResponse.getHeadphoneState();
                boolean pluggedIn = headphoneState.getState() == HeadphoneState.PLUGGED_IN;
                String stateStr =
                    "Headphones are " + (pluggedIn ? "plugged in" : "unplugged");
                mLogFragment.getLogView().println(stateStr);
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Could not get headphone state: " + e);
            }
        });

## Get location

You can get the user's current location (latitude-longitude) with a call to [`getLocation()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getLocation()), which returns a [`LocationResponse`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/LocationResponse.md.txt).
You can then call [`LocationResponse.getLocation()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/LocationResponse.md.txt#getLocation())
to get a [`Location`](https://developer.android.com/reference/android/location/Location.html)
with the current location data.

The [`getLocation()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getLocation()) method requires the
`android.permission.ACCESS_FINE_LOCATION` permission. Add this permission to
[`AndroidManifest.xml`](https://developers.google.com/awareness/android-api/get-started.md.txt#declare_android_permissions_in_androidmanifestxml).

To get the current location, perform the following steps:

1. Check whether the user has granted the required permissions. The following
   example checks to see whether the `android.permission.ACCESS_FINE_LOCATION`
   permission has been granted. If not, the user is prompted for consent.


       if (ContextCompat.checkSelfPermission(
                   MainActivity.this,
                   Manifest.permission.ACCESS_FINE_LOCATION) !=
                   PackageManager.PERMISSION_GRANTED) {
               ActivityCompat.requestPermissions(
                       MainActivity.this,
                       new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                       MY_PERMISSION_LOCATION
               );
               return;
           }

2. Call [`getSnapshotClient.getLocation()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/SnapshotClient.md.txt#getLocation()).

3. Use [`addOnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task.md.txt#addOnSuccessListener(java.util.concurrent.Executor,%20com.google.android.gms.tasks.OnSuccessListener%3C?%20super%20TResult%3E))
   to create an [`OnSuccessListener`](https://developers.google.com/android/reference/com/google/android/gms/tasks/OnSuccessListener.md.txt) that can listen for a [`LocationResponse`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/LocationResponse.md.txt).

4. Call [`getStatus()`](https://developers.google.com/android/reference/com/google/android/gms/common/api/Result.md.txt#getStatus())
   to ensure that the result is valid.

5. Call [`LocationResponse.getLocation()`](https://developers.google.com/android/reference/com/google/android/gms/awareness/snapshot/LocationResponse.md.txt#getLocation())
   to return the current [`Location`](https://developer.android.com/reference/android/location/Location.html).

The following example shows how to get the current location:

    Awareness.getSnapshotClient(this).getLocation()
        .addOnSuccessListener(new OnSuccessListener<LocationResponse>() {
            @Override
            public void onSuccess(LocationResponse locationResponse) {
                Location loc = locationResponse.getLocationResult();
            }
        })