[Android] Proximity Sensor

Sathittham (Phoo) Sangthong
SS Blog
Published in
2 min readFeb 14, 2014

Proximity Sensor คืออะไร

Proximity Sensor (อ่านว่า พร็อกซิมิตี้เซนเซอร์) เป็นเซนเซอร์ที่ใช้ในการตรวจจับวัตถุ โดยอาศัยการกะประมาณ ซึ่งเราก็จะวัดค่าแค่ว่าตอนนี้มีวัตถุอยู่หรือไม่มีวัตถุอยู่ (true หรือ false) นั้นเองครับ (ไม่สนใจระยะห่างระหว่างของเซ็นเซอร์กับวัตถุเลยนะครับ) โดยเทคโนโลยีที่นำมาใช้ทำ Proximity sensor ก็มีหลากหลายครับ เช่น สนามแม่เหล็ก สนามไฟฟ้า แสง เสียง เป็นต้น

Proximity sensor ในโรงงานอุตสหกรรมก็มีการใช้งานกันอย่างมากมายและหลากหลายรูปแบบครับ ส่วนใหญ่ก็จะนำมาใช้แทน Limited Switch เนื่องจากว่าไม่ต้องกับสัมผัสกับวัตถุโดยตรง ทำให้อายุการใช้งานมากกว่านั้นเองครับ

  • Proximity ในโทรศัพท์มือถือ

สำหรับในโทรศัพท์มือถือเราจะใช้ Proximity Sensor ในการตรวจจับวัตถุต่างๆ เพื่อใช้ในการปิดหน้าจอ (ไม่ให้หน้าจอสัมผัสทำงานนั้นเอง) ซึ่งโดยทั่วไป ก็ใช้ตอนที่เราทำารรับสายโทรศัพท์ เจ้า Proximity Sensor ก็จะทำการเช็คว่าตอนนี้เราได้หยิบโทรศัพท์แนบกับหู หรือยัง? ถ้าแนบแล้วก็จะทำการปิดหน้าจอสัมผัสไป ถ้าเอาออกแล้ว ก็กลับมาเปิดหน้าจอสัมผัสดังเดิมครับ ซึ่งเทคโนโลยีที่ใช้ส่วนใหญ่ก็จะเป็นแบบ Optical Proximity Sensor เช่น Infrared หรือ Laser ที่มีราคาถูก ใช้งานง่าย ตรวจจับในระยะประมาณ 5 ซม.

ใน Samsung Galaxy Note 2 (ที่ผมใช้อยู่) จะอยู่บริเวณด้านบน ข้างขวาของลำโพง (สำหรับสนทนา) ซึ่งใช้ชิป CM36651 โดยชิปตัวนี้เป็นทั้ง Proximity และ Light Sensor เลยนะครับ

Proximity Sensor ภายใน Note 2 หน้าตาเป็นแบบนี้ครับ

การใช้งาน Proximity บน Android

เราจะทำการตรวจสอบวัตถุกันครับว่ามีหรือไม่มี โดยเซนเซอร์จะอ่านค่าออกมาเป็นเซ็นติเมตร (ซม.) แต่จริงๆแล้วมันแค่ไม่มี คือ 8 ซม. (False) และมีคือ 0 ซม. (True) ครับ จากนั้นผมได้เพิ่มลูกเล่นเข้าไปนิดหน่อยโดยการบอกว่า ถ้ามีวัตถุให้โทรศัพท์เราสั่น และเปลี่ยนสีพื้นหลังครับ

ไม่มีวัตถุ (False) อ่านได้ค่าเต็มที่คือ 8 ซม.

มีวัตถุอยู่ (True) อ่านได้ 0 ซม. มือถือก็จะสั่น และเปลี่ยนสีพื้นหลัง เป็นสีแดงแบบนี้ครับ

  • สั่งให้สั่นด้วย Vibrator
<uses-permission android:name="android.permission.VIBRATE"/>// Get instance of Vibrator
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
  • เปลี่ยนสีพื้นหลัง
//Get application view
view = this.getWindow().getDecorView();
//Set default background color as white
view.setBackgroundColor(0xFFFFFFFF);
  • activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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=".MainActivity" >
<TextView
android:id="@+id/prox_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Proximity Sensor Raw Data (cm)" />
<TextView
android:id="@+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
  • MainActivity.java
package sathittham.sangthong.myproximity;import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {private SensorManager mSensorManager;
private Sensor mProximity;
private TextView distance;
private Vibrator v;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
distance = (TextView) findViewById(R.id.distance);//Get application view
view = this.getWindow().getDecorView();
// Get instance of Vibrator
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
//Set default background color as white
view.setBackgroundColor(0xFFFFFFFF);
distance.setText("Distance from object : " + x);//If detect object
if (x == 0) {
// Vibrate for 500 milliseconds
v.vibrate(500);
// Set color to red
view.setBackgroundColor(0xFFFF0000);
}
}@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mProximity,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
  • AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sathittham.sangthong.myproximity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="sathittham.sangthong.myproximity.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

อ้างอิง

http://www.amda.co.th/2011/technical-skill/มารู้จักพร็อกซิมิตี้/http://developer.android.com/reference/android/graphics/Color.htmlhttp://thecodeartist.blogspot.in/2011/01/proximity-sensor-on-android-gingerbread.htmlhttp://www.capellamicro.com.tw/EN/products_view.php?id=68&mode=16

--

--

Sathittham (Phoo) Sangthong
SS Blog

Hi! It's me Phoo! I’m a Software Developer 👨‍💻 , a Startup Entrepreneur 📱 and a Runner 🏃 . Currently, I’m a Co-Founder and CTO of a Startup name “Urbanice”.