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

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

ภาพรวม

ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์ Light (TYPE_LIGHT) ซึ่งวัดความเข้มแสงแวดล้อม (Ambient Light) ใช้สำหรับ Auto-brightness และแอปที่ต้องปรับ UI ตามแสง

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

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

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

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

คุณสมบัติรายละเอียด
ConstantSensor.TYPE_LIGHT
หน่วยLux (ลักซ์) - SI unit ของ Illuminance
ช่วงค่า0 - 100,000+ lux (ขึ้นอยู่กับเซ็นเซอร์)
อุปกรณ์ที่มีสมาร์ทโฟนส่วนใหญ่ (Mid-range ขึ้นไป)

ค่า Lux อ้างอิง (Reference Values)

สถานการณ์ประมาณ Lux
แสงแดดกลางวัน10,000 - 100,000+
ในร่มวันที่มีแดด1,000 - 10,000
สำนักงาน/ห้องเรียน300 - 500
ห้องนั่งเล่นตอนเย็น50 - 150
ถนนมีไฟถนน10 - 50
ค่ำคืนไม่มีแสง< 1

ตัวอย่างการอ่านค่า Real-time

SensorEventListener lightListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {
        float lux = event.values[0];
        
        runOnUiThread(() -> {
            String level;
            if (lux > 10000) level = "กลางแจ้งแดดจัด";
            else if (lux > 1000) level = "ในร่มสว่าง";
            else if (lux > 300) level = "สำนักงาน";
            else if (lux > 50) level = "ห้องมืดน้อย";
            else level = "มืด";
            
            textView.setText(String.format("แสง: %.1f lux\n%s", lux, level));
            
            // ปรับความสว่างหน้าจอ (ต้องเป็น System app หรือใช้ WindowManager.LayoutParams)
            // WindowManager.LayoutParams params = getWindow().getAttributes();
            // params.screenBrightness = Math.min(1.0f, lux / 10000f);
            // getWindow().setAttributes(params);
        });
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};

sensorManager.registerListener(lightListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);

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

  • Auto Brightness: ระบบปรับความสว่างหน้าจออัตโนมัติ
  • Camera Apps: ปรับ Exposure, ISO, White balance
  • Reading Apps: เปลี่ยน Theme (Light/Dark mode) ตามแสง
  • Power Saving: ลดความสว่างในที่มืด ประหยัดแบต
  • AR/VR: ปรับแสง Virtual objects ให้เข้ากับสภาพแวดล้อมจริง

ข้อควรระวัง

  • เซ็นเซอร์อยู่ด้านหน้าจอ (ใกล้ Front camera/Proximity) - อาจถูกบังโดยนิ้ว/เคส
  • ค่าอาจ Noise/Fluctuate ต้องใช้ Moving Average หรือ Low-pass filter
  • Sampling rate ต่ำ (On-change หรือ ~1-5 Hz) ไม่ใช่ High-frequency
  • บางอุปกรณ์ Budget ไม่มีเซ็นเซอร์นี้
  • ใน Manifest: <uses-feature android:name="android.hardware.sensor.light" android:required="false" />
  • Android 10+ มี Sensor.TYPE_LIGHT เดิม แต่เพิ่ม Sensor.TYPE_LIGHT_SENSOR (alias)

คำสั่ง ADB ทดสอบ (Debug)

# ดูเซ็นเซอร์ทั้งหมด
adb shell dumpsys sensorservice

# ดูค่า Light sensor จริง
adb shell "while true; do dumpsys sensorservice | grep -A5 'Light Sensor'; sleep 1; done"