Android Question Sensor orientation

Rainer@B4A

Member
Licensed User
Longtime User
I want to display North as a symbol on the screen.

My phone gives orientation.azimuth = 0.0 when pointing its short top edge to North.
My tablet when pointing with the long edge.
This is for both independent from the activity layout (Portrait/Landscape/Reverse).

How to get my application informed about the orientation of the build in sensor ?

Additional to this information I need to know the current layout orientation, which is forced by the user holding the device straight or upside down etc.
 

Rainer@B4A

Member
Licensed User
Longtime User
Hi Tom and Dog,
thanks for your reply,
but it doesn't help me to find the right properties/methods I have to use in B4A for my app.

- 0° direction of the build in orientation.azimuth sensor
- recent layout orientation

each of both may have the value Portrait, Landscape, Revers Portrait, Revers Landscape
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to find the device default orientation:

B4X:
'True = Landscape, False = Portrait
Sub GetDefaultOrientation As Boolean
   Dim context As JavaObject
   context = context.InitializeStatic("anywheresoftware.b4a.BA").GetField("applicationContext")
   Dim rotation As Int = context.RunMethodJO("getSystemService", Array As Object("window")) _
     .RunMethodJO("getDefaultDisplay", Null).RunMethod("getRotation", Null)
   Dim configOrientation As Int = context.RunMethodJO("getResources", Null).RunMethodJO("getConfiguration", Null) _
     .GetField("orientation")
   If ((rotation = 0 OR rotation = 2) AND configOrientation = 2) OR _
     ((rotation = 1 OR rotation = 3) AND configOrientation = 1) Then
     Return True
   Else
     Return False
   End If
End Sub

I haven't tested it. It is based on: http://stackoverflow.com/questions/...ault-orientation-on-android-i-e-get-landscape
 
Upvote 0

Rainer@B4A

Member
Licensed User
Longtime User
Hi Erel, you are the best, even at the weekend.

This problem becomes a research project for me, but now I feel myself like an expert and I will share the result with our community:

There is a 'normal' orientation (rotation) for a device. By holding the device in this orientation, the function DisplayGetRotation() returns 0.
Turning the device left (counter clockwise) it returns 1, turning it right (clockwise) returns 3 and upside down it returns 2.
The 'normal' orientation of a phone is portrait, for a tablet it is landscape.
With the function GetDeviceType() the type phone, tablet or watch can be detected.

B4X:
'**************************************************************
'*    Returns the type of the device
'*    0 = phone, 1=tablet, 2=watch
'*
Sub GetDeviceType As Int
    Dim LayVal As LayoutValues = GetDeviceLayoutValues
    Dim Rot As Int = DisplayGetRotation
  
    If LayVal.Height = LayVal.Width Then
        Return 2
    Else If Rot = 0 OR Rot = 2 Then
        If LayVal.Height > LayVal.Width Then Return 0
        Return 1
    Else
        If LayVal.Height < LayVal.Width Then Return 0
        Return 1
    End If
End Sub

'**************************************************************
'*    Returns the current screen rotation
'*    0 = normal, 1=turned left, 2=upside down, 3= turned right
'*    (Thanks to joseluis)
Sub DisplayGetRotation As Int
    Dim r As Reflector
    r.Target = r.GetActivity
    r.Target = r.RunMethod("getWindowManager")
    r.Target = r.RunMethod("getDefaultDisplay")
    Return r.RunMethod("getRotation")
End Sub

I want to mark the north direction on the screen.
The magnetic sensor returns 0°, if you hold the device in the 'normal' orientation looking to north.
So I have to combine the magnetic information, the device orientation and the device type.
So it's done in the attached project.

Oh, I don't know how to attach a zip file !
 
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Dear...

TIP: in a Google Glass the Display Orientation call by "r.GetField("orientation")" not work.

The error is:

java.lang.NullPointerException
at anywheresoftware.b4a.agraham.reflection.Reflection.GetField(Reflection.java:310)
at com.visualnet.glasscolor.cameraexclass._setdisplayorientation(cameraexclass.java:930)
at com.visualnet.glasscolor.cameraexclass._camera_ready(cameraexclass.java:160)

and then just take off this line and your variable put = 0 and all works again.

Hugs for everyone!
 
Upvote 0
Top