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
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