Listen for magnetometer data

  1. Import the required classes and interfaces.
    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.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 4, represents the custom screen.

    public class MagListener extends UiApplication 
    {
        private DataScreen _scrData;
        
        public static void main(String[] args)
        {
            MagListener app = new MagListener();
            app.enterEventDispatcher();
        }
        
        public MagListener()
        {
            _scrData = new DataScreen();
            pushScreen(_scrData);
        }
        
    }
  3. Indicate that the application class implements the MagnetometerListener interface. Provide an implementation of the onData() method as specified by the interface. Invoke displayData() to display the magnetometer data on the screen. In the application constructor, invoke openChannel() to create a Channel object that provides a bridge between your application and the magnetometer over which data can flow. Invoke addMagnetometerListener() to add your application class as a magnetometer listener, which causes onData() to be called periodically with the latest MagnetometerData objects.

    public class MagListener extends UiApplication 
    implements MagnetometerListener
    {
        private DataScreen _scrData;
        
        public static void main(String[] args)
        {
            MagListener app = new MagListener();
            app.enterEventDispatcher();
        }
        
        public MagListener()
        {
            _scrData = new DataScreen();
            pushScreen(_scrData);
    
            Channel  magChannel = MagnetometerSensor.openChannel(this);
            magChannel.addMagnetometerListener(this);
        }
    
        public void onData(MagnetometerData magData)
        {
            _scrData.displayData(magData);
        }
        
    }
  4. 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 retrieve the magnetic field strength from incoming MagnetometerData objects and display the result by setting the text of the LabelField.
    class DataScreen extends MainScreen
    {
        LabelField _labelMagStrength;
        
        public DataScreen()
        {
            _labelMagStrength = new LabelField("No data yet.");
            add(_labelMagStrength);
        }
        
        void displayData(MagnetometerData magData)
        {
            _labelMagStrength.setText(Float.toString(magData.getFieldStrength()));
        }
        
    }

Was this information helpful? Send us your comments.