Saltar al contenido

Getting Started with Barometer

Este contenido aún no está disponible en tu idioma.

This guide will walk you through integrating the Capacitor Barometer plugin into your application.

Install the plugin using npm:

Terminal window
npm install @capgo/capacitor-barometer
npx cap sync

Add the following to your Info.plist:

<key>NSMotionUsageDescription</key>
<string>This app uses the barometer to measure atmospheric pressure</string>

No additional configuration required. The plugin automatically requests necessary permissions.

import { Barometer } from '@capgo/capacitor-barometer';
const checkBarometer = async () => {
const { available } = await Barometer.isAvailable();
console.log('Barometer available:', available);
};
const startMonitoring = async () => {
await Barometer.start({
interval: 1000 // Update interval in milliseconds
});
console.log('Barometer monitoring started');
};
Barometer.addListener('pressureChange', (data) => {
console.log('Pressure (hPa):', data.pressure);
console.log('Relative altitude (m):', data.relativeAltitude);
console.log('Timestamp:', data.timestamp);
});
const getCurrentPressure = async () => {
const reading = await Barometer.getCurrentReading();
console.log('Current pressure:', reading.pressure, 'hPa');
console.log('Relative altitude:', reading.relativeAltitude, 'm');
};
const stopMonitoring = async () => {
await Barometer.stop();
console.log('Barometer monitoring stopped');
};

Here’s a complete example showing barometer integration:

import { Barometer } from '@capgo/capacitor-barometer';
class BarometerService {
private listener: any;
async initialize() {
// Check availability
const { available } = await Barometer.isAvailable();
if (!available) {
console.error('Barometer not available on this device');
return;
}
// Start monitoring
await Barometer.start({ interval: 1000 });
// Add listener
this.listener = Barometer.addListener('pressureChange', (data) => {
this.handlePressureChange(data);
});
}
handlePressureChange(data: any) {
console.log('Pressure:', data.pressure, 'hPa');
console.log('Altitude:', data.relativeAltitude, 'm');
// Update UI or process data
this.updateDisplay(data);
}
updateDisplay(data: any) {
// Convert hPa to other units
const inHg = (data.pressure * 0.02953).toFixed(2);
const mmHg = (data.pressure * 0.750062).toFixed(1);
console.log(`Pressure: ${data.pressure.toFixed(1)} hPa`);
console.log(` ${inHg} inHg`);
console.log(` ${mmHg} mmHg`);
console.log(`Altitude: ${data.relativeAltitude.toFixed(1)} m`);
}
async cleanup() {
// Remove listener
if (this.listener) {
this.listener.remove();
}
// Stop monitoring
await Barometer.stop();
}
}
// Usage
const barometerService = new BarometerService();
barometerService.initialize();
// Cleanup when done
// barometerService.cleanup();
  • Measured in hectopascals (hPa) or millibars (mb)
  • Standard sea-level pressure: 1013.25 hPa
  • Higher altitude = lower pressure
  • Weather systems affect pressure readings
  • Calculated from pressure changes
  • Relative to starting reference point
  • Accuracy: ±10-30 meters
  • Works best for short-term tracking
  1. Check Availability: Always verify sensor availability before use
  2. Reasonable Intervals: Don’t poll too frequently (1000ms is good)
  3. Remove Listeners: Clean up listeners when not needed
  4. Calibration: Use known altitude for accurate readings
  5. Battery Impact: Monitor battery usage with continuous sensing
try {
const { available } = await Barometer.isAvailable();
if (!available) {
// Fallback to GPS altitude or manual input
showAlternativeMethod();
}
} catch (error) {
console.error('Barometer check failed:', error);
}
class PressureFilter {
private readings: number[] = [];
private maxReadings = 5;
addReading(pressure: number): number {
this.readings.push(pressure);
if (this.readings.length > this.maxReadings) {
this.readings.shift();
}
// Return moving average
const sum = this.readings.reduce((a, b) => a + b, 0);
return sum / this.readings.length;
}
}
  • Explore the API Reference for complete method documentation
  • Check out the example app for advanced usage
  • See the tutorial for complete implementation examples