Getting Started with Barometer
Dieser Inhalt ist in Ihrer Sprache noch nicht verfügbar.
This guide will walk you through integrating the Capacitor Barometer plugin into your application.
Installation
Section titled “Installation”Install the plugin using npm:
npm install @capgo/capacitor-barometernpx cap synciOS Configuration
Section titled “iOS Configuration”Add the following to your Info.plist:
<key>NSMotionUsageDescription</key><string>This app uses the barometer to measure atmospheric pressure</string>Android Configuration
Section titled “Android Configuration”No additional configuration required. The plugin automatically requests necessary permissions.
Basic Usage
Section titled “Basic Usage”Import the Plugin
Section titled “Import the Plugin”import { Barometer } from '@capgo/capacitor-barometer';Check Sensor Availability
Section titled “Check Sensor Availability”const checkBarometer = async () => { const { available } = await Barometer.isAvailable(); console.log('Barometer available:', available);};Start Monitoring Pressure
Section titled “Start Monitoring Pressure”const startMonitoring = async () => { await Barometer.start({ interval: 1000 // Update interval in milliseconds });
console.log('Barometer monitoring started');};Listen to Pressure Events
Section titled “Listen to Pressure Events”Barometer.addListener('pressureChange', (data) => { console.log('Pressure (hPa):', data.pressure); console.log('Relative altitude (m):', data.relativeAltitude); console.log('Timestamp:', data.timestamp);});Get Current Reading
Section titled “Get Current Reading”const getCurrentPressure = async () => { const reading = await Barometer.getCurrentReading(); console.log('Current pressure:', reading.pressure, 'hPa'); console.log('Relative altitude:', reading.relativeAltitude, 'm');};Stop Monitoring
Section titled “Stop Monitoring”const stopMonitoring = async () => { await Barometer.stop(); console.log('Barometer monitoring stopped');};Complete Example
Section titled “Complete Example”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(); }}
// Usageconst barometerService = new BarometerService();barometerService.initialize();
// Cleanup when done// barometerService.cleanup();Understanding Readings
Section titled “Understanding Readings”Atmospheric Pressure
Section titled “Atmospheric Pressure”- Measured in hectopascals (hPa) or millibars (mb)
- Standard sea-level pressure: 1013.25 hPa
- Higher altitude = lower pressure
- Weather systems affect pressure readings
Relative Altitude
Section titled “Relative Altitude”- Calculated from pressure changes
- Relative to starting reference point
- Accuracy: ±10-30 meters
- Works best for short-term tracking
Best Practices
Section titled “Best Practices”- Check Availability: Always verify sensor availability before use
- Reasonable Intervals: Don’t poll too frequently (1000ms is good)
- Remove Listeners: Clean up listeners when not needed
- Calibration: Use known altitude for accurate readings
- Battery Impact: Monitor battery usage with continuous sensing
Common Issues
Section titled “Common Issues”Sensor Not Available
Section titled “Sensor Not Available”try { const { available } = await Barometer.isAvailable(); if (!available) { // Fallback to GPS altitude or manual input showAlternativeMethod(); }} catch (error) { console.error('Barometer check failed:', error);}Noisy Data
Section titled “Noisy Data”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; }}Next Steps
Section titled “Next Steps”- Explore the API Reference for complete method documentation
- Check out the example app for advanced usage
- See the tutorial for complete implementation examples