ad

Showing posts with label android programming. Show all posts
Showing posts with label android programming. Show all posts

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.


Thursday, February 23, 2017

Bit and pieces of android app development part 2 - DIY woodworking India

Bit and pieces of android app development part 2 - DIY woodworking India


Hey all! Welcome. Hope you have gone through the first part of bit and pieces of android app development. If not, here is the link. That part was kind of introductory. It was more about how to solve an existing problem with the means you already have! technically. After all, it is more about passion and motivation. If you really want to solve a real problem with your heart and soul, I know you will do it. For that reason, anything coming your way - may it be a new learning or a skill set, you will surely do it. Learning android app development is just an another dot in that context, but the diameter of that dot is huge.

Continuing the discussion from our last blog, let us put a high level use case of our application.




Pictures can convey the message more clearly. The app user 1 has encountered the pothole and the app has written the coordinates and timestamp into the local sqlite data base of the android phone. Time to time, the app will upload the data to the server. Lets assume, it has already uploaded the data. App user 2 is approaching the same pothole, but he will see in his app that there is a pothole nearby. The app will send him a cautious message in terms of a tone or an alarm. Does it sound good?

Lets draw a high level design of our application. It is little more technical. But I will explain it in simple language.



This is a flow chart of the application we are going to build.

START - implies the application has started.
LISTEN TO THE ACCELEROMETER - implies that inbuilt accelerometer of the android phone is reading the acceleration value in X,Y and Z direction.
VALUE > 15 - implies that acceleration in either direction is more than 15.
RECORD GEOGRAPHICAL COORDINATES INTO DB - implies that phone's sqlite DB table is updated with the coordinates.
SYNC DB TO CLOUD SERVER - implies that phone's DB is uploaded to cloud server and data are synced.

Y and N means Yes and No respectively. Below picture depicts multiple users using the app syncing their own DB with cloud.


So far so good. I hope the design is pretty clear now. Let me give you some more detail. Two more diagrams are listed below.



No sooner than I am writing this blog, I have completed the application. Let me give you a screenshot and the play store link.


Here is the link to the app.

https://play.google.com/store/apps/details?id=com.sbordolo.potholefinder

You can install the app and try it out. If you find any difficulty, please comment.

Till this point, it has been only requirements and designs. You may be looking for the coding, of course. That will follow shortly in subsequent pages. Stay tuned.

Thanks for visiting DIY woodworking India.



Friday, February 17, 2017

Bit and pieces of android app development - DIY woodworking India

Bit and pieces of android app development - DIY woodworking India


Being in the tech world as a network engineer for almost a decade, I have always tried to keep myself up to date with the evolving technologies. It often eats up my free time. But believe me, its really good. There are so many things in there, unless you try it, you won't realise it. One of those things which I have tried is android programming.

As smartphones have become more and more popular, more and more people are connected via internet. Information transfer has become lightening fast. Everything is at your finger tip. To alleviate my learnings, I have started exploring android application development. I had an end goal of course. I was residing in an area where the road was full of potholes and there was no street light at all. Often, my back used to take the heat at night time. I was thinking what I can do to address that problem or perhaps solve it if possible. If there were 1 or 2 potholes, that would have been practical. But there were hundreds! I started raising complaints to fill those with the town planning authority. As expected, nothing happened as usual. Then I thought how about avoiding those potholes by putting sign boards! Too many it will be. But how about plotting the location of potholes on a Google map and alerting the user if he/she is about to hit it? Thats a good idea!

Therefore, my research started and I started gathering information about what all things I need to gather information about potholes. Either I have to inspect a pothole and plot the map manually or I have to  make everything automatic. Of course latter was better.

To detect a pothole, we need a sensor, an accelerometer. What is an accelerometer? It is a device which can give you acceleration value in XYZ direction. Just to mention, acceleration means rate of change of velocity. Its a vector entity. How we can use it for pothole detection? Whenever we hit a pothole, we fall vertically down. It may be very little but still there is a fall vertically. Therefore, accelerometer shows increase in acceleration value in vertical direction. That is the clue. Once you get values or reading like that, capture the geo location or latitude/longitude. Logically these are the two things we will be needing.

Now where I can find an accelerometer?
Its available in most of the android smartphones! Just google it, you will find related android API probably from android SDK 6 itself and accelerometer is present from that moment on.

Once we have the inbuilt accelerometer, we must know how to get the readings. There comes the android APIs, details about which we can find in google developer site. Lets stop here today. Next blog will be more about the entities in android application development. Till then, bye.




ad