Android Sensor list 종류

2016. 6. 27. 20:09Mobile/Android



반응형

 Android Sensor list 종류


단말기마다 센서의 종류가 다르다. 저가 핸드폰의 경우 그 비용 절감을 하기 위해 센서의 몇 가지가 빠지는 부분이 있다.


대표적인 예로 GPS가 빠지는 기기도 그런 예이다.


예제를 보고 기기의 센서들 리스트를 뽑아보도록 하자.


MainActivity.java


package com.example.testsensorlist;


import android.hardware.Sensor;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.TextView;


import java.util.HashMap;

import java.util.List;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        TextView tvSensor = (TextView) findViewById(R.id.tvSensor);


        SensorManager sm = (SensorManager) this.getSystemService(SENSOR_SERVICE);

        List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);

        StringBuilder msg = new StringBuilder();

        msg.append("센서들:\n");


        for (Sensor sensor : sensors) {

            msg.append(sensor.getName() + "\n");

            msg.append(" Type: " + sensorTypes.get(sensor.getType()) + "\n");

            msg.append(" Vendor: " + sensor.getVendor() + "\n");

            msg.append(" Version: " + sensor.getVersion() + "\n");

            msg.append(" Resolution: " + sensor.getResolution() + "\n");

            msg.append(" MaximumRange: " + sensor.getMaximumRange() + "\n");

            msg.append(" Power: " + sensor.getPower() + "\n\n");

        }

        tvSensor.setText(msg);

    }

    //참고 :  https://developer.android.com/guide/topics/sensors/sensors_overview.html

    private HashMap<Integer, String> sensorTypes = new HashMap<Integer, String>();

    {

        sensorTypes.put(Sensor.TYPE_ACCELEROMETER, "TYPE_ACCELEROMETER"); // 1

        sensorTypes.put(Sensor.TYPE_MAGNETIC_FIELD, "TYPE_MAGNETIC_FIELD"); // 2

        sensorTypes.put(Sensor.TYPE_ORIENTATION, "TYPE_ORIENTATION"); // 3

        sensorTypes.put(Sensor.TYPE_GYROSCOPE, "TYPE_GYROSCOPE"); // 4

        sensorTypes.put(Sensor.TYPE_LIGHT, "TYPE_LIGHT"); // 5

        sensorTypes.put(Sensor.TYPE_PRESSURE, "TYPE_PRESSURE"); // 6

        sensorTypes.put(Sensor.TYPE_TEMPERATURE, "TYPE_TEMPERATURE"); // 7

        sensorTypes.put(Sensor.TYPE_PROXIMITY, "TYPE_PROXIMITY"); // 8

        sensorTypes.put(Sensor.TYPE_GRAVITY, "TYPE_GRAVITY"); // 9

        sensorTypes.put(Sensor.TYPE_LINEAR_ACCELERATION, "TYPE_LINEAR_ACCELERATION"); // 10

        sensorTypes.put(Sensor.TYPE_ROTATION_VECTOR, "TYPE_ROTATION_VECTOR"); // 11

    }

}




activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.youngjun.testsensorlist.MainActivity">


    <TextView

        android:id="@+id/tvSensor"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text=""/>

</RelativeLayout>



반응형