Implementation of Proximity Sensor with Android

  • 2021-12-04 11:07:03
  • OfStack

In this paper, we share the specific code of Android proximity sensor for your reference. The specific contents are as follows

1. The proximity sensor detects the distance between the object and the earpiece (mobile phone), and the unit is centimeters.

Some proximity sensors can only return far and near states. For example, my mobile phone Meizu E2 can only recognize two distances: 0CM (close distance) and 5CM (long distance)
Therefore, the proximity sensor returns the maximum distance to the far state and the distance less than the maximum to the near state.
The proximity sensor can be used to automatically turn off the LCD screen when answering the phone to save power.

Some chips integrate the functions of proximity sensor and light sensor (Meizu E2).

2. The code is as follows:

MainActivity.class


package com.example.sz.proximitytest;

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.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
 private static final String TAG = "MainActivity";
 private SensorManager mSensorManager=null;
 private Sensor mSensor=null;
 private TextView textView1=null;
 private TextView textView2=null;
 private TextView textView3=null;
 private Button button1=null;
 private Button button2=null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 textView1 = (TextView) findViewById(R.id.textView1);
 textView2 = (TextView) findViewById(R.id.textView2);
 textView3 = (TextView) findViewById(R.id.textView3);
 /* Get system services ( SENSOR_SERVICE ) Returns 1 A SensorManager Object */
 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
 /* Pass SensorManager Get the corresponding (proximity sensor) Sensor Type object */
 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
 /* Register the corresponding SensorService*/
 button1 = (Button) findViewById(R.id.button1);
 button1.setOnClickListener(new Button.OnClickListener() {

  @Override
  public void onClick(View arg0) {
  mSensorManager.registerListener(mSensorEventListener, mSensor
   , SensorManager.SENSOR_DELAY_NORMAL);
  }
 });
 /*  Destroy the corresponding SensorService
  *  The key part, note, is mentioned in the description document, even if Activity When it is invisible, the sensor will continue to work 
  *  So 1 Be sure to turn off the trigger, otherwise it will consume a lot of power from the user */
 button2 = (Button) findViewById(R.id.button2);
 button2.setOnClickListener(new Button.OnClickListener() {

  @Override
  public void onClick(View v) {
  mSensorManager.unregisterListener(mSensorEventListener, mSensor);
  }
 });
 }

 /* Declaration 1 A SensorEventListener Object is used to listen Sensor Event and overloads the onSensorChanged Method */
 private final SensorEventListener mSensorEventListener = new SensorEventListener() {

 @Override
 public void onSensorChanged(SensorEvent event) {
  Log.e(TAG, "onSensorChanged: -----0------"+event.values[0]);
  Log.e(TAG, "onSensorChanged: ------1-----"+event.values[1]);
  Log.e(TAG, "onSensorChanged: --------2---"+event.values[2]);




  if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
  /* The proximity sensor detects the distance between the object and the earpiece in centimeters. */
  // It should be noted here that normal is to take the first place 1 Bit, but I encountered the value of 1 Take the first place 2 Bitwise 
  float distance1 = event.values[0];
  float distance2 = event.values[1];
  float distance3 = event.values[2];
  textView1.setText("[0] Distance: "+String.valueOf(distance1) + "cm");
  textView2.setText("[1] Distance: "+String.valueOf(distance2) + "cm");
  textView3.setText("[2] Distance: "+String.valueOf(distance3) + "cm");
  }
 }

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

 }
 };


}

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
 android:gravity="center"
 android:orientation="vertical"
 tools:context=".MainActivity">

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:text=" Open " />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:text=" Shut down " />
</LinearLayout>

Source code download: Android proximity sensor


Related articles: