ภาพรวม
ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์อุณหภูมิรอบข้าง (Ambient Temperature) โดยใช้ Sensor.TYPE_AMBIENT_TEMPERATURE เซ็นเซอร์นี้วัดอุณหภูมิแวดล้อม ไม่ใช่อุณหภูมิร่างกายหรือ CPU
ขั้นตอนการสร้างโปรเจกต์
ขั้นตอนที่ 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);
// ตรวจสอบเซ็นเซอร์ Ambient Temperature
Sensor tempSensor = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (tempSensor != null) {
actionEvent.setText("รองรับ Ambient Temperature Sensor");
String info = String.format(
"Name: %s\nVendor: %s\nVersion: %d\n" +
"Max Range: %.2f °C\nResolution: %.2f °C\nPower: %.2f mA",
tempSensor.getName(),
tempSensor.getVendor(),
tempSensor.getVersion(),
tempSensor.getMaximumRange(),
tempSensor.getResolution(),
tempSensor.getPower()
);
actionEvent.append("\n\n" + info);
} else {
actionEvent.setText("ไม่รองรับ Ambient Temperature Sensor\n(โทรศัพท์ส่วนใหญ่ไม่มีเซ็นเซอร์นี้)");
}
}
}
ข้อมูลเซ็นเซอร์ TYPE_AMBIENT_TEMPERATURE
| คุณสมบัติ | รายละเอียด |
|---|---|
| Constant | Sensor.TYPE_AMBIENT_TEMPERATURE |
| API Level | เพิ่มใน API 14 (Android 4.0) |
| หน่วย | องศาเซลเซียส (°C) |
| วัด | อุณหภูมิแวดล้อม (Ambient air temperature) |
| อุปกรณ์ที่มี | นادرةมาก - Galaxy S4, Note 3, บางรุ่น Sony, รุ่น Rugged phones |
เซ็นเซอร์อุณหภูมิอื่นๆ ใน Android
| Sensor Type | คำอธิบาย | หน่วย |
|---|---|---|
| TYPE_AMBIENT_TEMPERATURE | อุณหภูมิแวดล้อม (อากาศรอบข้าง) | °C |
| TYPE_SKIN_TEMPERATURE | อุณหภูมิผิวหนัง (API 31+) | °C |
| TYPE_TEMPERATURE | Deprecated ใช้ AMBIENT_TEMPERATURE แทน | °C |
ตัวอย่างการอ่านค่าอุณหภูมิ
SensorEventListener tempListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float tempCelsius = event.values[0];
float tempFahrenheit = (tempCelsius * 9/5) + 32;
textView.setText(String.format("อุณหภูมิ: %.1f°C / %.1f°F", tempCelsius, tempFahrenheit));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
sensorManager.registerListener(tempListener, tempSensor, SensorManager.SENSOR_DELAY_NORMAL);
ข้อควรระวัง
- โทรศัพท์มือถือส่วนใหญ่ ไม่มี เซ็นเซอร์อุณหภูมิแวดล้อม
- เซ็นเซอร์ในโทรศัพท์ส่วนใหญ่วัดอุณหภูมิ CPU/Battery ไม่ใช่อากาศรอบข้าง
- อุปกรณ์ที่มี: Samsung Galaxy S4, Note 3, Sony Xperia Z series, Cat phones, บางรุ่น Rugged
- สำหรับแอปวัดอุณหภูมิร่างกาย ต้องใช้เซ็นเซอร์เฉพาะ (TYPE_SKIN_TEMPERATURE API 31+) หรืออุปกรณ์ภายนอก Bluetooth
- ใน Manifest:
<uses-feature android:name="android.hardware.sensor.ambient_temperature" android:required="false" />
ทางเลือกสำหรับแอปอุณหภูมิ
- ใช้ Weather API (OpenWeather, WeatherAPI) ดึงข้อมูลจากสถานีวัดอากาศใกล้เคียง
- ใช้ Bluetooth Thermometer อุปกรณ์ภายนอก
- ประมาณจาก Battery Temperature (ไม่แม่นยำสำหรับอากาศรอบข้าง)