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

วิธีตรวจสอบว่ามือถือ Android รองรับเซ็นเซอร์ Low Latency Off-body Detect หรือไม่

ภาพรวม

ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์ TYPE_LOW_LATENCY_OFFBODY_DETECT ซึ่งใช้ตรวจจับว่าอุปกรณ์ถูกลอยออกจากร่างกาย (Off-body detection) ด้วย Latency ต่ำ เหมาะสำหรับอุปกรณ์ Wearable เช่น Smartwatch

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

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

        // ตรวจสอบเซ็นเซอร์ Low Latency Off-body Detect
        Sensor offBodySensor = sensorManager.getDefaultSensor(Sensor.TYPE_LOW_LATENCY_OFFBODY_DETECT);
        
        if (offBodySensor != null) {
            actionEvent.setText("รองรับ Low Latency Off-body Detect");
            
            String info = String.format(
                "Name: %s\nVendor: %s\nVersion: %d\nMax Range: %.2f\nResolution: %.2f\nPower: %.2f mA",
                offBodySensor.getName(),
                offBodySensor.getVendor(),
                offBodySensor.getVersion(),
                offBodySensor.getMaximumRange(),
                offBodySensor.getResolution(),
                offBodySensor.getPower()
            );
            actionEvent.append("\n\n" + info);
        } else {
            actionEvent.setText("ไม่รองรับ Low Latency Off-body Detect\n(มักพบใน Wear OS / Smartwatch)");
        }
    }
}

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

คุณสมบัติรายละเอียด
API Levelเพิ่มใน API 26 (Android 8.0 Oreo)
ประเภทHardware/Composite sensor
หน่วยBoolean (0 = On-body, 1 = Off-body)
Latencyต่ำมาก (Low Latency) - ตอบสนองเร็ว
อุปกรณ์หลักWear OS, Smartwatch, Fitness tracker

การทำงานของ Off-body Detection

  • ใช้เซ็นเซอร์ PPG (Heart rate) + Accelerometer + Algorithms
  • ตรวจจับการสวมใส่/ถอดนาฬิกาออกจากข้อมือ
  • Latency ต่ำ = ตอบสนองภายในไม่กี่วินาที
  • ใช้ประหยัดแบตเตอรี่: ปิดหน้าจอ/เซ็นเซอร์อื่นๆ เมื่อ Off-body
  • ใช้เพื่อความปลอดภัย: Lock อุปกรณ์เมื่อถอดออก

ความแตกต่างกับเซ็นเซอร์ Off-body ธรรมดา

คุณสมบัติTYPE_OFF_BODY_DETECTTYPE_LOW_LATENCY_OFFBODY_DETECT
Latencyสูงกว่า (นาที)ต่ำมาก (วินาที)
ความแม่นยำสูงสูง (แต่เร็วกว่า)
การใช้พลังงานต่ำกว่าสูงกว่า (ต้อง sampling บ่อย)

ข้อควรระวัง

  • โทรศัพท์มือถือทั่วไป ไม่มี เซ็นเซอร์นี้
  • ต้องทดสอบบน Wear OS Emulator หรืออุปกรณ์จริง
  • เป็น System sensor - อาจต้องการ Permission เฉพาะ
  • สำหรับแอป Wear OS ควรใช้ WearableSensorManager หรือ Health Services API แทน