Use of Android phone sensor

Source: Internet
Author: User

Now there are all kinds of sensors in Android phones, such as accelerometers, gyroscopes and the like, I now use a total of nine sensors in the Vivo V3 phone:

Lis3dh-accel : Tri-axis acceleration sensor

Tmd277x-proximity : Near distance sensor

Tmd277x-light : Light sensor

Yas533-orientation : Direction sensor

Yas533-pseudo-gyro : Motion sensor

Yas533-linear-acceleration : Linear acceleration Sensor

Yas533-rotation-vector : rotational vector sensor

Yas533-gravity : Gyroscope

Yas533-mag : Magnetic sensor

The above sensors are self-written by a program measured out.

The operation of the sensor in the phone is nothing more than a few steps:

    1. Get sensor Manager
    2. Get Sensor Object
    3. Registering sensor monitoring
    4. Capturing data in sensor monitoring (data change events)

First step: Get the Sensor manager

Sensormanager Sensormanager = (sensormanager) getsystemservice (Context.sensor_service); // Get sensor Manager

There are several important methods in Sensormanager:

    • getsensorlist (int type) returns a list collection of all sensor objects (sensors) in the phone set
    • Getdefaultsensor (int type) returns the type of the specified sensor object, such as sensors accelsensor = Sensormanager.getdefaultsensor (sensor.type_  ACCELEROMETER); Gets the Accelerometer object
    • Registerlistener (sensorlistener Listener, int sensors, int rate) Register Sensor listener Events

Step two: Get the Sensor object

Sensor accelsensor = Sensormanager.getdefaultsensor (Sensor.type_accelerometer); // Get Sensor Object

The sensor object is primarily used to determine what sensor to operate on, in the above example (triaxial) acceleration sensor

    • The sensor class has many type_xxxx int-value constants, each of which represents a different sensor, which is primarily used to determine the transducer type when acquiring a sensor object
    • GetName () get the sensor name
    • Getpower () Gain sensor power, unit unknown
    • Getresolution () Get sensor resolution
    • GetType () Get sensor type
    • Getvendor () Get sensor manufacturer
    • GetVersion () Get the sensor version

Step Three: Register Sensor listener Events

Using the Sensormanager in the

/**  *   @param  @param @param */int int Rate

    • Create a class to implement the listener's Sensoreventlistener interface, and implement two of these methods: the Onsensorchanged (Sensorevent event) method of the callback when the sensor value changes, the resolution change callback onaccuracychanged (int sensor, int accuracy) method (emphasis)
    • The Onsensorchanged (Sensorevent event) method uses Event.values to get the float array, and the value of the array is the measured value, as detailed in the API
    • The second parameter passes in the Sensor object
    • The third parameter is a sample rate,
      Sensor_delay_normal  200000μs Sampling once
      SENSOR_DELAY_UI 60000μs Sampling Once
      Sensor_delay_game   2000μs Sampling once
      Sensor_delay_fastest   0μs Sampling once (at the fastest CPU sampling rate)

For example:

Sensormanager.registerlistener (new Accellistener (), Accelsensor, sensormanager.sensor_delay_normal); // registering sensor listener Events
    /*** Internal class, implement Sensoreventlistener interface *@authorAdministrator **/    classAccellistenerImplementssensoreventlistener{/** Callback when sensor value changes * @see android.hardware.sensoreventlistener#onsensorchanged (android.hardware.SensorEvent )         */@Override Public voidonsensorchanged (Sensorevent event) {//TODO auto-generated Method Stub            floatI= event.values[0];                        System.out.print (i); } @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy) {            //TODO auto-generated Method Stub                    }            }    

The Android Develop API Android.hardware has a detailed description of the sensor acquisition method and algorithm, and I hope you will read it carefully.

Use of Android phone sensor

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.