Android Question Sensors example

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all. I need to add a function detecting when the phone is horizontal. I downloaded from GooglePlay some programs that do exactly what I want, so I argued that I can implement something about my problem, accessing Gyroscope sensor, that seems to be the only sensor able to detect phone position. With my surprise, while the downloaded apps work on my phone, the B4A example found here says that no Gyroscope sensor is available. I did nothing but compiling the example found here. I tried probably all the examples found in this forum, and no one works. Actually they are (very) old. So maybe Android changes are responsible for this situation or am I missing something ?
 
Last edited:

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Probably it depends on the phone, having only accelerometer sensor. This seemed unlikely, but other phones give better results. So, forget about. Thanks for reading.
 
Upvote 0

emexes

Expert
Licensed User

Gyroscope detects (relative) rotational movement, not (absolute) orientation.

Accelerometers can detect orientation with respect to gravity, assuming that phone is not changing speed or direction. If you throw your phone up in the air then - from when it leaves your hand until when you catch it on the way down - it will measure all zeroes in all axis, and be unable to detect if it is horizontal, vertical, or any other orientation.

If horizontal means laying flat horizontally, then Z-axis will be approx. 1 G (9.8 m/s/s) usually +1 if face up or -1 if face down. The X and Y axis will correspondingly be approx. 0 G. So if abs(Z-axis) is > 0.8 G and abs(X-axis) and abs(Y-axis) are both < 0.2 G then that would indicate your phone is (close enough to) horizontal.

Other than the Samsung phones with VR, few Android phones have gyroscopes. Admittedly, I do live at the cheap end of the spectrum; I think all my Android phones are under $100. iPhones have had gyroscopes since the iPhone 4, and I think all iPhones since have had them, even the cheaper plastic ones.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
To your question. Gyroscope sensor can be substituted with Orientation, Y and Z value can tell you whether the phone is horizontal.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Thanks. Unluckily this phone has only accelerometer and proximity (Samsung Galaxy A12). Anyway, seeing that there are Apps doing what I need on this phone, the conclusion is that just accelerometer is evidently enough. Thanks again.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…