Automatically exported from
Java Objective-C C#
Latest commit 23f3f11 Oct 14, 2016 @chen-fishbein committed on GitHub Merge pull request #1 from Cramps/master
Set sensor speed (sampling rate)
Failed to load latest commit information.
CN1Sensors Improved Oct 14, 2016


Sensors library for Codename One, implemented on Android and iOS.


Step 1. Get instance

Normal usage using default sensor speed.
SensorsManager gyroscope = SensorsManager.getSensorsManager(SensorsManager.TYPE_GYROSCOPE);
SensorsManager accelerometer = SensorsManager.getSensorsManager(SensorsManager.TYPE_ACCELEROMETER);
SensorsManager magnetic = SensorsManager.getSensorsManager(SensorsManager.TYPE_MAGNETIC);
Custom sensor speed (sampling rate)

You can also add your desired sensor speed (in microseconds) as a second parameter. For example, if you want to receive (approximately) 10 samples/second (10 hz) you would use 1000000/10 = 100000 microseconds, like this:

SensorsManager accelerometer = SensorsManager.getSensorsManager(SensorsManager.TYPE_ACCELEROMETER, 100000);

Step 2. Register listener (do stuff with sensor data)

if (accelerometer != null) {
    accelerometer.registerListener(new SensorListener() {
        public void onSensorChanged(long timeStamp, float x, float y, float z) {
            //do your stuff here...