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

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

ภาพรวม

ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์ Step Counter (TYPE_STEP_COUNTER) ซึ่งนับจำนวนก้าวเดินสะสมตั้งแต่ Boot ล่าสุด ใช้สำหรับแอปสุขภาพ/ฟิตเนส

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

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

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

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

คุณสมบัติรายละเอียด
API Levelเพิ่มใน API 19 (KitKat)
ประเภทHardware Sensor (มีใน SoC/Hub)
หน่วยSteps (Integer - สะสม)
ค่าเริ่มต้น0 หลัง Boot แล้ว เพิ่มขึ้นเรื่อยๆ
อุปกรณ์ที่มีสมาร์ทโฟน Mid-range ขึ้นไป (มี Sensor Hub)

Step Counter vs Step Detector

คุณสมบัติTYPE_STEP_COUNTERTYPE_STEP_DETECTOR
ค่าที่ให้จำนวนก้าวสะสม (Cumulative)Event ต่อก้าว (1 event = 1 ก้าว)
LatencyReal-time (อ่านค่าได้ทุกเมื่อ)Real-time event
การใช้งานแสดงจำนวนก้าวรวมนับก้าว Real-time, Trigger action
ResetReset เฉพาะ RebootN/A (Event-based)

ตัวอย่างการอ่านค่า Step Counter แบบ Real-time

private SensorEventListener stepListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {
        int steps = (int) event.values[0];
        textView.setText("ก้าวเดิน: " + steps);
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};

// ลงทะเบียน
sensorManager.registerListener(stepListener, stepCounter, SensorManager.SENSOR_DELAY_NORMAL);

// ยกเลิกใน onPause/onStop
sensorManager.unregisterListener(stepListener);

Permission ที่จำเป็น

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<!-- API 29+ ต้องขอ Runtime Permission -->

แนวทางปัจจุบัน (Modern Approach)

แทนที่จะใช้ Sensor โดยตรง ควรใช้:

  • Google Fit API: อ่านข้อมูลก้าวจาก Google Fit (รวมหลายแหล่ง)
  • Health Services (Wear OS): สำหรับนาฬิกาอัจฉริยะ
  • Activity Recognition API: ตรวจจับกิจกรรม (เดิน, วิ่ง, ขับรถ)

ข้อควรระวัง

  • เซ็นเซอร์นี้เป็น Hardware-based ต้องมี Sensor Hub รองรับ
  • ค่าจะ Reset เมื่อ Reboot อุปกรณ์
  • อาจนับก้าวขณะขับรถ/นั่งรถ (False positives)
  • ต้องขอ Permission ACTIVITY_RECOGNITION (Dangerous permission API 29+)
  • บาง OEM มี Step Counter ของตัวเอง (Samsung Health, Mi Fit, etc.)