Configure and retrieve magnetometer sampling frequency

The default magnetometer sensor sampling rate is 20Hz. You can specify a preferred sampling rate by creating a MagChannelConfig and specifying the sampling rate you want. You pass the MagChannelConfig as a parameter when you create the Channel. You have to balance the need for timely data against the increased power requirements of a higher sampling rate. There is no guarantee that you will get your suggested sampling rate.

  1. Retain a reference to the Channel object.
  2. Invoke Channel.getConfig() to retrieve the MagnetometerChannelConfig object.
  3. Invoke MagnetometerChannelConfig.getFrequency() to retrieve the magnetometer sampling frequency.

Code sample: Configure and display magnetometer sampling frequency

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.capability.DeviceCapability;
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 MagChannelConfig extends UiApplication
{  
    private Channel _magChannel;
    
    public static void main(String[] args)
    {
        MagChannelConfig app = new MagChannelConfig();
        app.enterEventDispatcher();
    }
    
    public MagChannelConfig()
    {
        int freq = 20; // (Hz)
        MagnetometerChannelConfig magChannelConfig = new MagnetometerChannelConfig(freq);
        _magChannel = MagnetometerSensor.openChannel(this,magChannelConfig);
        Dialog.alert(Integer.toString(_magChannel.getConfig().getFrequency()));
        _magChannel.close();
    }
}
Next topic: Code samples

Was this information helpful? Send us your comments.