Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
How to Display the List of Sensors Present in an Android Device Programmatically?
Next article icon

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

Last Updated : 23 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. 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.
  2. 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.
  3. Position Sensors: Position Sensors measure the physical position of a device in the space. They include orientation sensors and magnetometers.

Generally Available Sensors in an Android Device

In general, any Android Device on or above Android Version 4.4 (Kitkat) have these sensors present in them:

  1. Accelerometer - Hardware Sensor - Motion Sensor
  2. Gravity Sensor - Program Based (Software) - Raw data derived from the Motion Sensors for Gravity calculation.
  3. Ambient Temperature - Hardware Sensor - Environment Sensor
  4. Gyroscope - Hardware Sensor - Motion Sensor
  5. Light Sensor - Hardware Sensor - Environment Sensor
  6. Orientation Sensor - Program Based (Software) - Raw data derived from the Position and Motion Sensors
  7. Proximity Sensor - Hardware Sensor - Position Sensor

Approach

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.Context import android.hardware.Sensor import android.hardware.SensorManager import android.os.Bundle import android.widget.TextView import 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")         }     } } 

Output: Run on Emulator


Next Article
How to Display the List of Sensors Present in an Android Device Programmatically?

A

aashaypawar
Improve
Article Tags :
  • Kotlin
  • Android
  • Android-Misc

Similar Reads

    How to Get the Device's IMEI and ESN Programmatically in Android?
    Many times while building Android Applications we require a unique identifier to identify the specific mobile users. For identifying that user we use a unique address or identity. For generating that unique identity we can use the android device id. In this article, we will take a look at How to get
    4 min read
    How to Find the Screen Resolution of a Device Programmatically in Android?
    Screen Resolution refers to the number of pixels on display. A higher resolution means more pixels and more pixels provide the ability to display more visual information. This entity is widely used in applications related to the broadcasting of real-time visuals such as live video, gaming, etc for o
    3 min read
    How to Get the MAC of an Android Device Programmatically?
    MAC stands for Media Access Control. The MAC address is also known as the Equipment Id Number. This MAC Address is provided by the Network Interface Card. In this article, we will see step by step from creating a new empty project to How to make an android app to display MAC Address using Java. Note
    2 min read
    How to Display Bluetooth Paired Devices Programmatically in Android?
    Bluetooth's technology is a high-speed, low-powered wireless technology link designed to connect devices such as phones or other portable equipment. It has a specification (IEEE 802.15.1) for low-power radio communications to link computers, phones, and other network devices over a short distance in
    5 min read
    How to Find Dots-Per-Inch (DPI) of Screen in Android Programmatically?
    Dots-Per-Inch or DPI is a measure of pixel density over the physical area on the screen. A pixel is the smallest unit of any screen display. and the sum of all the pixels present on the screen is termed as Screen Resolution. The pixels available to the user are called Viewport and in this article, w
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences