This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo DeviceMotion
一个提供设备运动和方向传感器访问权限的库
expo-sensors 中的 DeviceMotion 提供对设备运动和方向传感器的访问。所有数据均以穿过设备的三个轴为基准。以纵向为准:X 轴从左向右,Y 轴从底部向顶部,Z 轴垂直穿过屏幕,从背面指向正面。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
在应用配置中配置
如果项目使用配置插件(持续原生生成(CNG)),则可以使用内置的配置插件来配置来自 expo-sensor 的 DeviceMotion。该插件允许配置各种无法在运行时设置、且必须重新构建应用二进制文件才能生效的属性。如果应用不使用 CNG,则需要手动配置该库。
Example app.json with config plugin
Configurable properties
Are you using this library in an existing React Native app?
如果未使用持续原生生成(CNG),或者手动使用原生 ios 项目,则需要在原生项目中配置 NSMotionUsageDescription 键,才能访问 DeviceMotion 数据:
API
import { DeviceMotion } from 'expo-sensors';
Constants
Type: number
Constant value representing standard gravitational acceleration for Earth (9.80665 m/s^2).
Classes
Type: Class extends DeviceSensor<DeviceMotionMeasurement>
A base class for subscribable sensors. The events emitted by this class are measurements
specified by the parameter type Measurement.
DeviceMotion Properties
DeviceMotion Methods
Subscribe for updates to the device motion sensor.
EventSubscriptionA subscription that you can call remove() on when you would like to unsubscribe the listener.
Checks user's permissions for accessing sensor.
Promise<PermissionResponse>Returns boolean which signifies if sensor has any listeners registered.
booleanYou should always check the sensor availability before attempting to use it.
Returns whether the accelerometer is enabled on the device.
On mobile web, you must first invoke DeviceMotion.requestPermissionsAsync() in a user interaction (i.e. touch event) before you can use this module.
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.
Promise<boolean>A promise that resolves to a boolean denoting the availability of device motion sensor.
Deprecated: use subscription.remove() instead.
voidAsks the user to grant permissions for accessing sensor.
Promise<PermissionResponse>Interfaces
A subscription object that allows to conveniently remove an event listener from the emitter.
Types
Literal type: union
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never' | number
Enums
权限
iOS
此库使用以下使用说明键: