/**
* The PhoneSensors object allows you to listen for changes in one of the device sensors.
*See the <link>Sensors example|http://www.b4x.com/forum/basic4android-getting-started-tutorials/6647-orientation-accelerometer.html</link>.
*Most devices do not support all sensors. The StartListening method returns False if the sensor is not supported.
*After initializing the object and calling StartListening, the SensorChanged event will be raised each time the sensor value changes.
*The value is passed as an array of Floats. Some sensors pass a single value and some pass three values.
*/
@ShortName("PhoneSensors")
@Events(values={"SensorChanged (Values() As Float)"})
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);
}
};
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();
}
}