Android Question Timestamp in SensorData

gezueb

Active Member
Licensed User
Longtime User
Hi,
I would like to use gyro data for an artificial horizon. However, I have not found a way to get the timestamp in nanoseconds from the the Sensor_Sensordata Event in the published Example "sensors". Library used is phone 2.0
Help would be appreciated, Thanks and regards
Georg
 

daemon

Active Member
Licensed User
Longtime User
In the app I'm developing, I need accurate timestamp for sensor events. How involved will it be to add timestamp argument to SensorChanged event of PhoneSensors?

I know it will break compatibility for existing users, how can such change be handled?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can compile a new library.

This is the updated code:
B4X:
  @ShortName("PhoneSensors2")
   @Events(values={"SensorChanged (Values() As Float, TimeStamp As Long)"})
   public static class PhoneSensors{
     /**
      * Single value - Ambient light level measured in SI lux units.
      */
     public static int TYPE_LIGHT = Sensor.TYPE_LIGHT;
     /**
      * Three values - Ambient magnetic field measured in micro-Tesla for the X, Y and Z axis.
      */
     public static int TYPE_MAGNETIC_FIELD = Sensor.TYPE_MAGNETIC_FIELD;
     /**
      * Single value - Atmospheric pressure.
      */
     public static int TYPE_PRESSURE = Sensor.TYPE_PRESSURE;
     /**
      * Single value - Proximity measured in centimeters. Most devices will return only two possible values representing "near" and "far".
      *"far" should match MaxRange and "near" should be a value smaller than MaxRange.
      */
     public static int TYPE_PROXIMITY = Sensor.TYPE_PROXIMITY;
     /**
      * Single value - Ambient temperature.
      */
     public static int TYPE_TEMPERATURE = Sensor.TYPE_TEMPERATURE;
     /**
      * Three values - Angular velocity measured in Radians / Second around each of the three axis.
      */
     public static int TYPE_GYROSCOPE = Sensor.TYPE_GYROSCOPE;
     /**
      * Three values - Acceleration measured in Meters / Second ^ 2 for each axis (X, Y and Z).
      */
     public static int TYPE_ACCELEROMETER = Sensor.TYPE_ACCELEROMETER;
     /**
      * Three values - Orientation measured in degrees for azimuth, pitch and roll.
      */
     public static int TYPE_ORIENTATION = Sensor.TYPE_ORIENTATION;
     private int currentType;
     private int sensorDelay;
     private SensorEventListener listener;
     /**
      * Initializes the object and sets the sensor type (one of the TYPE_ constants).
      */
     public void Initialize(int SensorType) {
       Initialize2(SensorType, 3);
     }
     /**
      * Initializes the object and sets the sensor type and sensor events rate.
      *SensorType - One of the TYPE_ constants.
      *SensorDelay - A value between 0 (fastest rate) to 3 (slowest rate). This is only a hint to the system.
      */
     public void Initialize2(int SensorType, int SensorDelay) {
       this.currentType = SensorType;
       this.sensorDelay = SensorDelay;
     }
     /**
      * Starts listening for sensor events.
      *Returns True if the sensor is supported.
      *Usually you will want to start listening in Sub Activity_Resume and stop listening in Sub Activity_Pause.
      */
     public boolean StartListening(final BA ba, String EventName) {
       SensorManager sm = (SensorManager) BA.applicationContext.getSystemService(Context.SENSOR_SERVICE);
       final String s = EventName.toLowerCase(BA.cul) + "_sensorchanged";
       listener = new SensorEventListener() {

         @Override
         public void onAccuracyChanged(Sensor sensor, int accuracy) {
           //
         }

         @Override
         public void onSensorChanged(SensorEvent event) {
           ba.raiseEvent(PhoneSensors.this, s, event.values, event.timestamp);
         }

       };
       return sm.registerListener(listener, sm.getDefaultSensor(currentType), sensorDelay);
     }
     /**
      * Stops listening for events.
      */
     public void StopListening(BA ba) {
       if (listener != null) {
         SensorManager sm = (SensorManager) ba.context.getSystemService(Context.SENSOR_SERVICE);
         sm.unregisterListener(listener);
       }
     }
     /**
      * Returns the maximum value for this sensor.
      *Returns -1 if this sensor is not supported.
      */
     public float getMaxValue() {
       java.util.List<Sensor> l = ((SensorManager) BA.applicationContext.getSystemService(Context.SENSOR_SERVICE)).getSensorList(currentType);
       if (l == null || l.size() == 0)
         return -1;
       return l.get(0).getMaximumRange();
     }


   }
 
Upvote 0

daemon

Active Member
Licensed User
Longtime User
Thanks for quick solution!

I have attached library for others to use...
 

Attachments

  • PhoneSensors2.zip
    5.6 KB · Views: 185
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
This lib has saved my day! The rise of the sensor event frequency is spectacular and allows much better low pass filtering. Thank you! Georg
 
Upvote 0
Top