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

วิธีตรวจสอบว่ามือถือ Android รองรับเซ็นเซอร์ Humidity (ความชื้น) หรือไม่

ภาพรวม

ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์ Relative Humidity (TYPE_RELATIVE_HUMIDITY) ซึ่งวัดความชื้นสัมพัทธ์ของอากาศแวดล้อม เซ็นเซอร์นี้หายากมากในสมาร์ทโฟน

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

ขั้นตอนที่ 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);

        // ตรวจสอบเซ็นเซอร์ Humidity
        Sensor humiditySensor = sensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY);
        
        if (humiditySensor != null) {
            actionEvent.setText("รองรับ Humidity Sensor (ความชื้น)");
            
            String info = String.format(
                "Name: %s\nVendor: %s\nVersion: %d\n" +
                "Max Range: %.2f %%RH\nResolution: %.2f %%RH\nPower: %.2f mA",
                humiditySensor.getName(),
                humiditySensor.getVendor(),
                humiditySensor.getVersion(),
                humiditySensor.getMaximumRange(),
                humiditySensor.getResolution(),
                humiditySensor.getPower()
            );
            actionEvent.append("\n\n" + info);
        } else {
            actionEvent.setText("ไม่รองรับ Humidity Sensor\n(โทรศัพท์เกือบทุกเครื่องไม่มี)");
        }
    }
}

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

คุณสมบัติรายละเอียด
ConstantSensor.TYPE_RELATIVE_HUMIDITY
API Levelเพิ่มใน API 14 (Android 4.0)
หน่วยPercent Relative Humidity (%RH)
ช่วงค่า0% - 100% RH
อุปกรณ์ที่มี極稀少 - Galaxy S4, Note 3, 一部 Sony, Rugged phones เท่านั้น

ทำไมโทรศัพท์มือถือไม่มี Humidity Sensor?

  • ขนาด/ต้นทุน: เซ็นเซอร์ความชื้นใหญ่และแพงกว่าเซ็นเซอร์อื่นๆ
  • ตำแหน่งติดตั้ง: ต้องสัมผัสอากาศโดยตรง ไม่ได้อยู่ในเคสโลหะ
  • การป้องกันน้ำ: IP68 ทำให้ลูกไหล่ติดตั้งเซ็นเซอร์ยาก
  • ความต้องการ: ผู้ใช้ส่วนใหญ่ไม่ต้องการวัดความชื้นจากโทรศัพท์

ตัวอย่างการอ่านค่า (หากมีเซ็นเซอร์)

SensorEventListener humidityListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {
        float humidity = event.values[0]; // %RH
        
        runOnUiThread(() -> {
            String comfort;
            if (humidity < 30) comfort = "แห้งเกินไป";
            else if (humidity < 60) comfort = "สบาย (Comfort zone)";
            else if (humidity < 70) comfort = "ชื้น";
            else comfort = "ชื้นมาก/ไม่สบาย";
            
            textView.setText(String.format("ความชื้น: %.1f%% RH\n%s", humidity, comfort));
        });
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};

การประยุกต์ใช้ร่วมกับเซ็นเซอร์อื่นๆ

การคำนวณสูตร/คำอธิบายต้องการเซ็นเซอร์
Dew Point (จุดน้ำค้าง)Magnus formula จาก Temp + RHTemperature + Humidity
Heat Index (ดัชนีความร้อน)Feels-like temperatureTemperature + Humidity
Absolute Humidityg/m³ น้ำในอากาศTemperature + Humidity + Pressure
WBGT (Heat Stress)มาตรฐานความปลอดภัยงานกลางแจ้งTemp + Humidity + Globe Temp + Wind

ทางเลือกสำหรับแอปวัดความชื้น

  • Weather API: OpenWeatherMap, WeatherAPI, Dark Sky - ดึงข้อมูลจากสถานีวัดใกล้เคียง
  • Bluetooth Hygrometer: อุปกรณ์วัดความชื้นภายนอก (Govee, SensorPush, SwitchBot)
  • WiFi Sensors: ESP32/ESP8266 + DHT22/SHT3x สร้างเอง
  • Estimation: ประมาณจาก GPS Location + Weather Service

ข้อควรระวัง

  • โทรศัพท์มือถือสมัยใหม่ ไม่มี เซ็นเซอร์นี้เลย (แม้ Flagship)
  • Galaxy S4/Note 3 (2013) เป็นรุ่นสุดท้ายที่มีเซ็นเซอร์นี้ใน Mainstream
  • ใน Manifest: <uses-feature android:name="android.hardware.sensor.humidity" android:required="false" />
  • หากพัฒนาแอป IoT ให้ใช้ External sensor ผ่าน BLE/Bluetooth Classic