Expo Sensors provides comprehensive hardware sensor access for React Native and Expo applications. It offers JavaScript/TypeScript APIs for accessing device accelerometer, gyroscope, magnetometer, barometer, light sensor, and pedometer functionality with unified interfaces across Android, iOS, and web platforms.
npx expo install expo-sensorsimport {
Accelerometer,
Gyroscope,
DeviceMotion,
Magnetometer,
Barometer,
LightSensor,
Pedometer
} from "expo-sensors";For individual sensor imports with types:
import { Accelerometer, AccelerometerMeasurement } from "expo-sensors";
import { Gyroscope, GyroscopeMeasurement } from "expo-sensors";
import { DeviceMotion, DeviceMotionMeasurement, DeviceMotionOrientation } from "expo-sensors";
import { Magnetometer, MagnetometerMeasurement } from "expo-sensors";
import { Barometer, BarometerMeasurement } from "expo-sensors";
import { LightSensor, LightSensorMeasurement } from "expo-sensors";import { Accelerometer, DeviceMotion, Pedometer } from "expo-sensors";
// Subscribe to accelerometer updates
const subscription = Accelerometer.addListener(({ x, y, z, timestamp }) => {
console.log(`Acceleration: x=${x}, y=${y}, z=${z}`);
});
// Check sensor availability
const isAvailable = await Accelerometer.isAvailableAsync();
if (isAvailable) {
// Set update interval (in milliseconds)
Accelerometer.setUpdateInterval(100);
}
// Clean up subscription
subscription.remove();
// Use pedometer for step counting
Pedometer.watchStepCount(({ steps }) => {
console.log(`Steps: ${steps}`);
});Expo Sensors is built around several key components:
Core motion sensing capabilities including accelerometer, gyroscope, and combined device motion tracking. Essential for fitness apps, games, and augmented reality applications.
// Accelerometer - measures device acceleration in g-forces
interface AccelerometerMeasurement {
x: number; // g-forces in X axis
y: number; // g-forces in Y axis
z: number; // g-forces in Z axis
timestamp: number; // measurement timestamp in seconds
}
// Gyroscope - measures device rotation in radians per second
interface GyroscopeMeasurement {
x: number; // rotation in rad/s around X axis
y: number; // rotation in rad/s around Y axis
z: number; // rotation in rad/s around Z axis
timestamp: number; // measurement timestamp in seconds
}Combined motion sensor providing comprehensive device orientation, acceleration, and rotation data. Ideal for applications requiring precise motion tracking and orientation awareness.
interface DeviceMotionMeasurement {
acceleration: null | {
x: number; y: number; z: number; timestamp: number;
};
accelerationIncludingGravity: {
x: number; y: number; z: number; timestamp: number;
};
rotation: {
alpha: number; beta: number; gamma: number; timestamp: number;
};
rotationRate: null | {
alpha: number; beta: number; gamma: number; timestamp: number;
};
interval: number;
orientation: DeviceMotionOrientation;
}
enum DeviceMotionOrientation {
Portrait = 0,
RightLandscape = 90,
UpsideDown = 180,
LeftLandscape = -90,
}Magnetometer functionality for compass applications and magnetic field detection, available in both calibrated and uncalibrated forms.
interface MagnetometerMeasurement {
x: number; // magnetic field strength in μT (X axis)
y: number; // magnetic field strength in μT (Y axis)
z: number; // magnetic field strength in μT (Z axis)
timestamp: number; // measurement timestamp in seconds
}
interface MagnetometerUncalibratedMeasurement {
x: number; // uncalibrated magnetic field strength in μT (X axis)
y: number; // uncalibrated magnetic field strength in μT (Y axis)
z: number; // uncalibrated magnetic field strength in μT (Z axis)
timestamp: number; // measurement timestamp in seconds
}Barometer and light sensor capabilities for measuring atmospheric pressure, altitude, and ambient light conditions.
interface BarometerMeasurement {
pressure: number; // atmospheric pressure in hPa
relativeAltitude?: number; // altitude in meters (iOS only)
timestamp: number; // measurement timestamp in seconds
}
interface LightSensorMeasurement {
illuminance: number; // ambient light level in lux (lx)
timestamp: number; // measurement timestamp in seconds
}Pedometer functionality for fitness and health applications with step counting and historical data access.
interface PedometerResult {
steps: number; // number of steps taken
}
type PedometerUpdateCallback = (result: PedometerResult) => void;
// Functions for step counting
function watchStepCount(callback: PedometerUpdateCallback): Subscription;
function getStepCountAsync(start: Date, end: Date): Promise<PedometerResult>; // iOS only
function isAvailableAsync(): Promise<boolean>;// Base sensor interface
interface DeviceSensor<Measurement> {
addListener(listener: (event: Measurement) => void): Subscription;
hasListeners(): boolean;
getListenerCount(): number;
removeAllListeners(): void;
removeSubscription(subscription: Subscription): void;
setUpdateInterval(intervalMs: number): void;
isAvailableAsync(): Promise<boolean>;
getPermissionsAsync(): Promise<PermissionResponse>;
requestPermissionsAsync(): Promise<PermissionResponse>;
}
// Subscription for event listeners
interface Subscription {
remove(): void;
}
// Permission system types
interface PermissionResponse {
granted: boolean;
expires: 'never' | number;
canAskAgain: boolean;
status: PermissionStatus;
}
enum PermissionStatus {
UNDETERMINED = 'undetermined',
GRANTED = 'granted',
DENIED = 'denied',
}
// Listener function type
type Listener<E> = (event: E) => void;