Barometer 시작하기
이 가이드는 애플리케이션에 Capacitor Barometer 플러그인을 통합하는 과정을 안내합니다.
npm을 사용하여 플러그인을 설치하세요:
npm install @capgo/capacitor-barometernpx cap synciOS 구성
Section titled “iOS 구성”Info.plist에 다음을 추가하세요:
<key>NSMotionUsageDescription</key><string>This app uses the barometer to measure atmospheric pressure</string>Android 구성
Section titled “Android 구성”추가 구성이 필요하지 않습니다. 플러그인은 필요한 권한을 자동으로 요청합니다.
기본 사용법
Section titled “기본 사용법”플러그인 가져오기
Section titled “플러그인 가져오기”import { Barometer } from '@capgo/capacitor-barometer';센서 가용성 확인
Section titled “센서 가용성 확인”const checkBarometer = async () => { const { available } = await Barometer.isAvailable(); console.log('Barometer available:', available);};압력 모니터링 시작
Section titled “압력 모니터링 시작”const startMonitoring = async () => { await Barometer.start({ interval: 1000 // Update interval in milliseconds });
console.log('Barometer monitoring started');};압력 이벤트 수신
Section titled “압력 이벤트 수신”Barometer.addListener('pressureChange', (data) => { console.log('Pressure (hPa):', data.pressure); console.log('Relative altitude (m):', data.relativeAltitude); console.log('Timestamp:', data.timestamp);});현재 판독값 가져오기
Section titled “현재 판독값 가져오기”const getCurrentPressure = async () => { const reading = await Barometer.getCurrentReading(); console.log('Current pressure:', reading.pressure, 'hPa'); console.log('Relative altitude:', reading.relativeAltitude, 'm');};모니터링 중지
Section titled “모니터링 중지”const stopMonitoring = async () => { await Barometer.stop(); console.log('Barometer monitoring stopped');};완전한 예제
Section titled “완전한 예제”다음은 기압계 통합을 보여주는 완전한 예제입니다:
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();판독값 이해하기
Section titled “판독값 이해하기”- 헥토파스칼(hPa) 또는 밀리바(mb) 단위로 측정됩니다
- 표준 해수면 압력: 1013.25 hPa
- 고도가 높을수록 압력이 낮아집니다
- 기상 시스템이 압력 판독값에 영향을 미칩니다
- 압력 변화로부터 계산됩니다
- 시작 기준점을 기준으로 합니다
- 정확도: ±10-30미터
- 단기 추적에 가장 적합합니다
- 가용성 확인: 사용하기 전에 항상 센서 가용성을 확인하세요
- 합리적인 간격: 너무 자주 폴링하지 마세요(1000ms가 좋습니다)
- 리스너 제거: 필요하지 않을 때 리스너를 정리하세요
- 보정: 정확한 판독값을 위해 알려진 고도를 사용하세요
- 배터리 영향: 지속적인 센싱으로 배터리 사용량을 모니터링하세요
일반적인 문제
Section titled “일반적인 문제”센서를 사용할 수 없음
Section titled “센서를 사용할 수 없음”try { const { available } = await Barometer.isAvailable(); if (!available) { // Fallback to GPS altitude or manual input showAlternativeMethod(); }} catch (error) { console.error('Barometer check failed:', error);}노이즈가 있는 데이터
Section titled “노이즈가 있는 데이터”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; }}- 전체 메서드 문서는 API Reference를 확인하세요
- 고급 사용법은 예제 앱을 확인하세요
- 전체 구현 예제는 튜토리얼을 참조하세요