This article translated from: http://developer.android.com/guide/topics/sensors/sensors_environment.html#sensors-using-humid
The Android platform provides four sensors that monitor various environmental attributes. These sensors can be used to monitor air humidity, illuminance, atmospheric pressure, and temperature in the vicinity of Android devices. All four environment sensors are hardware-based and are only valid when the manufacturer embeds them into the device. Most manufacturers use light sensors to control screen brightness. Other environmental sensors are not always effective on all devices. For this reason, it is particularly important to confirm whether the corresponding environment sensor exists before obtaining environment data from the visual view during runtime.
Unlike the multi-dimensional arrays that use each sensorevent parameter to return sensor values, the Environment sensor returns a single sensor value with each data event. For example, the temperature in the unit of °C and the pressure in the unit of HPA. Unlike motion sensors and position sensors, it often requires high-pass or low-pass filtering. Typical environmental sensors do not need any data filtering or data processing. Table 1 provides an overview of Environment sensors supported by the Android platform:
Table 1. Environment sensors supported by the Android platform
Sensor |
Sensor event data |
Measurement Unit |
Data Introduction |
Type_ambient_temperature |
event.values[0]
|
°C |
Ambient Air Temperature |
Type_light |
event.values[0]
|
Lx |
Illuminance |
Type_pressure |
event.values[0]
|
HPA or mbar |
Ambient Air Pressure |
Type_relative_humidity |
event.values[0]
|
% |
Surrounding relative humidity |
Type_temperature |
event.values[0]
|
°C |
Device temperature. 1 |
. 1. This implementation varies with devices and is discarded in android4.0 (API Level 14.
Use light, pressure, and temperature sensors
Typically, raw data obtained from light, pressure, and temperature sensors does not require correction, filtering, or correction, making them the easiest sensors to use. First, create a sensormanager class instance to obtain data. You can use the sensormanager class instance to obtain a physical sensor instance. Register a sensor listener in the onresume () method and start to process the input sensor data in the onsensorchanged () callback method. The following code shows how to do this:
Public class sens?ti=extends activity implements sensoreventlistener {
Private sensormanager msensormanager;
Private sensor mpressure;
@ Override
Public final void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Get an instance of the sensor service, and use that
To get an instance
// A participant sensor.
Msensormanager = (sensormanager) getsystemservice (context. sensor_service );
Mpressure = msensormanager. getdefasensensor (sensor. type_pressure );
}
@ Override
Public final void onaccuracychanged (sensor, int accuracy ){
// Do something here if sensor accuracy changes.
}
@ Override
Public final void onsensorchanged (sensorevent event ){
Float millibars_of_pressure = event. Values [0];
// Do something with this sensor data.
}
@ Override
Protected void onresume (){
// Register a listener for the sensor.
Super. onresume ();
Msensormanager. registerlistener (this, mpressure, sensormanager. sensor_delay_normal );
}
@ Override
Protected void onpause (){
// Be sure to unregister the sensor when the activity pauses.
Super. onpause ();
Msensormanager. unregisterlistener (this );
}
}
You must first include the implementation of onaccuracychanged () and onsensorchanged () callback methods. Also, always ensure that the sensor is deregistered when the activity is suspended, which will prevent continuous sensing data and reduce battery consumption.
Use a Humidity Sensor
Like using light, pressure, and temperature sensors, you can use humidity sensors to obtain raw relative humidity data. However, if a device has both a humidity sensor (type_relative_humidity) and a temperature sensor (type_ambient_temperature), you can use these two data streams to calculate the dew point and absolute humidity.
Dew Point
The dew point is the temperature at which a given volume of gas is converted from a gas to water under constant atmospheric pressure. The following code shows how to calculate the dew point:
ln(RH/100%) + m·t/(Tn+t)
td(t,RH) = Tn · ------------------------------------
m - [ln(RH/100%) + m·t/(Tn+t)]
Where
1. TD = dew point temperature, unit: degree Celsius;
2. t = actual temperature, unit: degree Celsius;
3. RH = actual relative humidity, percentage;
4. M = 17.62
5. Tn = 243.12
Absolute humidity
Absolute humidity is the quality of water in the air of a given volume. The absolute humidity measurement unit is gram/cubic meter (grams/meter3). The following formula is used to calculate the absolute humidity:
(RH/100%) · A · exp(m·t/(Tn+t)
dv(t,RH) = 216.7 · ------------------------------------
273.15 + t
Where:
1. DV = absolute humidity, unit: Gram/cubic meter
2. t = actual temperature, unit: degree Celsius
3. RH = actual relative humidity, percentage
4. M = 17.62
5. Tn = 243.12 degrees Celsius
6. A = 6.20.hpa