Friday, February 18, 2011

Get the list of available sensors, SensorManager.getSensorList()

The class android.hardware.SensorManager provide a method getSensorList(). Use this method to get the list of available sensors of a certain type, or use Sensor.TYPE_ALL to get all the sensors.

Get the list of available sensors, SensorManager.getSensorList()

package com.exercise.AndroidSensorList;

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

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

public class AndroidSensorList extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SensorManager sensorManager
= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> listSensor
= sensorManager.getSensorList(Sensor.TYPE_ALL);

List<String> listSensorType = new ArrayList<String>();
for(int i=0; i<listSensor.size(); i++){
listSensorType.add(listSensor.get(i).getName());
}

setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listSensorType));
getListView().setTextFilterEnabled(true);
}
}


Download the files.

Related Article:
- Detect rotation around X, Y & Z axis, using SensorManager and SensorEventListener
- Detect Android Accelerometer sensor changed

No comments:

Post a Comment