简体   繁体   中英

Getting pitch from devices that dont have magnetometer

I need to get the orientation of the device(pitch to be specific), I am using a method that involves implementing SensorEventListener the accelerometer and the magnetometer

The problem is some devices dont have the magnetometer(I stumbled upon the samsung J7 and J5 but assuming there are more), all other devices i tested work like a charm. now i do know these devices dont have a magnetometer so i tried registering the deprecated "TYPE_ORIENTATION" event and the "TYPE_ROTATION_VECTOR" event but none work.

All i get on those devices is the event from accelerometer.

I know it is possible because i have downloaded bubble level apps on these phones and they work(compass dosent) Thanks

Edit: I need to scroll views by tilting the phone, so i need to calibrate once and than know if the pitch is bigger or smaller then initial calibration

You need to low pass filter accelerometer to get the gravity vector.

float[] gravity = low pass filter accelerometer

Calculate the norm of the gravity

float gravityNorm = calculate the norm of gravity.

Then pitch would be

(float) Math.asin(-gravity[1] / gravityNorm))

For the formula above read the document for TYPE_PITCH in the class DSensor in my project DSensor at https://github.com/hoananguyen/dsensor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM