Practical No 22

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Mobile Application Development (22617) Practical No.

22

Practical No. 22: Develop a program to implement sensors.


I. Practical Significance
Most of the android devices have built-in sensors that measure motion, orientation, and
various environmental condition. Android allows us to get the raw data from these sensors
and use it in our application.

II. Relevant Program Outcomes (POs)


PO2- Discipline knowledge
PO3- Experiments and practice
PO4- Engineering tools
PO5- The engineer and society
PO7- Ethics

III. Competency and Practical Skills


“Create simple Android applications.”
This practical is expected to develop the following skills
1. Able to get a list of sensors supported by the device.
2. Able to demonstrate the use of Sensor Manager class.

IV. Relevant Course Outcome(s)


1. Interpret features of Android operating system.
2. Configure Android environment and development tools.

V. Practical Outcome (PrOs)


Develop a program to implement sensors.

VI. Relevant Affective Domain Related Outcome(s)


1. Work collaboratively in team
2. Follow ethical Practices.

VII. Minimum Theoretical Background


The android platform supports three broad categories of sensors. Motion Sensors,
Environmental sensors, Position sensors. Some of the sensors are hardware based and some
are software-based sensors. 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 Sensor Manager and Sensor classes to use the sensors in our
application. In order to use sensors, first thing you need to do is to instantiate the object of
SensorManager class. Example: SensorManager sMgr;
sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);

The next thing you need to do is to instantiate the object of Sensor class by calling the
getDefaultSensor() method of the SensorManager class. Its syntax is given below: Sensor light;
light = sMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
Once that sensor is declared, you need to register its listener and override two methods
which are onAccuracyChanged and onSensorChanged. Its syntax is as follows:

Maharashtra State Board of Technical Education 1


Mobile Application Development (22617) Practical No. 22

sMgr.registerListener(this, light,SensorManager.SENSOR_DELAY_NORMAL); public void


onAccuracyChanged(Sensor sensor, int accuracy)
{}
public void onSensorChanged(SensorEvent event) { }

Methods:
1. getDefaultSensor(int type) :- This method get the default sensor for a given type. Explain
methods
2. getOrientation(float[] R, float[] values) :- This method returns a description of the current
primary clip on the clipboard but not a copy of its data.
3. getInclination(float[] I) :- This method computes the geomagnetic inclination angle in
radians from the inclination matrix.
4. registerListener(SensorListener listener, int sensors, int rate) :-This method registers a
listener for the sensor
5. unregisterListener(SensorEventListener listener, Sensor sensor) :-This method unregisters
a listener for the sensors with which it is registered.
6. getOrientation(float[] R, float[] values) :-This method computes the device's orientation
based on the rotation matrix.
7. getAltitude(float p0, float p) :-This method computes the Altitude in meters from the
atmospheric pressure and the pressure at sea-level.

VIII. Resources required (Additional)

Sr. Instrument /Object Specification Quantity Remarks


No.
Android enabled 2 GB RAM 1 Data cable is
1 smartphone / Android mandatory for
version supporting emulator emulators

IX. Practical related Questions


Note: Below given are few sample questions for reference. Teachers must design more
such questions to ensure the achievement of identified CO.
1. List the best practices for accessing and using sensors.
2. Differentiate between Sensor Class and Sensor Manager Class.

(Space for answers)


1.
• Only gather sensor data in the foreground: Due to some restrictions, it's best to detect
sensor events either when your app is in the foreground or as part of a foreground
service.
• Unregister sensor listeners: Be sure to unregister a sensor's listener when you are
done using the sensor or when the sensor activity pauses.
• Test with the Android Emulator: The Android Emulator includes a set of virtual sensor
controls that allow you to test sensors such as accelerometer, ambient temperature,
magnetometer, proximity, light, and more.

Maharashtra State Board of Technical Education 2


Mobile Application Development (22617) Practical No. 22

2.
SensorManager: This is used to get access to various sensors present in the device to use it
according to need.
Sensor: This class is used to create an instance of a specific sensor.

X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to changes the background color when device is shuffled.
2. Write a program to display the list of sensors supported by the mobile device.

(Space for answers)


1.
MainActivity.java

package com.jamiapolytechnic.experiment221;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;
import android.hardware.Sensor;
import java.util.List;

public class MainActivity extends Activity {


SensorManager sm = null;
TextView txtView1 = null;
List list;
SensorEventListener sel = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
int v0 = (int) values[0];

Maharashtra State Board of Technical Education 3


Mobile Application Development (22617) Practical No. 22

int v1 = (int) values[1];


int v2 = (int) values[2];
txtView1.setText("Move device to change background color");
int v3 = (v0*100)%255;
int v4 = (v1*100)%255;
int v5 = (v2*100)%255;
txtView1.setBackgroundColor(Color.rgb(v3,v4,v5));
txtView1.setTextColor(Color.rgb(255-v3,255-v4,255-v5));
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Get a SensorManager instance */
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
txtView1 = (TextView)findViewById(R.id.txtView1);
txtView1.setTextSize(20);
list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(list.size()>0){
sm.registerListener(sel, (Sensor) list.get(0), SensorManager.SENSOR_DELAY_NORMAL);
}else{
Toast.makeText(getBaseContext(), "Error: No Accelerometer.",
Toast.LENGTH_LONG).show();
}
}

@Override
protected void onStop() {
if(list.size()>0){
sm.unregisterListener(sel);
}
super.onStop();
}

Maharashtra State Board of Technical Education 4


Mobile Application Development (22617) Practical No. 22

//=====================================================================
//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center" />

</androidx.constraintlayout.widget.ConstraintLayout>

//=====================================================================
2.
MainActivity.java

package com.jamiapolytechnic.experiment222;

import androidx.appcompat.app.AppCompatActivity;

Maharashtra State Board of Technical Education 5


Mobile Application Development (22617) Practical No. 22

import android.os.Bundle;

import android.os.Bundle; import android.content.Context; import android.hardware.Sensor;


import android.hardware.SensorManager; import android.view.View; import
android.widget.TextView; import java.util.List;
public class MainActivity extends AppCompatActivity {
private SensorManager mgr;
private TextView txtList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mgr = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
txtList = (TextView)findViewById(R.id.sensorslist);
List<Sensor> sensorList = mgr.getSensorList(Sensor.TYPE_ALL);
StringBuilder strBuilder = new StringBuilder();
for(Sensor s: sensorList){
strBuilder.append(s.getName()+"\n");
}
txtList.setVisibility(View.VISIBLE); txtList.setText(strBuilder);
}
}

//=====================================================================
//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="@+id/sensorslist"

Maharashtra State Board of Technical Education 6


Mobile Application Development (22617) Practical No. 22

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Sensors"
android:textSize="20dp"
android:textStyle="bold"
android:layout_gravity="center"
android:visibility="gone"/>
</LinearLayout>

Maharashtra State Board of Technical Education 7


Mobile Application Development (22617) Practical No. 22

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

XII. Assessment Scheme

Performance indicators Weightage

Process related (10 Marks) 30%

1. Logic Formation 10%


2. Debugging ability 15%
3. Follow ethical practices 5%
Product related (15 Marks) 70%

4. Interactive GUI 20%


5. Answer to Practical related questions 20%
6. Expected Output 20%
7. Timely Submission 10%
Total (25 Marks) 100%

List of student Team Members


1
2
3
4

Dated signature of
Marks Obtained
Teacher
Process Product Total
Related(10) Related(15) (25)

Maharashtra State Board of Technical Education 8

You might also like