1. sylsau's Avatar
    Hello,

    I create that thread to offer you a new tutorial aiming to learn you how to list all Sensors available on your Android device. You can discover the tutorial on my blog here : List all sensors available on an Android device but I put the complete content in that thread also.

    List all sensors available on an Android device

    Android devices have a lot of sensors inside. Android SDK put on the hands of developers all the necessary APIs to take advantages of them and offer to their users applications that use them. In this tutorial, you’re going to learn how to list all the sensors available on an Android device. In a future tutorial, we will see how to monitor their activity.

    Note that this tutorial is also available on Youtube in video :



    1/ List all sensors available

    Code:
    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> sensors sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
    Here, sensors list contains all the sensors available on the Android device.

    2/ Display all the sensors on a list

    To display all the sensors, we need to create a simple layout with a ListView :

    Code:
    <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=".MainActivity" >
     
        <ListView
          android:id="@+id/list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
        />
     
    </RelativeLayout>
    We must define also the layout for items :

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
     
      <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
     
    </LinearLayout>
    Then, we define an adapter implementation :

    Code:
    public class MySensorsAdapter extends ArrayAdapter<Sensor> {
     
      private int textViewResourceId;
     
      private static class ViewHolder {
        private TextView itemView;
      }
     
      public MySensorsAdapter(Context context, int textViewResourceId, List<Sensor> items) {
        super(context, textViewResourceId, items);
        this.textViewResourceId = textViewResourceId;
      }
     
      public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
     
        if (convertView == null) {
          convertView = LayoutInflater.from(this.getContext()).inflate(textViewResourceId, parent, false);
     
          viewHolder = new ViewHolder();
          viewHolder.itemView = (TextView) convertView.findViewById(R.id.content);
          convertView.setTag(viewHolder);
        } else {
          viewHolder = (ViewHolder) convertView.getTag();
        }
     
        Sensor item = getItem(position);
     
        if (item != null) {
          viewHolder.itemView.setText("Name : " + item.getName()
            + " / Int Type : " + item.getType()
            + " / String Type : "
            + sensorTypeToString(item.getType()) + " / Vendor : "
            + item.getVendor() + " / Version : "
            + item.getVersion() + " / Resolution : "
            + item.getResolution() + " / Power : "
            + item.getPower() + " mAh"
            + " / Maximum Range : " + item.getMaximumRange());
        }
     
        return convertView;
      }
    }
    Note that we use ViewHolder pattern. You can also see that we need a method to get sensor type name from sensor type integer value. This static method is called sensorTypeToString :

    Code:
    public static String sensorTypeToString(int sensorType) {
      switch (sensorType) {
      case Sensor.TYPE_ACCELEROMETER:
      return "Accelerometer";
      case Sensor.TYPE_AMBIENT_TEMPERATURE:
      case Sensor.TYPE_TEMPERATURE:
      return "Ambient Temperature";
      case Sensor.TYPE_GAME_ROTATION_VECTOR:
      return "Game Rotation Vector";
      case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
      return "Geomagnetic Rotation Vector";
      case Sensor.TYPE_GRAVITY:
      return "Gravity";
      case Sensor.TYPE_GYROSCOPE:
      return "Gyroscope";
      case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
      return "Gyroscope Uncalibrated";
      case Sensor.TYPE_HEART_RATE:
      return "Heart Rate Monitor";
      case Sensor.TYPE_LIGHT:
      return "Light";
      case Sensor.TYPE_LINEAR_ACCELERATION:
      return "Linear Acceleration";
      case Sensor.TYPE_MAGNETIC_FIELD:
      return "Magnetic Field";
      case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
      return "Magnetic Field Uncalibrated";
      case Sensor.TYPE_ORIENTATION:
      return "Orientation";
      case Sensor.TYPE_PRESSURE:
      return "Orientation";
      case Sensor.TYPE_PROXIMITY:
      return "Proximity";
      case Sensor.TYPE_RELATIVE_HUMIDITY:
      return "Relative Humidity";
      case Sensor.TYPE_ROTATION_VECTOR:
      return "Rotation Vector";
      case Sensor.TYPE_SIGNIFICANT_MOTION:
      return "Significant Motion";
      case Sensor.TYPE_STEP_COUNTER:
      return "Step Counter";
      case Sensor.TYPE_STEP_DETECTOR:
      return "Step Detector";
      default:
      return "Unknown";
    }
    3/ Run the sensors list application

    Before to run the sensors list application, we just need to add the following line to set the adapter to the listview instance in the onCreate method on an Activity for example :

    Code:
    list.setAdapter(new MySensorsAdapter(this, R.layout.row_item, sensors));
    That's all. Look at the demo video to see the application in action . In a next tutorial, we will learn how to monitor a specific sensors and get data associated.

    Don't hesitate to give me your feedbacks or ideas for future tutorials.

    Thanks.

    Sylvain
    08-29-2016 03:48 AM
  2. dpham00's Avatar
    Thanks for sharing
    08-29-2016 06:43 AM

Similar Threads

  1. How du I format SD card on a Huawei P9 lite
    By AC Question in forum Ask a Question
    Replies: 3
    Last Post: 02-22-2018, 02:28 AM
  2. [Free][App]USA Zip code on your Mobile 2.0
    By mz4Mobile in forum Android Apps
    Replies: 6
    Last Post: 12-18-2017, 01:34 PM
  3. Replies: 1
    Last Post: 08-30-2016, 01:24 AM
  4. Replies: 1
    Last Post: 08-29-2016, 05:52 AM
  5. Replies: 1
    Last Post: 08-29-2016, 04:33 AM

Tags for this Thread

LINK TO POST COPIED TO CLIPBOARD