Guides
Plan-enterprise-icon
Expo Application Services
API Reference

DeviceMotion

DeviceMotion from expo-sensors provides access to the device motion and orientation sensors. All data is presented in terms of three axes that run through a device. According to portrait orientation: X runs from left to right, Y from bottom to top and Z perpendicularly through the screen from back to front.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb
Status-success-iconStatus-success-iconStatus-success-iconStatus-success-iconStatus-success-icon

Installation

Terminal
→ npx expo install expo-sensors

If you're installing this in a bare React Native app, you should also follow these additional installation instructions.

API

import { DeviceMotion } from 'expo-sensors';

Constants

Gravity

Type: number

Constant value representing standard gravitational acceleration for Earth (9.80665 m/s^2).

Classes

DeviceMotion

Type: Class extends DeviceSensor<DeviceMotionMeasurement>

DeviceMotion Properties

getListenerCount()

Returns the registered listeners count.

  • Undo-iconnumber

getPermissionsAsync()

Checks user's permissions for accessing sensor.

hasListeners()

Returns boolean which signifies if sensor has any listeners registered.

  • Undo-iconboolean

removeAllListeners()

Removes all registered listeners.

  • Undo-iconvoid

removeSubscription(subscription)

NameTypeDescription
subscriptionSubscription

A subscription to remove.

Removes the given subscription.

  • Undo-iconvoid

requestPermissionsAsync()

Asks the user to grant permissions for accessing sensor.

setUpdateInterval(intervalMs)

NameTypeDescription
intervalMsnumber

Desired interval in milliseconds between sensor updates.

Info-icon

Starting from Android 12 (API level 31), the system has a 200ms limit for each sensor updates.

If you need an update interval less than 200ms, you should:

  • add android.permission.HIGH_SAMPLING_RATE_SENSORS to app.json permissions field
  • or if you are using bare workflow, add <uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS"/> to AndroidManifest.xml.

Set the sensor update interval.

  • Undo-iconvoid

Interfaces

PermissionResponse

An object obtained by permissions get and request functions.

NameTypeDescription
canAskAgainbooleanIndicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission.
expiresPermissionExpirationDetermines time when the permission expires.
grantedbooleanA convenience boolean that indicates if the permission is granted.
statusPermissionStatusDetermines the status of the permission.

Types

DeviceMotionMeasurement

NameTypeDescription
accelerationnull | { x: number, y: number, z: number }Device acceleration on the three axis as an object with x, y, z keys. Expressed in meters per second squared (m/s^2).
accelerationIncludingGravity{ x: number, y: number, z: number }Device acceleration with the effect of gravity on the three axis as an object with x, y, z keys. Expressed in meters per second squared (m/s^2).
intervalnumberInterval at which data is obtained from the native platform. Expressed in milliseconds (ms).
orientationDeviceMotionOrientationDevice orientation based on screen rotation. Value is one of:
  • 0 (portrait),
  • 90 (right landscape),
  • 180 (upside down),
  • -90 (left landscape).
rotation{ alpha: number, beta: number, gamma: number }Device's orientation in space as an object with alpha, beta, gamma keys where alpha is for rotation around Z axis, beta for X axis rotation and gamma for Y axis rotation.
rotationRatenull | { alpha: number, beta: number, gamma: number }Device's rate of rotation in space expressed in degrees per second (deg/s).

PermissionExpiration

Permission expiration time. Currently, all permissions are granted permanently.

Acceptable values are: 'never', number.

Subscription

NameTypeDescription
remove() => voidA method to unsubscribe the listener.

Enums

DeviceMotionOrientation

DeviceMotionOrientation.LeftLandscape = -90
DeviceMotionOrientation.Portrait = 0
DeviceMotionOrientation.RightLandscape = 90
DeviceMotionOrientation.UpsideDown = 180

PermissionStatus

PermissionStatus.DENIED = "denied"

User has denied the permission.

PermissionStatus.GRANTED = "granted"

User has granted the permission.

PermissionStatus.UNDETERMINED = "undetermined"

User hasn't granted or denied the permission yet.

  • Message-iconAsk a question on the forums about DeviceMotion
  • Github-iconView open bug reports for DeviceMotion
  • Code-iconView source code for DeviceMotion
  • Build-iconView package in npm Registry
  • Edit-iconEdit this page

Was this doc helpful?