Friday 18 October 2013

List of Sensors Availble

Create new Android Project


Project Name: SensorsList

//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: SensorsList
Package Name: com.shaikhhamadali.blogspot.sensorslist
Create layout file: activity_sensors_list

1.create layout:

<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"
    tools:context=".SensorsList" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List Of All The Sensors in Your Device"
        android:textColor="#000000"
        android:textSize="30sp"
        android:textStyle="italic"
        android:typeface="sans" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_centerInParent="true" >
    </ListView>

</RelativeLayout>

2.code of activity:
package com.shaikhhamadali.blogspot.sensorslist;

import java.util.ArrayList;
import java.util.List;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SensorsList extends Activity {
 //variables
 private ListView listView;
 private SensorManager mSensorManager;
 //list of sensors
 private List<Sensor> deviceSensors = null;
 //list of sensors names
 private List<String> deviceSensorsList = new ArrayList<String>();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_sensors_list);
  //create instance of list view 
  listView = ((ListView) findViewById(R.id.listView1));
  //create instance of sensor manager and get system sensor service
  mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  //get list of all types of sensors in you device
  deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
  /*you can get specific sensors by selecting type in getSensorList(type you want);*/
  for(Sensor s: deviceSensors){
   //get names of all the sensors in your device and add into list
   deviceSensorsList.add(s.getName());
  }
  listView.setAdapter(new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, deviceSensorsList));
 }
}

3. note that:

4. conclusion:

  • Some information about how to get list of all/selected type sensors in device.
  • know what are Sensors and how to use Sensors.

5. About the post:

  • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!
6. Source Code:
        you can download the source code here

Cheers,
Hamad Ali Shaikh