ad

Thursday, March 9, 2017

Proximity sensor in your smartphone - android programming

Proximity sensor android program


Have you tried to know which proximity sensor is available in your smartphone? Proximity sensor is one of the most important sensors available in the smartphone. You have perhaps noticed that when you receive a call and take the phone near your ear, the display goes off. Its all getting triggered from the sensor reading. There are so many applications in the smartphones making use of the same.

Let's code to find out the same. I am assuming that you have some basic knowledge of android programming, Java SDK etc and I am hoping you have android studio installed in your system.


Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sbordolo.closer.MainActivity">

    <TextView
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sensor_detail"/>
    <TextView
        android:layout_below="@+id/sensor_detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/heading"/>
    <TextView
        android:layout_below="@+id/heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/counter"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/counter"
        android:id="@+id/distance"/>

</RelativeLayout>



MainActivity.java

package com.sbordolo.closer;
import android.app.Activity;
import android.app.IntentService;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends Activity implements SensorEventListener {
    private SensorManager mSensorManager;
    private Sensor mSensor;
    ImageView iv;
    TextView heading;
    TextView counter;
    TextView distance;
    Integer integer;
    TextView sensor_detail;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

        //iv = (ImageView) findViewById(R.id.imageView1);
        sensor_detail = (TextView) findViewById(R.id.sensor_detail);

        heading = (TextView) findViewById(R.id.heading);
        counter = (TextView) findViewById(R.id.counter);
        distance = (TextView) findViewById(R.id.distance);

        sensor_detail.setText("Sensor Name: " + mSensor.getName() +"\n"+
                "Sensor Range: " + String.valueOf(mSensor.getMaximumRange()) +"\n");
        //distance.setText(String.valueOf(mSensor.getMaximumRange()));
        integer = 0;

    }

    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

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

    public void onSensorChanged(SensorEvent event) {
        heading.setText("Something is out there!");
        //integer = integer + 1;        //counter.setText("No. of objects coming closer: " +Integer.toString(integer));        distance.setText("Current value(it may be the maximum range in case of some sensors): "+Float.toString(event.values[0]));
        if (event.values[0] < 1.00f) {
            //iv.setImageResource(R.drawable.near);            heading.setText("Gotcha! Intruder");
            integer = integer + 1;
            counter.setText("No. of objects coming closer: " +Integer.toString(integer));
            distance.setText("Distance from the screen(It may be 0.0 in case of some sensors): "+Float.toString(event.values[0]));
        } else {
            heading.setText("Scanning.........");
        }
    }
}


Thats it. The same is available in google play store. Here is the link: 
https://play.google.com/store/apps/details?id=com.sbordolo.closer


Here is demo video of the same.


No comments:

Post a Comment

ad