ภาพรวม
ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์ Proximity (TYPE_PROXIMITY) ซึ่งตรวจจับวัตถุใกล้เคียง (มักใช้ปิดหน้าจอขณะโทรศัพท์) พบได้ในสมาร์ทโฟนเกือบทุกเครื่อง
ขั้นตอนการสร้างโปรเจกต์
ขั้นตอนที่ 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);
// ตรวจสอบเซ็นเซอร์ Proximity
Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (proximitySensor != null) {
actionEvent.setText("รองรับ Proximity Sensor (คลิกลาก)");
String info = String.format(
"Name: %s\nVendor: %s\nVersion: %d\n" +
"Max Range: %.2f cm\nResolution: %.2f cm\nPower: %.2f mA\nMin Delay: %d µs",
proximitySensor.getName(),
proximitySensor.getVendor(),
proximitySensor.getVersion(),
proximitySensor.getMaximumRange(),
proximitySensor.getResolution(),
proximitySensor.getPower(),
proximitySensor.getMinDelay()
);
actionEvent.append("\n\n" + info);
} else {
actionEvent.setText("ไม่รองรับ Proximity Sensor");
}
}
}
ข้อมูลเซ็นเซอร์ TYPE_PROXIMITY
| คุณสมบัติ | รายละเอียด |
|---|---|
| Constant | Sensor.TYPE_PROXIMITY |
| หน่วย | เซนติเมตร (cm) |
| ค่าที่ได้ | มักเป็น Binary: 0 = ใกล้ (Near), MaxRange = ไกล (Far) |
| Max Range | ปกติ 5.0 cm (ขึ้นอยู่กับฮาร์ดแวร์) |
| อุปกรณ์ที่มี | เกือบทุกสมาร์ทโฟน (ใกล้ Ear piece/Receiver) |
การทำงานของ Proximity Sensor
- ใช้ Infrared (IR) LED + Photodiode
- ปล่อยแสง IR → สะท้อนจากวัตถุ (หน้า/แก้ม) → รับสัญญาณกลับ
- ค่า 0.0 (หรือค่าต่ำมาก) = วัตถุใกล้ (Near) → ปิดหน้าจอ
- ค่า = Max Range (เช่น 5.0) = ไม่มีวัตถุใกล้ (Far) → เปิดหน้าจอ
- Sampling rate ต่ำ (On-change) ไม่ใช่ Continuous
ตัวอย่างการอ่านค่า Real-time
SensorEventListener proximityListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float distance = event.values[0];
float maxRange = event.sensor.getMaximumRange();
boolean isNear = distance < maxRange;
runOnUiThread(() -> {
textView.setText(String.format(
"ระยะ: %.1f cm / Max: %.1f cm\nสถานะ: %s",
distance, maxRange, isNear ? "ใกล้ (Near)" : "ไกล (Far)"
));
// ควบคุมหน้าจอ (ต้องการ Permission WRITE_SETTINGS หรือ System app)
// getWindow().getDecorView().setSystemUiVisibility(isNear ? View.SYSTEM_UI_FLAG_LOW_PROFILE : 0);
});
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
sensorManager.registerListener(proximityListener, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
การใช้งานจริง
- Call Screen Off: ปิดหน้าจอ/ปิด Touch ขณะรับ/โทร (ป้องกันกดผิด)
- Pocket Mode: ล็อคหน้าจอเมื่อใส่กระเป๋า
- Wave to Wake: กลิ้งมือผ่านเซ็นเซอร์เพื่อปลุกหน้าจอ
- Smart Cover/Flip Cover: ปิด/เปิดหน้าจอตามฝาครอบ (Hall sensor + Proximity)
ข้อควรระวัง
- ส่วนใหญ่ให้ค่า Binary (Near/Far) ไม่ใช่ค่า Per-cm ต่อเนื่อง
- Max Range มักเป็น 5cm แต่บางเครื่องอาจต่างไป
- เซ็นเซอร์อยู่บริเวณด้านบนหน้าจอ (ใกล้ Receiver/Front camera)
- อาจชนกับ Screen Protector ที่หนาหรือสีเข้ม
- ใน Manifest:
<uses-feature android:name="android.hardware.sensor.proximity" android:required="false" /> - บางอุปกรณ์ใช้ Virtual Proximity (คำนวณจาก Gyro/Accel) แทน Hardware
Proximity vs Other Sensors
| Sensor | ใช้สำหรับ | ค่าที่ได้ |
|---|---|---|
| Proximity | วัตถุใกล้หน้าจอ | 0 / MaxRange (Binary) |
| Light | ความสว่างแวดล้อม | Lux (ต่อเนื่อง) |
| Gravity | แรงโน้มถ่วง/ทิศทาง | m/s² (3 แกน) |