Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีตรวจสอบว่ามือถือ Android รองรับเซ็นเซอร์ Pressure (ความดันบรรยากาศ) หรือไม่

ภาพรวม

ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์ Pressure (TYPE_PRESSURE) หรือ Barometer ซึ่งวัดความดันบรรยากาศ ใช้คำนวณความสูง (Altitude) และพยากรณ์อากาศ พบในสมาร์ทโฟน Mid-to-High-end

ขั้นตอนการสร้างโปรเจกต์

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/actionEvent"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:textSize="40sp"
        android:gravity="center" />

</LinearLayout>

ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)

package com.example.myapplication;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    SensorManager sensorManager;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView actionEvent = findViewById(R.id.actionEvent);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        // ตรวจสอบเซ็นเซอร์ Pressure (Barometer)
        Sensor pressureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
        
        if (pressureSensor != null) {
            actionEvent.setText("รองรับ Pressure Sensor (Barometer)");
            
            String info = String.format(
                "Name: %s\nVendor: %s\nVersion: %d\n" +
                "Max Range: %.2f hPa\nResolution: %.2f hPa\nPower: %.2f mA\nMin Delay: %d µs",
                pressureSensor.getName(),
                pressureSensor.getVendor(),
                pressureSensor.getVersion(),
                pressureSensor.getMaximumRange(),
                pressureSensor.getResolution(),
                pressureSensor.getPower(),
                pressureSensor.getMinDelay()
            );
            actionEvent.append("\n\n" + info);
        } else {
            actionEvent.setText("ไม่รองรับ Pressure Sensor\n(มักมีใน Flagship/Outdoor phones)");
        }
    }
}

ข้อมูลเซ็นเซอร์ TYPE_PRESSURE

คุณสมบัติรายละเอียด
ConstantSensor.TYPE_PRESSURE
API Levelเพิ่มใน API 9 (Android 2.3)
หน่วยHectopascal (hPa) = Millibar (mbar)
ช่วงค่าปกติ300 - 1100 hPa (ระดับน้ำทะเล ~1013.25 hPa)
อุปกรณ์ที่มีFlagship, Outdoor/Rugged phones, Smartwatches (Garmin, Suunto, Wear OS)

การคำนวณความสูง (Altitude) จากความดัน

// สูตร Barometric Formula (International Standard Atmosphere)
public float pressureToAltitude(float pressureHpa, float seaLevelPressureHpa) {
    // seaLevelPressureHpa = ความดันที่ระดับน้ำทะเลปัจจุบัน (จากสถานีอุตุนิยมวิทยา)
    // ค่าเริ่มต้น 1013.25 hPa
    return 44330.0f * (1.0f - (float) Math.pow(pressureHpa / seaLevelPressureHpa, 1.0f / 5.255f));
}

// ตัวอย่างการใช้งาน
SensorEventListener pressureListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {
        float pressure = event.values[0]; // hPa
        float altitude = pressureToAltitude(pressure, 1013.25f);
        
        runOnUiThread(() -> 
            textView.setText(String.format("ความดัน: %.2f hPa\nความสูง: %.1f ม.\n(อ้างอิง Sea Level 1013.25 hPa)", pressure, altitude))
        );
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};

การใช้งานจริง

  • Altimeter: นาฬิกานักเดินป่า, เครื่องบิน, โดรน
  • Weather Forecasting: ความดันลด = พายุ/ฝนเข้า | ความดันเพิ่ม = อากาศดี
  • Indoor Navigation: กำหนดชั้นอาคาร (Floor detection) ร่วมกับ WiFi/Bluetooth
  • Health/Fitness: วัดความสูงสะสมในการวิ่ง/ปั่นจักรยาน
  • Photography: คำนวณ Exposure compensation ที่ความสูงต่างๆ

ข้อควรระวัง

  • โทรศัพท์ Budget/Mid-range หลายรุ่น ไม่มี Barometer
  • ความแม่นยำ: ±1-3 hPa → ประมาณ ±8-25 เมตร (ขึ้นอยู่กับสภาพอากาศ)
  • ต้อง Calibrate ด้วย Sea Level Pressure ปัจจุบัน (QNH) จากสถานีอุตุนิยมวิทยาใกล้เคียง
  • อุณหภูมิและสภาพอากาศส่งผลต่อค่า Pressure มาก
  • ใน Manifest: <uses-feature android:name="android.hardware.sensor.barometer" android:required="false" />
  • บางอุปกรณ์มี Pressure sensor แต่ Expose ผ่าน TYPE_PRESSURE ไม่ได้ (OEM specific)

เซ็นเซอร์ที่เกี่ยวข้อง

Sensorหน่วยใช้ร่วมกับ Pressure
TYPE_RELATIVE_HUMIDITY%RHคำนวณ Heat Index, Dew Point
TYPE_AMBIENT_TEMPERATURE°Cปรับค่า Altitude ให้แม่นยำ
TYPE_GPS (Location)Lat/Lon/AltCalibrate Sea Level Pressure