Android Question Screen Facing up and down

Antony Danby

Member
Licensed User
Longtime User
I am wondering if anyone can help me. I am new to B4A and I want to create an application to trap when a phone is face down or when it is face up.

I have already looked at the Orientation and accelerometer tutorial at
http://www.b4x.com/android/forum/threads/orientation-and-accelerometer.6647/

The accelerometer and gyroscope figures go mad when i run that example. The
accelerometer and gyro numbers never stop changing even though my phone is face up on the table and not moving.

I have a Galaxy S4 to test on and this example say that Orientation is not supported, but I know other apps ( Tasker ) where it works perfectly well.

I have searched on stackoverflow and got this response:
http://stackoverflow.com/questions/...t-to-determine-if-device-is-facing-up-or-down

but I don't know what SensorManager.getRotationMatrix is.

Any help would be great.
Thanks
 

udg

Expert
Licensed User
Longtime User
Hi Antony,

please find documentation for SensorManager here.
Using the Reflection library you can call the getRotationMatrix function.

Umberto
 
Upvote 0

Antony Danby

Member
Licensed User
Longtime User
Umberto

Could I ask you if you can give me an example of how this would be done in this case.

Thank you
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Antony,
I don't know this matter well, so I had a quick look at the documentation.
To access the getRotationMatrix function by the Reflection lib, you should code something like the following:
B4X:
Dim R As Reflector
Dim b As Boolean
Dim Rot(9),Inclin(9),Grav(3),GeoM(3) As Float
b=R.RunStaticMethod("android.hardware.SensorManager","getRotationMatrix",Array As Object(Rot,Inclin,Grav,GeoM),Array As String("[F","[F","[F","[F"))
If b = True Then Log("success: "&Rot(8)) Else Log("error")
Before the function call, use Erel's tutorial to get values to pass in Gravity and GeoMagnetic arrays.
I'm sure someone more knowledgeble on the subject will jump in and help you in a more precise way.

Umberto
 
Upvote 0

Antony Danby

Member
Licensed User
Longtime User
Umberto

Very very helpful. Thank you so much
Can't try it right now, as I have to earn my pennies at work ;-)
As soon as I am in front of my home PC I will be trying this

Thanks once again
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Antony,

please find attached a project skeleton that, in a way, does it all as the StackOverflow example.
It's more a proof on concept than a functioning app, so there's some work for you to be done.
Anyway, it seems to works well with my tablet..

Umberto
 

Attachments

  • sensors1.zip
    6.6 KB · Views: 337
Upvote 0

Victor jung

Member
Licensed User
Longtime User
Thanks for your example anthony, I am trying to call getOrientation using your example, so like this:
b2=R2.RunStaticMethod("android.hardware.SensorManager","getOrientation",Array As Object(Rot,ypr),Array As String("[F","[F")

where ypr is defined as Dim ypr(3) as float

any ideas why it doesnt work?
 
Last edited:
Upvote 0
Top