ภาพรวม
ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์ Gravity ซึ่งเป็น Software/Virtual Sensor ที่คำนวณจาก Accelerometer (และบางครั้ง Gyroscope) เพื่อแยกแรงโน้มถ่วงออกจาก Linear Acceleration
ขั้นตอนการสร้างโปรเจกต์
ขั้นตอนที่ 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);
// ตรวจสอบเซ็นเซอร์ Gravity (Virtual Sensor)
Sensor gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
if (gravitySensor != null) {
actionEvent.setText("รองรับ Gravity Sensor (Virtual)");
String info = String.format(
"Name: %s\nVendor: %s\nVersion: %d\n" +
"Max Range: %.2f m/s²\nResolution: %.2f m/s²\nPower: %.2f mA",
gravitySensor.getName(),
gravitySensor.getVendor(),
gravitySensor.getVersion(),
gravitySensor.getMaximumRange(),
gravitySensor.getResolution(),
gravitySensor.getPower()
);
actionEvent.append("\n\n" + info);
} else {
actionEvent.setText("ไม่รองรับ Gravity Sensor\n(ต้องมี Accelerometer + Sensor Fusion)");
}
}
}
ข้อมูลเซ็นเซอร์ TYPE_GRAVITY
| คุณสมบัติ | รายละเอียด |
|---|---|
| ประเภท | Virtual/Software Sensor (คำนวณจาก Sensor Fusion) |
| หน่วย | m/s² (เหมือน Accelerometer) |
| แกน | 3 แกน (X, Y, Z) |
| ค่าปกติ | ~9.81 m/s² ลงทิศทาง Z (เมื่อวางแบน) |
| ขึ้นอยู่กับ | Accelerometer (+ Gyroscope เพื่อความแม่นยำ) |
ความสัมพันธ์: Accelerometer = Gravity + Linear Acceleration
// สมการพื้นฐาน Accelerometer = Gravity + Linear Acceleration // ดังนั้น Linear Acceleration = Accelerometer - Gravity Gravity = Accelerometer - Linear Acceleration
การใช้งานจริง
- Tilt Detection: ตรวจจับการเอียงของอุปกรณ์ (Portrait/Landscape)
- Step Counter/Pedometer: แยกการเคลื่อนที่ของร่างกายจากแรงโน้มถ่วง
- Gesture Recognition: Tap, Shake, Flip to mute
- AR/VR: การติดตามทิศทาง (Orientation tracking)
- Screen Rotation: Auto-rotate ที่ฉลาดกว่า
ข้อควรระวัง
- เป็น Virtual Sensor - ไม่มีฮาร์ดแวร์เฉพาะตัว ต้องคำนวณจาก Accelerometer (+ Gyro)
- อุปกรณ์ที่มีแต่ Accelerometer (ไม่มี Gyro) อาจมี Gravity Sensor แต่ความแม่นยำต่ำกว่า (ใช้ Low-pass filter เท่านั้น)
- มี Delay/Leeway ประมาณ 100-200ms จากการ Filter
- ค่า Z-axis = ~9.81 เมื่อวางแบนหน้าจอขึ้น | ค่า X,Y = 0
ตัวอย่างการอ่านค่า
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float gx = event.values[0];
float gy = event.values[1];
float gz = event.values[2];
// คำนวณมุมเอียง (Tilt)
double pitch = Math.atan2(gx, Math.sqrt(gy*gy + gz*gz));
double roll = Math.atan2(gy, Math.sqrt(gx*gx + gz*gz));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}, gravitySensor, SensorManager.SENSOR_DELAY_UI);