1

I created an application that reads data from various sensors. The problem is that in the case of the humidity and temperature sensor, no information is displayed. Other sensors work without any problems.

Properly working Light Sensor code:

public class LightFragment extends Fragment implements SensorEventListener {

TextView lightTV;
SensorManager sensorManager;
Sensor lightSensor;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_light, container, false);

    lightTV = (TextView) v.findViewById(R.id.LightValue);

    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

    lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

    sensorManager.registerListener(LightFragment.this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
}


@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor sensor = sensorEvent.sensor;
    if (sensor.getType() == Sensor.TYPE_RELATIVE_HUMIDITY) {

        lightTV.setText(String.format("%.2f", +(int) sensorEvent.values[0]) + "\t\t\t" + "[lux]");

    } else {

        lightTV.setText("Light Sensor not supported on this device");
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

Humidity Sensor Code:

public class HumiFragment extends Fragment implements SensorEventListener {

TextView humiTV;
SensorManager sensorManager;
Sensor humiditySensor;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_humi, container, false);

    humiTV = (TextView) v.findViewById(R.id.HumiValue);

    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

    humiditySensor = sensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY);

    sensorManager.registerListener(HumiFragment.this, humiditySensor, SensorManager.SENSOR_DELAY_NORMAL);
}


@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor sensor = sensorEvent.sensor;
    if (sensor.getType() == Sensor.TYPE_RELATIVE_HUMIDITY) {

        humiTV.setText(String.format("%.2f", +sensorEvent.values[0]) + "\t\t\t" + "[%]");

    } else {

        humiTV.setText("Humidity Sensor not supported on this device");
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

Temperature Sensor Code:

public class TempFragment extends Fragment implements SensorEventListener {

TextView tempTV;
SensorManager sensorManager;
Sensor tempSensor;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_temp, container, false);

    tempTV = (TextView) v.findViewById(R.id.TempValue);

    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

    tempSensor = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);

    sensorManager.registerListener(TempFragment.this, tempSensor, SensorManager.SENSOR_DELAY_NORMAL);
}


@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor sensor = sensorEvent.sensor;
    if (sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {

        tempTV.setText(String.format("%.4f", +sensorEvent.values[0]) + "\t\t\t" + "[°C]");

    } else {

        tempTV.setText("Temperature Sensor not supported on this device");
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

As you can see all codes are very similar but onSensorChanged only works in light sensor. I checked the applications on three different devices (Xiaomi, Samsung, Huawei) and on each these two specific sensors do not work. The onSensorChanged function should display information about the lack of support for the sensor, but it is not there. What is the reason the two sensors are not working and are not returning any information?

  • 1
    Weird guess, but do these specific devices even have humidity and temperature sensors? Which devices are you testing on? Also "_The onSensorChanged function **should** display information about the lack of support for the sensor, [...]_" I personally wouldn't bet on that. – Num Lock Aug 19 '20 at 06:03
  • I tested applications for the Huawei P10 lite, Samsung Galaxy S10 + and Xiaomi Redmi Note 5. In general, I wanted the function to display sensor values, if available, and information about the lack of support when the device is not equipped with a given sensor. Interestingly, in the case of a light sensor, the onSensorChanged function works properly, i.e. it prompts for no support for the sensor. Edit: I checked and none of these devices have a temperature and humidity sensor... – Mikołaj Nawrocki Aug 19 '20 at 06:29
  • Not sure about the other models, but the Xiaomi Redmi Note 5 definitely has no humidity nor temperature sensors. Well, internally there are sensors that monitor the battery and other hardware, but those are not accessible through any API AFAIK. I'd guess it's the same for the others, as I personally can't recall any mainstream phone that has these kind of sensors available to the user directly. – Num Lock Aug 19 '20 at 06:34
  • Ok. Thanks for help. Have a good day. – Mikołaj Nawrocki Aug 19 '20 at 06:36

1 Answers1

-1

The only commercially released devices with a Hygrometer (humidity sensor) and a thermometer are the Galaxy S4, and Note 3. Some CAT rugged devices have a thermometer.

Chrystian
  • 977
  • 3
  • 11
  • 24