본문 바로가기
PROGRAMMING CODE/ANDROID STUDIO

[Android Studio] 내장 센서 확인

by daye_ 2021. 11. 30.

기기의 사용 가능한 센서 확인하는 코드

센서 코드 짜기전에 무조건 확인하는것이 좋음,,

 

 

 

 

 

MainActivity.java

package com.example.mysensortest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private SensorManager mSensorManager;
    TextView tv_temperature;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StringBuffer sb = new StringBuffer("");


        tv_temperature = findViewById(R.id.tv_temperature);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
            sb.append("Sensor.TYPE_ACCELEROMETER\n");
        } else { // Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE) != null) {
            sb.append("Sensor.TYPE_AMBIENT_TEMPERATURE\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null) {
            sb.append("Sensor.TYPE_GRAVITY\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null) {
            sb.append("Sensor.TYPE_GYROSCOPE\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) != null) {
            sb.append("Sensor.TYPE_LIGHT\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION) != null) {
            sb.append("Sensor.TYPE_LINEAR_ACCELERATION\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null) {
            sb.append("Sensor.TYPE_MAGNETIC_FIELD\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION) != null) {
            sb.append("Sensor.TYPE_ORIENTATION\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) != null) {
            sb.append("Sensor.TYPE_PRESSURE\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY) != null) {
            sb.append("Sensor.TYPE_PROXIMITY\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY) != null) {
            sb.append("Sensor.TYPE_RELATIVE_HUMIDITY\n");
        } else {// Failure! No magnetometer.
        }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) != null) {
            sb.append("Sensor.TYPE_ROTATION_VECTOR\n");
        } else {// Failure! No magnetometer.
             }
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE) != null) {
            sb.append("Sensor.TYPE_TEMPERATURE\n");
        } else {// Failure! No magnetometer.
        }
        tv_temperature.setText(sb.toString());
    }
}