0% found this document useful (0 votes)
3 views5 pages

Android 31 (Sensors)

this is about android sensor and its uses. This also explains a lot about the different sensor that are used in android

Uploaded by

samjohn brave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Android 31 (Sensors)

this is about android sensor and its uses. This also explains a lot about the different sensor that are used in android

Uploaded by

samjohn brave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Sensors

Most of the android devices have built-in sensors that measure motion, orientation, and
various environmental condition. The android platform supports three broad categories of
sensors:

 Motion Sensors

which measure the force of acceleration and rotation along all the axes. It consists of
the accelerometers, gravity sensors, gyroscopes and rotational vector sensors.

 Environmental sensors

They measure the environmental conditions in which the device is currently present
such as temperature, pressure, humidity and illumination. It comprises of
barometers, photometers and thermometers.

 Position sensors

They measure the physical position of a device which includes orientation sensors
and magnetometers.
Whatever the sensor is, android allows us to get the raw data from these sensors and use it
in our application. For this android provides us with some classes. Android provides
SensorManager and Sensor classes to use the sensors in our application. In order to use
sensors , first thing we need to do is to instantiate the object of SensorManager class.Few
Sensor types supported by the Android platform:

Sensor Framework
 SensorManager
This class is used to create an instance of the sensor service. This class provides
various methods for accessing and listing sensors, registering and unregistering
sensor event listeners, and acquiring orientation information.

 Sensor
This is used class to create an instance of a specific sensor.

 SensorEvent
The system uses this class to create a sensor event object, which provides
information about a sensor event. A sensor event object includes the following
information: the raw sensor data, the type of sensor that generated the event, the
accuracy of the data, and the timestamp for the event.

 SensorEventListener
This interface is used to create two callback methods that receive notifications
(sensor events) when sensor values change or when sensor accuracy changes.

Use the following code to generate a list of all sensors.


public class MainActivity extends Activity implements SensorEventListener {
1. Sensor sensor;
SensorManager sm;
2. TextView tv;

3. @Override
protected void onCreate(Bundle savedInstanceState) {
4. super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
5. tv = (TextView) findViewById(R.id.textView1);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
6. sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
7.
@Override
8. protected void onResume() {
super.onResume();
9. sm.registerListener(this, sensor,
10 SensorManager.SENSOR_DELAY_NORMAL);
. }
11
. @Override
12 protected void onPause() {
. super.onPause();
13 sm.unregisterListener(this);
. }
14
. @Override
15 public void onAccuracyChanged(Sensor sensor, int accuracy) {
. // Do something here if sensor accuracy changes.
16 }
.
17 @Override
. public void onSensorChanged(SensorEvent event) {
18 float x = event.values[0];
. float y = event.values[1];
19 float z = event.values[2];
. String msg = String.format("\n x=%.2f\n y=%.2f\n z=%.2f",
20 x,y,z);
. tv.setText(msg);
21 }
.
22 }
.
23
.
24
.
25
.
26
.
27
.
28
.
29
.
30
.
31
.
32
.
33
.
34
.
35
.
36
.
37
.
38
.
39
.
40
.
41
.
42
.
43
.
44
.

The above example monitors the raw data provided by the


sensor TYPE_ACCELEROMETER (An acceleration sensor measures the acceleration applied to
the device, including the force of gravity). We instantiate the object of Sensor class by
calling the getDefaultSensor() method of the SensorManager class.
The accelerometer measures the acceleration acting on the device due to the force of
gravity in all the three axes and use it to detect motion.
Acceleration on the device = - gravitational force - force on the sensor/mass.
When the device is idle on the table, accelerometer reads 9.81m/s2 When the device is
falling freely, the accelerometer reads 0 The SensorEvent object for an accelerometer gives
3 values,
event.values[0] contains acceleration along x axis including gravity,
event.values[1] contains acceleration along y axis including gravity,
event.values[2] contains acceleration along z axis including gravity and the values are in
m/s2.
When the device is flat on the table in its natural orientation which is portrait for a phone
and landscape for a tablet, and if you push it towards the right, the x acceleration is positive.
When the device is flat on the table in its natural orientation and push it on the bottom so
that it moves away from you along the y axis, the y acceleration value is positive. When the
device is flat and you move it towards the sky, the z acceleration is +9.81.

Declaring Sensor Availability in the Manifest file


 If we however want the device to install our app only if it has a particular type of sensor,we
have to add the to our Manifest file so that only those users with the Sensor can get access to our
app.
 In the code snippet below, we indicate that any device that wants to install our app must have
an accelerometer.
 Use the android:required = "true" if you want the device to have the sensor.

Sensor Coordinate System


In general, the sensor framework uses a standard 3-axis coordinate system to express data values.
For most sensors, the coordinate system is defined relative to the device's screen when the device
is held in its default orientation( see in below figure). When a device is held in its default
orientation, the X axis is horizontal and points to the right, the Y axis is vertical and points up,
and the Z axis points toward the outside of the screen face. In this system, coordinates behind the
screen have negative Z values. This coordinate system is used by the following sensors:
acceleration sensor, gyroscope sensor,grevity sensor,geomagntic field sensor,linear accerleration
sensor.

You might also like