Detect and use light and gyroscope sensors in Android
Hello everyone!
Sensors play a vital role in any electronic device and more importantly help in detecting or measuring a physical property. Nowadays, Android phones come equipped with a range of sensors including light, pressure, temperature, gyroscope, proximity etc. Developers need to build applications using the functionality of these sensors. For that purpose, one needs to learn how to detect and implement the corresponding sensor functionality.
Through this post, we will understand how to detect and use the Light and Gyroscope sensors in Android.
Pre-requisites: Eclipse IDE, Android SDK
Step 1: Create a new Android application project called AndroidSensorsDemo with package name com.example
Step 2: Create Activity class
We will first create an Activity class called MySensorActivity in our existing package and add the following code!
MySensorActivity.java
package com.example;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
public class MySensorActivity extends Activity implements SensorEventListener {
private TextView tv;
private SensorManager mSensorManager;
private Sensor mGyroSensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView)findViewById(R.id.txt2);
// Get an instance of the sensor service
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mGyroSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
PackageManager PM= this.getPackageManager();
boolean gyro = PM.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE);
boolean light = PM.hasSystemFeature(PackageManager.FEATURE_SENSOR_LIGHT);
if(gyro){
if(light){
Toast.makeText(getApplicationContext(),"Both light and gyroscope sensors are present", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(),"Only gyroscope sensor is present", Toast.LENGTH_LONG).show();
}
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
float angularXSpeed = event.values[0];
tv.setText("Angular X speed level is: " + "" +angularXSpeed);
}
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mGyroSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// important to unregister the sensor when the activity pauses.
super.onPause();
mSensorManager.unregisterListener(this);
}
}
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"
tools:context=".MySensorActivity">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
<TextView
android:id="@+id/txt2"
android:layout_below="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Note: The above code displays values for a Gyroscope sensor. For using the light sensor, you just need to register the Light sensor instead of the Gyroscope sensor. The availability of sensors varies from device to device. Try and find out the sensors present in your device.
Finally, no changes are to be made to the AndroidManifest.xml file. Save and run the application on an Android device and you should the following output!
Reference: Android Sensors