Wednesday, January 2, 2019

Sensors

Sensors Overview
The Android platform supports three broad categories of sensors:
Motion sensors
These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.
Environmental sensors
These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers.
•Position sensors
These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers.
You can access sensors available on the device and acquire raw sensor data by using the Android sensor framework.
What can we do?
• Determine which sensors are available on a device.
• Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power requirements, and resolution.
• Acquire raw sensor data and define the minimum rate at which you acquire sensor data.
• Register and unregister sensor event listeners that monitor sensor changes.
Some sensors are hardware based and some are software based. Hardware based are typically the physical components built into the handset. Software based sensors derive their information from one or more hardware based sensors and so they mimic the hardware based sensors and seem to be similar. So they are also called as virtual/synthetic sensors. 
Package? android.hardware package
// => to get Sensor Manager
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// => To get List of all sensors
List<Sensor> lstSensors = sm.getSensorList(Sensor.TYPE_ALL);
// => To find a particular sensor
if (sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
  // Success! There's a magnetometer.
  }
else {
  // Failure! No magnetometer.
  }
// => To use a particular sensor, call public methods like:
s.getResolution(), s.getMaximumRange(), s.getPower()





Code

package net.samaysoftware.bletest;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Date;
import java.util.List;

public class SensorActivity extends AppCompatActivity implements SensorEventListener {

    TextView tvSensorActivity;
    LinearLayout llsensor;
    SensorManager sm;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        tvSensorActivity = (TextView) findViewById(R.id.tvsensor1);
        llsensor = (LinearLayout) findViewById(R.id.llSensor);
        sm = (SensorManager) getSystemService(this.SENSOR_SERVICE);
        Sensor s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this,s,SensorManager.SENSOR_DELAY_NORMAL);
        llsensor.setBackgroundColor(Color.parseColor("#ffffff"));


    }

    long lasttime;

    @Override    public void onSensorChanged(SensorEvent sensorEvent) {
        float[] arr = sensorEvent.values;
        long currenttime = new Date().getTime();
        if(currenttime -  lasttime >200){

            float ans = (arr[0]*arr[0]+arr[1]*arr[1]+arr[2]*arr[2])/ (SensorManager.GRAVITY_EARTH*SensorManager.GRAVITY_EARTH);
            if(ans>3){

                llsensor.setBackgroundColor(Color.parseColor("#ff0000"));
            }
            else{

                llsensor.setBackgroundColor(Color.parseColor("#ffffff"));
            }

            lasttime = currenttime;
        }



    }

    @Override    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override    protected void onDestroy() {
        super.onDestroy();
        sm.unregisterListener(this);
    }
}



        /*List<Sensor> sensorList = sm.getSensorList(Sensor.TYPE_ALL);        for (Sensor s: sensorList             ) {            tvSensorActivity.append(s.getName()+"\n");        }*//*
          if(arr[0]==1){                  llsensor.setBackgroundColor(Color.parseColor("#ffffff"));                  }else{                  // object detected                  llsensor.setBackgroundColor(Color.parseColor("#ff0000"));                  }*/
/*//tvSensorActivity.setText("X="+arr[0]+"\n"+"Y="+arr[1]+"\n"+"Z="+arr[2]);        float y = arr[1];        if(y<2 && y>-2){            llsensor.setBackgroundColor(Color.parseColor("#ffffff"));        }        else if(y<4 && y>=2){            llsensor.setBackgroundColor(Color.parseColor("#ffaaaa"));        }        else if(y<6 && y>=4){            llsensor.setBackgroundColor(Color.parseColor("#ff7777"));        }        else if(y<8 && y>=6){            llsensor.setBackgroundColor(Color.parseColor("#ff4444"));        }        else if(y<10 && y>=8){            llsensor.setBackgroundColor(Color.parseColor("#ff0000"));        }        else if(y>-4 && y<=-2){            llsensor.setBackgroundColor(Color.parseColor("#aaffaa"));        }        else if(y>-6 && y<=-4){            llsensor.setBackgroundColor(Color.parseColor("#77ff77"));        }        else if(y>-8 && y<=-6){            llsensor.setBackgroundColor(Color.parseColor("#44ff44"));        }        else if(y>-10 && y<=-8){            llsensor.setBackgroundColor(Color.parseColor("#00ff00"));        }*/

1 comment:

  1. In this modern world, the advancement of techniques has led people to work more comfortably and with more appropriateness. Using sensor in industries such as for evaluating the proper portion of chemicals, the specialists prefer to use chemical level sensors which can immediately tell the result or show the quantity for reactions.
    Chemical Level Sensor

    ReplyDelete

Near by App

https://drive.google.com/file/d/0B2ag35s4X53Eb2pSQVI1SzNudE0/view