×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Question N97 access compass using Orientation.getOrientation()

    Hello everyone!

    Problem synopsis
    I failed trying to access the N97’s compass from java (J2ME) using Orientation.getOrientation(). The methods always returns NULL, suggesting that "the orientation can’t be currently determinated"

    Solutions
    jschang (see post #8) got the posted code running on the N97 by:
    1.) Updating the firmware to 11.0.21
    2.) Having another compass application running in the background at the same time
    However, according to pstefan (see posts #9) the compass needs to be at best signal strengh and the midlet keeps crashing after 110 calls of Orientation.getOrientation().


    The details
    My goal was to access the built-in magnetic compass from J2ME. According to the following thread http://discussion.forum.nokia.com/fo...d.php?t=147285 in order to obtain compass values Nokia Maps has to be running and the compass has to be calibrated. Then, the code fragment “Orientation or = Orientation.getOrientation();” would return the compass orientation. The thread also explicitly states that this should be supported in the N97’s S60 5th Edition.

    Following the above instructions, I started Nokia Maps, calibrated the compass until the compass symbol went green and the map aligned itself north. Then, without closing Nokia Maps, I started my J2ME application that continuously calls Orientation.getOrientation(); . However, the method always returns NULL.

    Reading the JSR-179 API’s description of the Orientation class (see http://www-users.cs.umn.edu/~czhou/d...api/index.html), I learned that returning NULL means that the orientation can’t currently be determined. I also learned that if a device did not support retrieving the device’s orientation, a LocationException would have been thrown – which hasn’t occurred.
    Thus, I assume that the N97 supports accessing the compass from Java, but I somehow fail to initialise it correctly.


    The used program

    Code:
    package location;
    
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.StringItem;
    import javax.microedition.location.Orientation;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    public class OrientationPrototype extends MIDlet implements Runnable,
    		CommandListener {
    
    	// ===================================================================
    	// Fields
    	// ===================================================================
    
    	private Thread		runner;
    	private boolean		finished;
    
    	private Display		display;
    	private Form		form;
    	private Command		exitCommand;
    	private StringItem	orientItem;
    
    	// ===================================================================
    	// Constructor
    	// ===================================================================
    
    	public OrientationPrototype() {
    		this.display = Display.getDisplay(this);
    		this.form = new Form("OrientationPrototype");
    
    		this.exitCommand = new Command("exit", Command.EXIT, 0);
    		this.form.addCommand(exitCommand);
    		this.form.setCommandListener(this);
    
    		this.orientItem = new StringItem("orientation", "");
    		this.form.append(orientItem);
    	}
    
    	// ===================================================================
    	// Methods inherited from MIDlet
    	// ===================================================================
    
    	protected void startApp() throws MIDletStateChangeException {
    		this.display.setCurrent(form);
    		this.start();
    	}
    
    	protected void pauseApp() {
    	}
    
    	protected void destroyApp(boolean unconditional)
    			throws MIDletStateChangeException {
    		if (!unconditional)
    			this.stop();
    	}
    
    	// ===================================================================
    	// Implementation of CommandListener
    	// ===================================================================
    
    	public void commandAction(Command c, Displayable d) {
    		if (c == exitCommand) {
    			this.stop();
    			this.notifyDestroyed();
    		}
    	}
    
    	// ===================================================================
    	// Methods for obtaining and displaying the orientation
    	// ===================================================================
    
    	/**
    	 * Starts the MIDlets runner
    	 */
    	protected synchronized void start() {
    		this.runner = new Thread(this);
    		this.runner.start();
    	}
    
    	/**
    	 * Stops the MIDlets runner
    	 */
    	protected synchronized void stop() {
    		this.finished = true;
    		this.runner.interrupt();
    	}
    
    	public void run() {
    		while (!finished) {
    			String orientation = obtainOrientation();
    			this.orientItem.setText(orientation);
    		}
    	}
    
    	private String obtainOrientation() {
    		try {
    
    			Thread.sleep(1000);
    
    			// According to JSR-179 javadoc Orientation#getOrientation()
    			// "returns an Orientation object containing the terminal's current
    			// orientation or null if the orientation can't be currently
    			// determined"
    			Orientation or = Orientation.getOrientation();
    
    			if (or == null) {
    				return "or is NULL";
    			}
    
    			return or.getCompassAzimuth() + "°";
    
    		} catch (Exception e) {
    			return "" + e;
    		}
    	}
    }


    I'd be glad if someone had suggestions how to get this running!

    Thanks in advance,
    Martin
    Last edited by mpielot; 2009-07-28 at 08:35. Reason: Integrated new info (thanks pstefan)

  2. #2
    Regular Contributor
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    178

    Re: N97 access compass using Orientation.getOrientation()

    According to this, it says that "The Orientation support for Location API (JSR-179), if device has magnetometer hardware". At the very least the getOrientation() method should work. Perhaps there is a small error in your code? Not having an N97 I can't test it.

    If you do get it working, would you mind trying out the getPitch() and getRoll() methods in the Orientation class too? I'm interested in seeing if those work on the N97.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Re: N97 access compass using Orientation.getOrientation()

    Quote Originally Posted by Larry101 View Post
    Perhaps there is a small error in your code? Not having an N97 I can't test it.

    If you do get it working, would you mind trying out the getPitch() and getRoll() methods in the Orientation class too? I'm interested in seeing if those work on the N97.
    I'd be happy, if it was just a small error. I added the code to the initial thread.

    If I get the Orientation class working, I will try out the two methods and post the results here.

  4. #4
    Regular Contributor
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    178

    Re: N97 access compass using Orientation.getOrientation()

    Firstly, let me just say I don't think you should worry too much about the other thread you cited. That person was trying to use the Orientation class on a device which did not support the Orientation class (hence the LocationException he got). I find it unlikely that in order to get orientation information, you first need to start Nokia Maps. I could be wrong, but that seems unlikely.

    Having said that, I don't see anything obvious wrong with your code. The example in the Java ME developers library also has a thread with the following run method (the getOrientation() method just calls Orientation.getOrientation()):
    Code:
    public void run()
        {
            // Keep the thread running until another displayable is set visible.
            // See also hideNotify() method.
            while (threadActive)
            {
                Orientation orientation = ConfigurationProvider.getInstance()
                        .getOrientation();
    
                if (orientation != null)
                {
                    isMagnetic = orientation.isOrientationMagnetic();
                    azimuth = orientation.getCompassAzimuth();
                }
    
                repaint();
    
                try
                {
                    // Pause this thread for a secord before next update.
                    Thread.sleep(SLEEPTIME);
                }
                catch (InterruptedException e)
                {
                }
            }
        }
    Have you tried running the example midlet in the developer's guide: Tourist Route

    That seems to make use of the Orientation class.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Re: N97 access compass using Orientation.getOrientation()

    Thanks, Larry, for suggesting the Tourist Route sample application (http://www.forum.nokia.com/info/sw.n..._v1_0.zip.html). I installed in on an N97. However, the compass does not work there either.

    I also realised that this sample application as well as my own prototype keep crashing after about one minute. Both applications just exit without a note. I wonder if my N97 might have a hardware or firmware bug.

    Is anyone out there you successfully accessed the compass on an N97 from Java?

  6. #6
    Regular Contributor
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    178

    Re: N97 access compass using Orientation.getOrientation()

    If the sample application doesn't work, then perhaps there is a firmware issue. The compass is a bit of a strange thing to use because of the calibration step that seems to be required (I read about this in the N97 user guide I found online). Let's hope it's a firmware bug, which Nokia should fix, considering that Orientation support was promised...

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Re: N97 access compass using Orientation.getOrientation()

    Quote Originally Posted by Larry101 View Post
    If the sample application doesn't work, then perhaps there is a firmware issue. The compass is a bit of a strange thing to use because of the calibration step that seems to be required (I read about this in the N97 user guide I found online). Let's hope it's a firmware bug, which Nokia should fix, considering that Orientation support was promised...
    The calibration step is indeed wierd ... sitting in my office drawing virtual 8s in the air

    I came to the same conclusion: something with the firmware. I am giving up at this point. Thanks for your thoughts, Larry!

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Re: N97 access compass using Orientation.getOrientation()

    Quote Originally Posted by mpielot View Post
    Hello everyone!

    [...]

    I'd be glad if someone had suggestions how to get this running!

    Thanks in advance,
    Martin
    Hi, I've got a workaround to run your application. It's not feasable, but it shows that your code is basically correct should do:

    -Install another compass-app for the N97 (for example N97Compass from http://thinkchange.cn/products/compass.php)
    -Start this app (you can calibrate the compass now until the signal says "best")
    -Push the "Programs" button (or switch task via the taskmanager), go to your app and start it, while the N97Compass is still running in the background, and you will get the correct degrees shown instead of the "or is NULL" string.

    When you close both apps and start only yours again, it will not work, you will get the "or is NULL" again...


    This is really strange and I don't know what causes this problem... Maybe there is a bug in the firmware that causes this odd behaviour (I have v11.0.21)...

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Lightbulb Re: N97 access compass using Orientation.getOrientation()

    I've tried the compass in the N97 too. With the same results as described above in this thread.
    The tipp of jschang works fine, but the midlet still crashes after a while.

    A few things I've noticed additionally:

    * Pitch and Roll are not supported on the N97 (it is always Float.NaN)

    * I only get a Orientation object, if the N97Compass App has best signal strength. Good is not good enough for j2me.

    * The midlet doesn't crash after 1 minute. It crashes exactly after calling the getOrientation() Method 110 times. The time between the calls does not have any effect on the crash. At the 111s call of getOrientation() the midlet is closed.

    * Other running java midlets are not affected by the crash of the compass midlet

    * It is not only a problem on N97. The 5800 also crashes after a while. Although - as far as I know - it doesn't have a builtin compass. Seems to be a problem of the jvm of symbian s60v5.

    I hope Nokia finds the failure and fixs it. I want to add a reliable compass to my lbs system.

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Re: N97 access compass using Orientation.getOrientation()

    Quote Originally Posted by pstefan View Post

    * The midlet doesn't crash after 1 minute. It crashes exactly after calling the getOrientation() Method 110 times. The time between the calls does not have any effect on the crash. At the 111s call of getOrientation() the midlet is closed.
    I can confirm this. The application crashes exactly after the 110th call of gehtOrientation(). It produces an unhandled exception: Out Of Memory Error java.lang.OutOfMemoryError Failed to fork OS thread.
    Last edited by jschang; 2009-07-28 at 21:55.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: N97 access compass using Orientation.getOrientation()

    Hi,

    If it is OutOfMemoryError, then you are flooding the JVM and your code is not optimized to support such a usage pattern. In this case does not seem to be a problem with device's firmware as OutOfMemoryError can happen with any other device. Double check that!

    Br,
    Juarez Alvares Barbosa Junior - Brazil

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    26

    Question Re: N97 access compass using Orientation.getOrientation()

    Quote Originally Posted by jschang View Post
    I can confirm this. The application crashes exactly after the 110th call of gehtOrientation(). It produces an unhandled exception: Out Of Memory Error java.lang.OutOfMemoryError Failed to fork OS thread.
    Hi, all, I have also tested this feature in N97, After the 110th call of getOrientation(), the same error appears, just as jschang said. Anyone fixed it? Thanks in advance.
    Regards.
    tao

  13. #13
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Re: N97 access compass using Orientation.getOrientation()

    Quote Originally Posted by taojia View Post
    Hi, all, I have also tested this feature in N97, After the 110th call of getOrientation(), the same error appears, just as jschang said. Anyone fixed it? Thanks in advance.
    Regards.
    tao

    Maybe it's the repeated call of Orientation.getOrientation(); I would not see any other potential source of flooding the VM.

    Can somebody test this fragment?

    Code:
        public void run() {
            Orientation orientation = null;
    
            while (orientation == null)        
                  orientation = Orientation.getOrientation();
    
    
            while (threadActive)
            {
                azimuth = orientation.getCompassAzimuth();
                
                // TODO utilise/visualise azimuth and see if it updates correctly
    
                try
                {
                    Thread.sleep(SLEEPTIME);
                }
                catch (InterruptedException e)
                {
                }
            }
        }

  14. #14
    Registered User
    Join Date
    Jul 2009
    Posts
    26

    Re: N97 access compass using Orientation.getOrientation()

    Quote Originally Posted by mpielot View Post
    Maybe it's the repeated call of Orientation.getOrientation(); I would not see any other potential source of flooding the VM.

    Can somebody test this fragment?

    Code:
        public void run() {
            Orientation orientation = null;
    
            while (orientation == null)        
                  orientation = Orientation.getOrientation();
    
    
            while (threadActive)
            {
                azimuth = orientation.getCompassAzimuth();
                
                // TODO utilise/visualise azimuth and see if it updates correctly
    
                try
                {
                    Thread.sleep(SLEEPTIME);
                }
                catch (InterruptedException e)
                {
                }
            }
        }
    Hi, Sorry for the delaying. I have tested the code above, but bad luck, it failed. Basically, the program will get the same azimuth if using the code above. So it seems that repeated calling the method "orientation = Orientation.getOrientation()" will initialize the orientation object each time, and we could obtain different azimuth. But repeated calling Orientation.getOrientation() will result in the unhandled Exception(exactly after 110th call):
    Out of Memory Error
    Java.lang.OutOfMemoryError
    Failed to fork OS thread.
    Anyone could help us? Thanks.
    Regards.
    tao

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    24

    Re: N97 access compass using Orientation.getOrientation()

    OutOfMemoryError... Sure.

    Orientation orientation = null;

    while (orientation == null)
    orientation = Orientation.getOrientation();

    If you never reach an orientation object in time = endless loop.
    I think there must be another way to code this.

    Try to set the Thread in wait state until he gets an orientation object.

    KingZesi

Similar Threads

  1. Thawte Premium Server CA
    By Thygrrr in forum Mobile Java Networking & Messaging & Security
    Replies: 10
    Last Post: 2006-09-12, 05:04
  2. Access the 5140's Compass & GPS streams via Java
    By westie in forum Mobile Java Tools & SDKs
    Replies: 2
    Last Post: 2005-07-01, 09:57
  3. Gateway IP address in Nokia 6600 access point
    By yashodhan in forum General Development Questions
    Replies: 0
    Last Post: 2004-10-01, 12:22
  4. Midlet using a default Access Point
    By fboya in forum Mobile Java General
    Replies: 6
    Last Post: 2003-12-23, 09:58
  5. Midlet using a default Access Point
    By fboya in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2003-07-03, 18:53

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×