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

Reply With Quote




