How to Display the List of Sensors Present in an Android Device Programmatically? - GeeksforGeeks

23 Feb, 2021
All Android devices produced worldwide come with built-in sensors that measure motion, orientation, and various environmental conditions. These sensors generally facilitate Android architecture by providing the data from the sensor for various applications. For example, a temperature sensor provides the device’s temperature, information from which could be used for shutting down a few unrequired services. Such a sensor is a general type, but broadly, sensors are divided into three types:
Motion Sensors: Motion sensors measure the acceleration and rotational forces along the three axes x-y-z. Motion Sensors include accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.Environment Sensors: Environment sensors measure a variety of environmental parameters, such as pressure, ambient temperature (room temperature), illumination (light falling on the device), and humidity. They include barometers, photometers, and thermometers.Position Sensors: Position Sensors measure the physical position of a device in the space. They include orientation sensors and magnetometers.
Motion Sensors: Motion sensors measure the acceleration and rotational forces along the three axes x-y-z. Motion Sensors include accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.
Environment Sensors: Environment sensors measure a variety of environmental parameters, such as pressure, ambient temperature (room temperature), illumination (light falling on the device), and humidity. They include barometers, photometers, and thermometers.
Position Sensors: Position Sensors measure the physical position of a device in the space. They include orientation sensors and magnetometers.
In general, any Android Device on or above Android Version 4.4 (Kitkat) have these sensors present in them:
Accelerometer – Hardware Sensor – Motion SensorGravity Sensor – Program Based (Software) – Raw data derived from the Motion Sensors for Gravity calculation.Ambient Temperature – Hardware Sensor – Environment SensorGyroscope – Hardware Sensor – Motion SensorLight Sensor – Hardware Sensor – Environment SensorOrientation Sensor – Program Based (Software) – Raw data derived from the Position and Motion SensorsProximity Sensor – Hardware Sensor – Position Sensor
Accelerometer – Hardware Sensor – Motion Sensor
Gravity Sensor – Program Based (Software) – Raw data derived from the Motion Sensors for Gravity calculation.
Ambient Temperature – Hardware Sensor – Environment Sensor
Gyroscope – Hardware Sensor – Motion Sensor
Light Sensor – Hardware Sensor – Environment Sensor
Orientation Sensor – Program Based (Software) – Raw data derived from the Position and Motion Sensors
Proximity Sensor – Hardware Sensor – Position Sensor
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Go to the activity_main.xml file which represents the UI of the application, and create a TextView inside a ScrollView that shall list the sensors present in the device. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><ScrollView    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"    tools:context=".MainActivity">      <!--Text View that shall display the sensor information list-->    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true" />  </ScrollView>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Contextimport android.hardware.Sensorimport android.hardware.SensorManagerimport android.os.Bundleimport android.widget.TextViewimport androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {      // Information about Sensors present in the     // device is supplied by Sensor Manager of the device    private lateinit var sensorManager: SensorManager      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Initialize the variable sensorManager        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager          // getSensorList(Sensor.TYPE_ALL) lists all the sensors present in the device        val deviceSensors: List<Sensor> = sensorManager.getSensorList(Sensor.TYPE_ALL)          // Text View that shall display this list        val textView = findViewById<TextView>(R.id.tv)          // Converting List to String and displaying         // every sensor and its information on a new line        for (sensors in deviceSensors) {            textView.append(sensors.toString() + "\n\n")        }    }}
Android-Misc
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Content Providers in Android with Example
Android RecyclerView in Kotlin
Navigation Drawer in Android
Broadcast Receiver in Android With Example
Content Providers in Android with Example
Android UI Layouts
Android RecyclerView in Kotlin