// Cache if the device is a simulator or not
final boolean isSim = DeviceInfo.isSimulator();
// Add fields for displaying real time data
_streamingManager = new VerticalFieldManager();
_headingField = new TextField("Heading: ", "");
_strengthField = new TextField("Field strength: ", "");
_angleField = new TextField("Angle: ", "");
_quaternionField = new TextField("Quaternion: ", "");
_calibrationQualityField = new TextField("Calibration quality: ", "");
_streamingManager.add(_headingField);
_streamingManager.add(_angleField);
_streamingManager.add(_quaternionField);
_streamingManager.add(_strengthField);
_streamingManager.add(_calibrationQualityField);
_streamingManager.setPadding(4, 4, 4, 4);
add(_streamingManager);
// Add HorizontalFieldManager for buttons
final HorizontalFieldManager buttonManager =
new HorizontalFieldManager(FIELD_HCENTER);
_snapshotButton = new ButtonField("Snapshot");
_calibrateButton = new ButtonField("Calibrate");
_snapshotButton.setChangeListener(this);
_calibrateButton.setChangeListener(this);
buttonManager.add(_snapshotButton);
buttonManager.add(_calibrateButton);
add(buttonManager);
add(new SeparatorField());
// Add fields for displaying snapshot data
_snapshotManager = new VerticalFieldManager();
_snapshotHeadingField = new TextField("Snapshot heading: ", "");
_snapshotAngleField = new TextField("Snapshot angle: ", "");
_snapshotStrengthField = new TextField("Snapshot field strength: ", "");
_snapshotQualityField =
new TextField("Snapshot calibration quality: ", "");
_snapshotQuaternionField = new TextField("Snapshot quaternion: ", "");
_snapshotManager.add(_snapshotHeadingField);
_snapshotManager.add(_snapshotAngleField);
_snapshotManager.add(_snapshotQuaternionField);
_snapshotManager.add(_snapshotStrengthField);
_snapshotManager.add(_snapshotQualityField);
_snapshotManager.setPadding(4, 4, 4, 4);
add(_snapshotManager);
add(new SeparatorField());
// The magnetometer channel will be opened with a sampling
// frequency of 10 hertz and will be active only when the app
// is in the foreground. This is the default configuration
// that would be set automatically if the no arg constructor
// for MagnetometerChannelConfig was used.
final MagnetometerChannelConfig mcc =
new MagnetometerChannelConfig(10, true, false);
// Open up the magnetometer channel for reading data and
// set this class as a MagnetometerListener.
_magnetometerChannel = MagnetometerSensor.openChannel(app, mcc);
_magnetometerChannel.addMagnetometerListener(this);
// Cache the application for use later
_app = app;
// Start looking for the device's location and initialize
// the GeomagneticField if on a real device.
if (!isSim) {
// Add the declination fields only if on a real device
_declinationField = new TextField("Declination: ", "");
_streamingManager.add(_declinationField);
_snapshotDeclinationField =
new TextField("Snapshot declination: ", "");
_snapshotManager.add(_snapshotDeclinationField);
// Add field for displaying location status
_locationInfo = new TextField("Location status: ", "");
add(_locationInfo);
// Add HorizontalFieldManager for the location information
_locationManager = new HorizontalFieldManager(FIELD_HCENTER);
add(_locationManager);
_latitude = new TextField("Latitude : ", "");
_longitude = new TextField("Longitude : ", "");
_altitude = new TextField("Altitude : ", "");
// Initialize the GeomagneticField in a non-event thread
final Thread initializer = new Thread(new Runnable() {
public void run() {
getLocation();