Poll for magnetometer data

Note: Polling too frequently can significantly reduce your battery power level.
  1. Import the required classes and interfaces.
    import net.rim.device.api.system.Application;
    import net.rim.device.api.system.MagnetometerChannelConfig;
    import net.rim.device.api.system.MagnetometerData;
    import net.rim.device.api.system.MagnetometerListener;
    import net.rim.device.api.system.MagnetometerSensor;
    import net.rim.device.api.system.MagnetometerSensor.Channel;
    import net.rim.device.api.ui.component.Dialog;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.container.MainScreen;
    import net.rim.device.api.ui.UiApplication;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new application class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The DataScreen class, which is described in step 5, represents the custom screen.
    public class MagPoll extends UiApplication
    {
        private DataScreen _scrData;
        
        public static void main(String[] args)
        {
            MagPoll app = new MagPoll();
            app.enterEventDispatcher();
        }
        
        public MagPoll()
        {
            _scrData = new DataScreen();
            pushScreen(_scrData);
        }
    
  3. Indicate that the application class implements the Runnable interface. Provide an implementation of the run()method as specified by the interface. Invoke displayData() to pass the magnetometer data to the screen to display. In the application constructor, create a new Thread object to use to call your implementation of run(). Invoke start() to create the new thread to use for polling.
    public class MagPoll extends UiApplication implements Runnable
    {
        private DataScreen _scrData;
        
        public static void main(String[] args)
        {
            MagPoll app = new MagPoll();
            app.enterEventDispatcher();
        }
        
        public MagPoll()
        {
            _scrData = new DataScreen();
            pushScreen(_scrData);
            Thread poller = new Thread(this);
            poller.start();
        }
        
    public void run()
        {
           //implementation in next step
        }
    }
  4. Add a boolean test flag that you can use to break out of the polling loop. Provide an implementation of run() to do the polling. Invoke openChannel() to create a Channel object that provides a bridge between your application and the magnetometer over which data can flow. Create a loop that only ends if the value of the boolean test flag is set to false. Invoke getData() to retrieve a MagnetometerData object containing a snapshot of data from the magnetometer. Synchronize the MagnetometerData object stored in the data object and invoke getDirection() to retrieve the clockwise-positive angle between magnetic north and the top of the device. Invoke displayData() to display the magnetometer data on the screen. Invoke Thread.sleep() to establish the polling frequency. Outside of the loop, invoke magChannel.close() to stop polling the magnetometer.
    public class MagPoll extends UiApplication implements Runnable
    {
        private DataScreen _scrData;
        private boolean bRunning = true;
        
        public static void main(String[] args)
        {
            MagPoll app = new MagPoll();
            app.enterEventDispatcher();
        }
        
        public MagPoll()
        {
            _scrData = new DataScreen();
            pushScreen(_scrData);
            Thread poller = new Thread(this);
            poller.start();
        }
        
        public void run()
        {
     Channel magChannel = MagnetometerSensor.openChannel( Application.getApplication());
         
         while( bRunning ) 
         {
             double direction = 0;
             MagnetometerData data = magChannel.getData();
             synchronized( data ) 
             {
                 direction = data.getDirection(MagnetometerData.MAGNETOMETER_GET_DIRECTION_TOP);
             }   
     
             _scrData.displayData(direction);
    
             try
             {
                Thread.sleep(500);
             }
             catch(Exception e)
             {
                 //handle exception
             }
         }
         magChannel.close();
        }
    }
  5. Create the custom screen for the application by extending the MainScreen class. In the screen constructor, create a LabelField and add it to the screen. Provide an implementation of displayData() to convert the numeric direction value to a string and display the result by setting the text of the LabelField.
    class DataScreen extends MainScreen
    {
        LabelField _labelMagDir;
        
        public DataScreen()
        {
            _labelMagDir = new LabelField("No data yet.");
            add(_labelMagDir);
        }
        
        void displayData(double direction)
        {
            _labelMagDir.setText(Double.toString(direction));
        } 
    }

Was this information helpful? Send us your comments.