Android Tutorial Orientation and accelerometer

The attached program displays the values of the different sensors:

sensors.png


It creates a PhoneSensors object for each type of sensor.
As you can see in the attached code, two Maps are used to hold the phone sensors and the other required data. This way there is no need to write any duplicate code.

A better method for finding the orientation values: https://www.b4x.com/android/forum/threads/orientation-and-accelerometer.6647/page-6#post-476271
 

Attachments

  • SensorsExample.zip
    7.5 KB · Views: 902
Last edited:

Rick Harris

Well-Known Member
Licensed User
You are correct. The updated jar file is attached. It will show the same version (2.20).
Hello Erel. Installing this updated jar file into the B4A Libraries folder has not solved the problem of the many "True's" in the log screen (on my Nexus-7 tablet; on my HTC phone it is OK). Please find a solution. Thank you.
 

Rick Harris

Well-Known Member
Licensed User
You are currently the only one that encounters this issue. Have you downloaded this library: http://www.basic4ppc.com/android/forum/threads/orientation-and-accelerometer.6647/page-3#post-196085 ?
Hi Erel,
The problem still persists. FYI: every True is preceeded by a line in the log file that states: "Ignoring event: sensor_sensorchanged". After releasing or debugging the app, the TRUE's keep coming, even if I stop the app and remove it from my Android (while leaving B4A running)! Deleting everything that involves the sensors in the sourcecode does not help either. It looks almost as if B4A has started another app or service, but there are no services in my app.
Please help. I am now trying to add In-App billing but the True's make it impossible to debug.

UPDATE: The problem is now solved. This was done by closing one of my other apps which starts up automatically when the Android is switched on (using a Service). Strangely enough this app, even though it should be in activity_pause condition, apparently reports the status of its sensors to B4A while I am working on another app! Any idea about the cause?
Could this be some kind of memory leak?
 
Last edited:

Rick Harris

Well-Known Member
Licensed User
All Basic4android apps use the same "log tag".

When an activity is paused its events will not be raised. However if you haven't explicitly stopped the sensors then the sensors listeners are still active.
If no events are raised by pauzed apps then why do I still see all these TRUE's while working on another app? Surely these TRUE's are the result of events in the paused app (or a log function that is still running somewhere...)?!
 

dossa

New Member
Hello.
i need to give only accelerometer data(z) in variable. Can you help me?
Sorry for my english.
thanks
 

JordiCP

Well-Known Member
Licensed User
Hi all,

I want my app to decide when to be drawn in portrait or landscape mode, or reverse ones. Based on the X,Y sensors, it seems to be easy to know when it has rotated.

For instance, in a phone I get theses values (aprox)

portrait: X=0 Y=9.8
lanscape: X=9.8 Y=0 (rotating 90º left from portrait)

when running on a tablet, I have,

landscape: X=0 Y=9.8
portrait: X=9.8 Y=0

So sensors info is not enough for me to decide when to draw in landscape or portrait, since X,Y assignments seem to be related to the device default orientation. How can I decide how these values are related to the actual screen orientation?

(One possible approach would be trying to "guess" default screen orientation based on device type and screen size, but I am sure this will not work always.)

----------EDIT------------------------
Have just found LibDisplay tutorial from joseluis

http://www.basic4ppc.com/android/forum/threads/libdisplay-module.14111/#post79879

So I assume that accelerometer sensor information is related to the default screen.

Thanks.
 
Last edited:

JordiCP

Well-Known Member
Licensed User
Why don't you let Android take care of that?

You can set the #SupportedOrientations value to fullSensor.

Thank you for pointing this, Erel

There are two reasons for doing it this way

  1. My app uses the camera and only want the device to rotate under certain situations. For instance, not rotate accidentally when taking the photo, and let the user rotate freely when editing it
  2. In some devices, I still have a problem each time I rotate, memory gets lower till it crashes as stated here. So I want the user to start the app in the orientation he chooses, but blocking rotation afterwards.

I would prefer only to control it only for reason 1, but the second is also there
 

Scooter Willis

Member
Licensed User
Hi Erel,
The problem still persists. FYI: every True is preceeded by a line in the log file that states: "Ignoring event: sensor_sensorchanged". After releasing or debugging the app, the TRUE's keep coming, even if I stop the app and remove it from my Android (while leaving B4A running)! Deleting everything that involves the sensors in the sourcecode does not help either. It looks almost as if B4A has started another app or service, but there are no services in my app.
Please help. I am now trying to add In-App billing but the True's make it impossible to debug.

UPDATE: The problem is now solved. This was done by closing one of my other apps which starts up automatically when the Android is switched on (using a Service). Strangely enough this app, even though it should be in activity_pause condition, apparently reports the status of its sensors to B4A while I am working on another app! Any idea about the cause?
Could this be some kind of memory leak?

I have the same repeated output of true problem when beginning to listen to sensor value for barometric pressure. Using v3.0 with 2.2 phone library.
 

Ashley

Member
Licensed User
The attached program displays the values of the different sensors:

sensors.png


It creates a PhoneSensors object for each type of sensor.
As you can see in the attached code, two Maps are used to hold the phone sensors and the other required data. This way there is no need to write any duplicate code.
Hi Erel, I'm trying to get the exact direction in degrees the phone is facing. The position variables (x,y,z) seems to give errors in some directions. Is there any code that combines the relevant sensors to get the AHRS?.
Thanks
Ashley
 
Top