Code sample: Retrieving information from magnetometer data

import java.lang.StringBuffer;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.system.MagnetometerListener;
import net.rim.device.api.system.MagnetometerChannelConfig;
import net.rim.device.api.system.MagnetometerData;
import net.rim.device.api.system.MagnetometerSensor;
import net.rim.device.api.system.MagnetometerSensor.*;


public class MagData extends UiApplication implements MagnetometerListener
{
    private DataScreen _scrData;
    
    public static void main(String[] args)
    {
        MagData app = new MagData();
        app.enterEventDispatcher();
    }
    
    public MagData()
    {
        _scrData = new DataScreen();
        pushScreen(_scrData);
        
        Channel  magChannel = MagnetometerSensor.openChannel(this);
        magChannel.addMagnetometerListener(this);
    }
    
    public void onData(MagnetometerData magData)
    {
        _scrData.displayData(magData);
    }
}


class DataScreen extends MainScreen
{
    TextField _txtMagData;
    
    public DataScreen()
    {
        _txtMagData = new TextField("","No data yet.");
        add(_txtMagData);
    }
    
    void displayData(MagnetometerData magData)
    {
        StringBuffer strBuf = new StringBuffer();
        
        strBuf.append("Field strength: ");
        strBuf.append(Float.toString(magData.getFieldStrength()));
        
        float fDirection = magData.getDirectionTop();
        strBuf.append("\nDirection (degrees): ");
        strBuf.append(Float.toString(fDirection));
        
        strBuf.append("\nInclination: ");
        strBuf.append(Float.toString(magData.getInclination()));
        
 
        switch(MagnetometerData.getHeading(fDirection))
        {
            case MagnetometerData.MAGNETOMETER_HEADING_NORTH:
                strBuf.append("N");
                break;
            case MagnetometerData.MAGNETOMETER_HEADING_EAST_NORTH_EAST:
                strBuf.append("NE");
                break;
            case MagnetometerData.MAGNETOMETER_HEADING_EAST:
                strBuf.append("E");
                break;
            case MagnetometerData.MAGNETOMETER_HEADING_SOUTH_EAST:
                strBuf.append("E");
                break;
            case MagnetometerData.MAGNETOMETER_HEADING_SOUTH:
                strBuf.append("S");
                break;
            case MagnetometerData.MAGNETOMETER_HEADING_SOUTH_WEST
                strBuf.append("SW");
                break;
            case MagnetometerData.MAGNETOMETER_HEADING_WEST:
                strBuf.append("W");
                break;
            case MagnetometerData.MAGNETOMETER_HEADING_NORTH_WEST:
                strBuf.append("NW");
                break;                
            default:
                strBuf.append("*");   
        }
        
        _txtMagData.setText(strBuf.toString());
    }
    
}

Was this information helpful? Send us your comments.