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 轴垂直穿过屏幕,从后到前。
安装
🌐 Installation
If you are installing this in an existing React Native app, make sure to install expo in your project.
应用配置中的配置
🌐 Configuration in app config
如果你在项目中使用配置插件(连续原生生成 (CNG)),可以通过其内置的配置插件从expo-sensor配置DeviceMotion。该插件允许你配置各种无法在运行时设置的属性,并需要构建新的应用二进制文件才能生效。如果你的应用不使用CNG,则需要手动配置该库。
🌐 You can configure DeviceMotion from expo-sensor using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.
Example app.json with config plugin
Configurable properties
Are you using this library in an existing React Native app?
如果你没有使用连续原生生成(CNG),或者你是手动使用原生 iOS 项目,那么你需要在你的原生项目中配置 NSMotionUsageDescription 密钥以访问 DeviceMotion 统计数据:
🌐 If you're not using Continuous Native Generation (CNG) or you're using native ios project manually, then you need to configure NSMotionUsageDescription key in your native project to access DeviceMotion stats:
应用接口
🌐 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
权限
🌐 Permissions
iOS
该库使用以下用法描述键:
🌐 The following usage description keys are used by this library: