Check if BlackBerry device has a magnetometer

  1. Import the MagnetometerSensor class.
    import net.rim.device.api.system.MagnetometerSensor;
  2. Call the static isSupported() method and check the returned boolean value.
    if(MagnetometerSensor.isSupported())

Code sample: Check if device has a magnetometer

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.system.MagnetometerSensor;

public class MagCheck extends UiApplication
{ 
   public static void main(String[] args)
	  { 
		     MagCheck app = new MagCheck(); 
		     app.enterEventDispatcher(); 
	  } 

	  public MagCheck() 
	  { 
		     pushScreen(new OutputScreen()); 
	  }
}

class OutputScreen extends MainScreen
{ 
	  LabelField _labelMsg = new LabelField("Not set yet"); 

	  public OutputScreen() 
	  { 
		     if(MagnetometerSensor.isSupported()) 
		     { 
			        _labelMsg.setText("Magnetometer is supported.");
		     }
		     else
		     {
			        _labelMsg.setText("Magnetometer is not supported.");
		     }
		     add(_labelMsg);
	  }
}

Was this information helpful? Send us your comments.