기기에 설치된 센서종류 확인하기

이번 포스팅에서는 안드로이드 기기에 설치된 센서 종류를 확인하는 법에 대해 알아보도록 하겠습니다.

Android 플랫폼에서 지원하는 센서의 유형은 다음 와 같습니다.

Sensor Type Description Common Uses
TYPE_ACCELEROMETER Hardware Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity. Motion detection (shake, tilt, etc.).
TYPE_AMBIENT_TEMPERATURE Hardware Measures the ambient room temperature in degrees Celsius (°C). See note below. Monitoring air temperatures.
TYPE_GRAVITY Software or Hardware Measures the force of gravity in m/s2 that is applied to a device on all three physical axes (x, y, z). Motion detection (shake, tilt, etc.).
TYPE_GYROSCOPE Hardware Measures a device’s rate of rotation in rad/s around each of the three physical axes (x, y, and z). Rotation detection (spin, turn, etc.).
TYPE_LIGHT Hardware Measures the ambient light level (illumination) in lx. Controlling screen brightness.
TYPE_LINEAR_ACCELERATION Software or Hardware Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity. Monitoring acceleration along a single axis.
TYPE_MAGNETIC_FIELD Hardware Measures the ambient geomagnetic field for all three physical axes (x, y, z) in μT. Creating a compass.
TYPE_ORIENTATION Software Measures degrees of rotation that a device makes around all three physical axes (x, y, z). As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method. Determining device position.
TYPE_PRESSURE Hardware Measures the ambient air pressure in hPa or mbar. Monitoring air pressure changes.
TYPE_PROXIMITY Hardware Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person’s ear. Phone position during a call.
TYPE_RELATIVE_HUMIDITY Hardware Measures the relative ambient humidity in percent (%). Monitoring dewpoint, absolute, and relative humidity.
TYPE_ROTATION_VECTOR Software or Hardware Measures the orientation of a device by providing the three elements of the device’s rotation vector. Motion detection and rotation detection.
TYPE_TEMPERATURE Hardware Measures the temperature of the device in degrees Celsius (°C). This sensor implementation varies across devices and this sensor was replaced with the TYPE_AMBIENT_TEMPERATURE sensor in API Level 14 Monitoring temperatures.

센서도 많은 종류가 있는 것을 알 수 있습니다. 여기서는 기기에 장착된 센서를 확인하는 방법을 알아볼 겁니다.

우선 새로운 프로젝트를 만들고 센서 리스트를 표시할 화면을 작성합니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tv_sensor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

다음은 onCreate에서 센서 리스트를 획득하는 코드를 작성합니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class MainActivity : AppCompatActivity() {
    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }
    private val sensorManager by lazy {
        getSystemService(Context.SENSOR_SERVICE) as SensorManager
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        getSensorList()
    }

    private fun getSensorList() {
        val sensorList: List<Sensor> = sensorManager.getSensorList(Sensor.TYPE_ALL)
        var sensorResult = String()
        sensorList.forEachIndexed { index, sensor ->
            sensorResult += """
                $index : ${sensor.name}
                toString : $sensor
                
                
                """.trimIndent()
        }

        binding.tvSensor.text = sensorResult
    }
}

우선은 센서 서비스를 관할하는 SensorManager 인스턴스를 획득합니다. 그리고나면 getSensorList로 모든 센서 리스트를 가져온 뒤, 텍스트뷰에 이 리스트의 속성을 표시하면 됩니다.

Sensor의 get 속성은 여러가지가 있는데요, toString으로 모든 속성을 확인할 수 있습니다. 안드로이드 스튜디오에서 제공하는 에뮬레이터에는 다음과 같은 센서가 설치되어 있다고 하네요.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
[
   {
      "Sensor name=""Goldfish 3-axis Accelerometer",
      "vendor=""The Android Open Source Project",
      version=1,
      type=1,
      maxRange=39.300102,
      resolution=2.480159E-4,
      power=3.0,
      minDelay=10000
   },
   {
      "Sensor name=""Goldfish 3-axis Gyroscope",
      "vendor=""The Android Open Source Project",
      version=1,
      type=4,
      maxRange=16.46,
      resolution=0.001,
      power=3.0,
      minDelay=10000
   },
   {
      "Sensor name=""Goldfish 3-axis Magnetic field sensor",
      "vendor=""The Android Open Source Project",
      version=1,
      type=2,
      maxRange=2000.0,
      resolution=0.5,
      power=6.7,
      minDelay=10000
   },
   {
      "Sensor name=""Goldfish Orientation sensor",
      "vendor=""The Android Open Source Project",
      version=1,
      type=3,
      maxRange=360.0,
      resolution=1.0,
      power=9.7,
      minDelay=10000
   },
   {
      "Sensor name=""Goldfish Ambient Temperature sensor",
      "vendor=""The Android Open Source Project",
      version=1,
      type=13,
      maxRange=80.0,
      resolution=1.0,
      power=0.001,
      minDelay=0
   },
   {
      "Sensor name=""Goldfish Proximity sensor",
      "vendor=""The Android Open Source Project",
      version=1,
      type=8,
      maxRange=1.0,
      resolution=1.0,
      power=20.0,
      minDelay=0
   },
   {
      "Sensor name=""Goldfish Light sensor",
      "vendor=""The Android Open Source Project",
      version=1,
      type=5,
      maxRange=40000.0,
      resolution=1.0,
      power=20.0,
      minDelay=0
   },
   {
      "Sensor name=""Goldfish Pressure sensor",
      "vendor=""The Android Open Source Project",
      version=1,
      type=6,
      maxRange=800.0,
      resolution=1.0,
      power=20.0,
      minDelay=10000
   },
   {
      "Sensor name=""Goldfish Humidity sensor",
      "vendor=""The Android Open Source Project",
      version=1,
      type=12,
      maxRange=100.0,
      resolution=1.0,
      power=20.0,
      minDelay=0
   },
   {
      "Sensor name=""Goldfish 3-axis Magnetic field sensor (uncalibrated)",
      "vendor=""The Android Open Source Project",
      version=1,
      type=14,
      maxRange=2000.0,
      resolution=0.5,
      power=6.7,
      minDelay=10000
   },
   {
      "Sensor name=""Game Rotation Vector Sensor",
      "vendor=""AOSP",
      version=3,
      type=15,
      maxRange=1.0,
      resolution=5.9604645E-8,
      power=12.7,
      minDelay=10000
   },
   {
      "Sensor name=""GeoMag Rotation Vector Sensor",
      "vendor=""AOSP",
      version=3,
      type=20,
      maxRange=1.0,
      resolution=5.9604645E-8,
      power=12.7,
      minDelay=10000
   },
   {
      "Sensor name=""Gravity Sensor",
      "vendor=""AOSP",
      version=3,
      type=9,
      maxRange=19.6133,
      resolution=2.480159E-4,
      power=12.7,
      minDelay=10000
   },
   {
      "Sensor name=""Linear Acceleration Sensor",
      "vendor=""AOSP",
      version=3,
      type=10,
      maxRange=19.6133,
      resolution=2.480159E-4,
      power=12.7,
      minDelay=10000
   },
   {
      "Sensor name=""Rotation Vector Sensor",
      "vendor=""AOSP",
      version=3,
      type=11,
      maxRange=1.0,
      resolution=5.9604645E-8,
      power=12.7,
      minDelay=10000
   },
   {
      "Sensor name=""Orientation Sensor",
      "vendor=""AOSP",
      version=1,
      type=3,
      maxRange=360.0,
      resolution=0.00390625,
      power=12.7,
      minDelay=10000
   }
]

이렇게해서 안드로이드 기기에 설치된 센서 리스트를 확인하는 법에 대해 알아보았습니다.

Licensed under CC BY 4.0
Built with Hugo
Theme Stack designed by Jimmy