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?