Android Question Sensor orientation

Discussion in 'Android Questions' started by Rainer@B4A, Jan 17, 2014.

  1. Rainer@B4A

    Rainer@B4A Member Licensed 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.
     
  2. TomA

    TomA Active Member Licensed User

  3. Rainer@B4A

    Rainer@B4A Member Licensed 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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code to find the device default orientation:

    Code:
    '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 = 2AND configOrientation = 2OR _
         ((rotation = 
    1 OR rotation = 3AND configOrientation = 1Then
         
    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
     
    cyiwin and Theera like this.
  5. Rainer@B4A

    Rainer@B4A Member Licensed 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.

    Code:
    '**************************************************************
    '*    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 !
     
  6. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed 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!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice