Langsung ke konten

Getting Started with Barometer

Konten ini belum tersedia dalam bahasa Anda.

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

Installation

Install the plugin using npm:

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

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

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

Basic Usage

Import the Plugin

import { Barometer } from '@capgo/capacitor-barometer';

Check Sensor Availability

const checkBarometer = async () => {
const { available } = await Barometer.isAvailable();
console.log('Barometer available:', available);
};

Start Monitoring Pressure

const startMonitoring = async () => {
await Barometer.start({
interval: 1000 // Update interval in milliseconds
});
console.log('Barometer monitoring started');
};

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

const getCurrentPressure = async () => {
const reading = await Barometer.getCurrentReading();
console.log('Current pressure:', reading.pressure, 'hPa');
console.log('Relative altitude:', reading.relativeAltitude, 'm');
};

Stop Monitoring

const stopMonitoring = async () => {
await Barometer.stop();
console.log('Barometer monitoring stopped');
};

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();
}
}
// Usage
const barometerService = new BarometerService();
barometerService.initialize();
// Cleanup when done
// barometerService.cleanup();

Understanding Readings

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

  • Calculated from pressure changes
  • Relative to starting reference point
  • Accuracy: ±10-30 meters
  • Works best for short-term tracking

Best Practices

  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

Common Issues

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

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

  • Explore the API Reference for complete method documentation
  • Check out the example app for advanced usage
  • See the tutorial for complete implementation examples