# Eye

Artifact: [androidx.xr.arcore:arcore](https://developer.android.com/jetpack/androidx/releases/xr-arcore) [View Source](https://cs.android.com/search?q=file:androidx/xr/arcore/Eye.kt+class:androidx.xr.arcore.Eye) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-arcore#1.0.0)

*** ** * ** ***


```
public final class Eye implements Trackable<@NonNull Eye.State>
```

<br />

*** ** * ** ***

A representation of a user's eye.

An `https://developer.android.com/reference/androidx/xr/arcore/Eye` instance provides the state of the eye (shut or gazing), as well as a `https://developer.android.com/reference/androidx/xr/runtime/math/Pose` indicating where the user is currently looking.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/xr/arcore/Eye.State implements https://developer.android.com/reference/androidx/xr/arcore/Trackable.State` The representation of the current state of an `https://developer.android.com/reference/androidx/xr/arcore/Eye`. |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/arcore/Eye.State>` | `https://developer.android.com/reference/androidx/xr/arcore/Eye#getState()()` A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` that contains the latest `https://developer.android.com/reference/androidx/xr/arcore/Eye.State` of an `https://developer.android.com/reference/androidx/xr/arcore/Eye`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/arcore/Eye` | `https://developer.android.com/reference/androidx/xr/arcore/Eye#left(androidx.xr.runtime.Session)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session)` Returns the left eye. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/arcore/Eye` | `https://developer.android.com/reference/androidx/xr/arcore/Eye#right(androidx.xr.runtime.Session)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session)` Returns the right eye. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/xr/arcore/Eye#toString()()` Returns a string representation of `https://developer.android.com/reference/androidx/xr/arcore/Eye` for debugging. |

## Public methods

### getState

```
public @NonNull StateFlow<@NonNull Eye.State> getState()
```

A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html` that contains the latest `https://developer.android.com/reference/androidx/xr/arcore/Eye.State` of an `https://developer.android.com/reference/androidx/xr/arcore/Eye`.

### left

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-arcore#1.0.0)

```
public static final @NonNull Eye left(@NonNull Session session)
```

Returns the left eye.

```kotlin
import androidx.xr.arcore.ArDevice
import androidx.xr.arcore.Eye
import androidx.xr.arcore.TrackingState
import androidx.xr.arcore.hitTest
import androidx.xr.runtime.Session
import androidx.xr.runtime.math.Ray
import androidx.xr.scenecore.scene

// We need the ArDevice object to properly transform the eye pose coordinate space into
// something we can use.
val arDevice = ArDevice.getInstance(session)

// Obtain the left eye from the Session object.
Eye.left(session)?.let {
    // The left eye is available; launch a coroutine
    // to respond to changes in the eye's state.
    yourCoroutineScope.launch {
        it.state.collect { eyeState ->
            // Early out if the eye is not reported as tracking
            if (eyeState.trackingState != TrackingState.TRACKING) return@collect

            // We are only interested in the eye pose if it is open, so early out if it is
            // closed as well.
            if (!eyeState.isOpen) return@collect

            // The pose we get from the eye is the actual pose of the eye. We want to turn that
            // into something we can use for interacting with other objects in the scene (for
            // example, a hit test). To do that, we need to convert the eye pose into the
            // correct coordinate space, and then use it to create a `Ray` we can pass to the
            // `hitTest()` function.
            //
            // Unlike most other objects, which are reported in the perception coordinate space,
            // the eye pose is reported in the device coordinate space. So we'll first need to
            // convert the device pose into the activity space (via the perception space), and
            // then we can use that to convert the eye pose in to the activity space.
            val transformedPose =
                eyeState.pose.let { pose ->
                    val headPose =
                        session.scene.perceptionSpace.getScenePoseFromPerceptionPose(
                            arDevice.state.value.devicePose
                        )
                    headPose.transformPoseTo(pose, session.scene.activitySpace)
                }

            val gazeRay = Ray(transformedPose.translation, transformedPose.backward)

            val hits = hitTest(session, gazeRay)
            yourHitTestHandler(hits)
        }
    }
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session` | the `https://developer.android.com/reference/androidx/xr/runtime/Session` to retrieve the eye from |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if `https://developer.android.com/reference/androidx/xr/runtime/Config#eyeTracking()` is set to `https://developer.android.com/reference/androidx/xr/runtime/EyeTrackingMode#DISABLED()` |

### right

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-arcore#1.0.0)

```
public static final @NonNull Eye right(@NonNull Session session)
```

Returns the right eye.

```kotlin
import androidx.xr.arcore.ArDevice
import androidx.xr.arcore.Eye
import androidx.xr.arcore.TrackingState
import androidx.xr.arcore.hitTest
import androidx.xr.runtime.Session
import androidx.xr.runtime.math.Ray
import androidx.xr.scenecore.scene

// We need the ArDevice object to properly transform the eye pose coordinate space into
// something we can use.
val arDevice = ArDevice.getInstance(session)

// Obtain the right eye from the Session object.
Eye.right(session)?.let {
    // The left eye is available; launch a coroutine
    // to respond to changes in the eye's state.
    yourCoroutineScope.launch {
        it.state.collect { eyeState ->
            // Early out if the eye is not reported as tracking
            if (eyeState.trackingState != TrackingState.TRACKING) return@collect

            // We are only interested in the eye pose if it is open, so early out if it is
            // closed as well.
            if (!eyeState.isOpen) return@collect

            // The pose we get from the eye is the actual pose of the eye. We want to turn that
            // into something we can use for interacting with other objects in the scene (for
            // example, a hit test). To do that, we need to convert the eye pose into the
            // correct coordinate space, and then use it to create a `Ray` we can pass to the
            // `hitTest()` function.
            //
            // Unlike most other objects, which are reported in the perception coordinate space,
            // the eye pose is reported in the device coordinate space. So we'll first need to
            // convert the device pose into the activity space (via the perception space), and
            // then we can use that to convert the eye pose in to the activity space.
            val transformedPose =
                eyeState.pose.let { pose ->
                    val headPose =
                        session.scene.perceptionSpace.getScenePoseFromPerceptionPose(
                            arDevice.state.value.devicePose
                        )
                    headPose.transformPoseTo(pose, session.scene.activitySpace)
                }

            val gazeRay = Ray(transformedPose.translation, transformedPose.backward)

            val hits = hitTest(session, gazeRay)
            yourHitTestHandler(hits)
        }
    }
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session` | the `https://developer.android.com/reference/androidx/xr/runtime/Session` to retrieve the eye from |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if `https://developer.android.com/reference/androidx/xr/runtime/Config#eyeTracking()` is set to `https://developer.android.com/reference/androidx/xr/runtime/EyeTrackingMode#DISABLED()` |

### toString

```
public @NonNull String toString()
```

Returns a string representation of `https://developer.android.com/reference/androidx/xr/arcore/Eye` for debugging.

Note: Not intended for production use.