Android: Accelerometer

Source: Internet
Author: User

Android: Accelerometer

Welcome to the sensor world.
The sensor literally refers to the instrument that transmits the feeling. What is the feeling?
Visual, auditory, taste, touch, and smell.
Some people say that the existence and development of sensors allow objects to have the senses such as touch, taste, and smell, so that objects can gradually become active.
At present, Android devices have been integrated into dozens of sensors, and we usually have acceleration sensors, gyroscope, and geomagnetic sensors.
Despite the wide variety of features, the sensor-related functions are completed by providing only a few classes and interfaces in the Framework. Next we will take the accelerometer as an example to guide everyone into the world of Android sensors.

Coordinate System of sensor world


X axis: left to right
Y axis: from bottom to top
Z axis: from inside to outside

This coordinate system is different from the Android 2D API. The returned values in the sensor are subject to this coordinate system.

API Overview

The sensor APIs are stored in the android. hardware package. We mainly use three classes: Sensor, SensorEvent, SensorManager, and SensorEventListener.
SensorManager is responsible for registering and listening to the status of a Sensor. Data of sensorsor is returned through SensorEvent.

Obtain all available sensor from the device

SensorManager provides the getSensorList method and passes in TYPE_ALL to obtain all the sensors of the current device.

SensorManager sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);        List
  
    sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);        mTvInfo.setText(sensors:  + sensors.size());        Log.d(TAG, sensors:  + sensors.size());        for (int i = 0;i < sensors.size();++i) {            Log.d(TAG,sensor name: +sensors.get(i).getName());            Log.d(TAG,sensor vendor: +sensors.get(i).getVendor());            Log.d(TAG,sensor power: +sensors.get(i).getPower());            Log.d(TAG, sensor resolution:  + sensors.get(i).getResolution());        }
  
Sensor coding routine
public class SensorActivity extends Activity, implements SensorEventListener {     private final SensorManager mSensorManager;     private final Sensor mAccelerometer;     public SensorActivity() {         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);         mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);     }     protected void onResume() {         super.onResume();         mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);     }     protected void onPause() {         super.onPause();         mSensorManager.unregisterListener(this);     }     public void onAccuracyChanged(Sensor sensor, int accuracy) {     }     public void onSensorChanged(SensorEvent event) {     } }

After different sensors are extracted from commonalities, everything becomes so simple as defined by the above encoding routines.

Background of the accelerometer

The acceleration here refers to the acceleration of gravity, so the return value of the gravity sensor is the same as that of the acceleration sensor at rest.
The gravity acceleration of static objects on the Earth is about 9.8 Mb/s ^ 2.
Borrow a constant from SensorManager:
Public static final float STANDARD_GRAVITY = 9.80665F;

We can determine the status of the device by using the value on three axes (refer to the coordinate diagram above), for example:
1. When the value of the X axis is close to the acceleration of gravity, it indicates that the device is down to the left.
2. When the X axis value is close to the negative g value, it indicates that the right side of the device is down.
3. When the value of the Y axis is close to the value of g, it indicates that the device is underfloor (the same as that of the device ).
4. When the Y axis value is close to the negative g value, it indicates that the device is down (inverted ).
5. When the Z axis value is close to the g value, the screen of the device is facing up.
6. When the Z axis value is close to the negative g value, the screen of the device is down.
The Code is as follows:

@ Override public void onSensorChanged (SensorEvent sensorEvent) {Log. d (linc, value size: + sensorEvent. values. length); float xValue = sensorEvent. values [0]; // Acceleration minus Gx on the x-axis float yValue = sensorEvent. values [1]; // Acceleration minus Gy on the y-axis float zValue = sensorEvent. values [2]; // Acceleration minus Gz on the z-axis mTvInfo. setText (X axis: + xValue + Y axis: + yValue + z axis: + zValue); if (xValue> mGravity) {mTvInfo. append (gravity points to the left of the device);} else if (xValue <-mGravity) {mTvInfo. append (gravity points to the right of the device);} else if (yValue> mGravity) {mTvInfo. append (gravity points to the bottom of the device);} else if (yValue <-mGravity) {mTvInfo. append (gravity points to the top of the device);} else if (zValue> mGravity) {mTvInfo. append (screen up);} else if (zValue <-mGravity) {mTvInfo. append (screen down );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.