This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 57).
Expo 加速度计
一个提供访问设备加速度计传感器的库。
Accelerometer 来自 expo-sensors,提供对设备加速度计传感器及相关监听器的访问,用于响应三维空间中加速度的变化,也就是任何移动或振动。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
用法
API
import { Accelerometer } from 'expo-sensors';
Classes
Type: Class extends DeviceSensor<AccelerometerMeasurement>
A base class for subscribable sensors. The events emitted by this class are measurements
specified by the parameter type Measurement.
Accelerometer Methods
Subscribe for updates to the accelerometer.
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 Accelerometer.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 the accelerometer.
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
Each of these keys represents the acceleration along that particular axis in g-force (measured in gs).
A g is a unit of gravitational force equal to that exerted by the earth’s gravitational field (9.81 m/s^2).
Literal type: union
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never' | number