B4i Library [CLASS] XPhoneSensor - Motion object as per B4A

This class allows you to easily port to B4i a B4A app that uses the B4A PhoneSensors object with types TYPE_ACCELEROMETER, TYPE_MAGNETIC_FIELD and TYPE_GYROSCOPE.

"Out of the box" the Motion object of the iPhone library supplies:
  • Uncalibrated magnetometer (GetMagnetometer) - this is not the same as the B4A TYPE_MAGNETIC_FIELD which is calibrated (i.e. corrected for device bias) - these calibration corrections can be large.
  • User acceleration (GetUserAcceleration) - this is not the same as B4A TYPE_ACCELEROMETER which is user acceleration + gravity.
This class corrects these deficiencies and adds:
  • Gyroscope.
  • Calibrated magnetometer calibration accuracy - in this class this property can be 0 (unreliable/uncalibrated) , 1 (low), 2 (medium) or 3 (high). It is essential that this be 3 (high) before reliance is put on the calibrated magnetometer values.
The class is supplied encapsulated in an example app which shows as follows:

Line 1: current total acceleration readings along device X Y Z axes
Line 2: max total acceleration
Line 3: min total acceleration
Line 4: current gyroscope readings about device X Y Z axes
Line 5: calibrated magnetometer calibration accuracy (if not 3, wave device around in a figure 8 pattern while slowly turning through 360 deg)
Line 6: current calibrated magnetometer readings along device X Y Z axes
Line 7: current magnetometer total intensity (should remain fairly constant regardless of device orientation)

  • Gravity comes from this post:

from which I was able to work out how to get gyroscope.​
  • Calibrated magnetometer comes from this post:

from which I was able to work out how to get calibrated magnetometer calibration accuracy.​


  • MotionAsPerB4A.zip
    6.7 KB · Views: 30
Last edited: