A library that provides access to a device's motion and orientation sensors.
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.
Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
-
npx expo install expo-sensors
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
import { DeviceMotion } from 'expo-sensors';
DeviceMotion.isAvailableAsync()
You should always check the sensor availability before attempting to use it.
Returns whether the DeviceMotion
API is enabled on the device.
On mobile web, you need to request permissions for a sensor in response to a user interaction (touch event) before you can use this module. For example, Accelerometer.requestPermissionsAsync()
If the status
is not equal to granted
then you should inform the end user that they may have to open settings.
On web this starts a timer and waits to see if an event is fired. This should predict if the iOS device has the device orientation API disabled in Settings > Safari > Motion & Orientation Access
. Some devices will also not fire if the site isn't hosted with HTTPS as DeviceMotion
is now considered a secure API. There is no formal API for detecting the status of DeviceMotion
so this API can sometimes be unreliable on web.
boolean
denoting the availability of the sensor.DeviceMotion.addListener(listener)
Subscribe for updates to DeviceMotion.
listener (function) -- A callback that is invoked when a DeviceMotion update is available. When invoked, the listener is provided a single argument that is an object containing following fields:
interval (number) -- Interval at which data is obtained from the native platform. Expressed in milliseconds.
acceleration (object) -- Device acceleration on the three axis as an object with x, y, z keys. Expressed in m/s2.
accelerationIncludingGravity (object) -- Device acceleration with the effect of gravity on the three axis as an object with x, y, z keys. Expressed in m/s2.
rotation (object) -- 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.
rotationRate (object) -- Device's rate of rotation in space expressed in degrees per second (deg/s).
orientation (number) -- Device orientation based on screen rotation. Value is on of 0
(portrait), 90
(right landscape), 180
(upside down), -90
(left landscape).
remove()
on when you
would like to unsubscribe the listener.DeviceMotion.removeAllListeners()
Remove all listeners.
DeviceMotion.setUpdateInterval(intervalMs)
Subscribe for updates to DeviceMotion.