Wednesday 27 March 2013

Sensors

A sensor (also called detector) is a converter that measures a physical quantity and converts
it into a signal which can be read by an observer or by an (today mostly electronic) instrument.

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions.

The Android sensor framework lets you access many types of sensors. Some of these sensors are hardware-based and some are software-based. Hardware-based sensors are physical components built into a handset or tablet device.

The Android platform supports three broad categories of sensors:
1.Motion Sensors
2.Environmental Senosors
3.Position Sensors

1.Motion Sensors:
These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.


2.Environmental Sensors:
These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers.


3.Position Sensors:
These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers.

Sensors Android supports several sensors via the SensorManager .

for example have look on following example using SensorManager we can get the list of available sensors from android device.

Getting list of available Sensors:
activity_main.xml:
<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" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </ScrollView>

</RelativeLayout>

MainActivity.java:
package com.ram.sensor1;

import java.util.List;

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

public class MainActivity extends Activity {
SensorManager sensorManager;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

display = (TextView) findViewById(R.id.textView1);

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

List<Sensor> deviceSensors = sensorManager
.getSensorList(Sensor.TYPE_ALL);

StringBuffer sb = new StringBuffer();
sb.append("avialable sensors are :" + "\n" + deviceSensors.size()
+ " sensors found :" + "\n");

for (Sensor sensor : deviceSensors) {
sb.append(sensor.getName() + "\n" + "\n");
}

display.setText(sb);

}

}















No comments:

Post a Comment