I can't get the ambient temperature on my phone

67 Views Asked by At

I am building an app which shoud display all the values from all the sensors including the TYPE_AMBIENT_TEMPERATURE sensor. I am using the SensorManager class.

I have written the code and tested it on the Emulator and it worked. But if I run the app on my phone (Galaxy A20e) it did not worked. It seems to not enter the onSensorChanged method. I have placed logs in the code and so I know, that the sensor is initialised an is activated. I have also installed the Thermometer-App and the App is working fine. So the physical Sensor is working. Does anyone know what is wrong with the code? My Code is the folowing:

MainActivity

package com.blueapps.sensorstation;

import android.content.Context;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.blueapps.sensorstation.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    private setOnClickListener Listener;

    private StartSensors startSensors;

    private SensorManager sensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // init View
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);


        // adapt the Layout to the Screen
        adaptLayout layoutAdapter = new adaptLayout(this.getWindowManager(), getResources(), binding);
        layoutAdapter.adaptView();

        // set OnCLickListener for components
        Listener = new setOnClickListener(getApplicationContext(), view, binding);
        Listener.setListener();


        // Get the Sensor Manager
        sensorManager = ( SensorManager) getSystemService(Context.SENSOR_SERVICE);

        // init the start Sensors Class
        startSensors = new StartSensors(getResources(), sensorManager, binding);

    }

    @Override
    protected void onPause() {
        super.onPause();
        // Deactivate Sensors
        startSensors.deactivate();

        // Give the On Click Listener the information about the onPause Call
        Listener.Pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Activate Sensors
        startSensors.activate();

        // Give the On Click Listener the information about the onResume Call
        Listener.Resume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Give the On Click Listener the information about the onPause Call
        Listener.Pause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        // Give the On Click Listener the information about the onResume Call
        Listener.Resume();
    }
}

StartSensors

package com.blueapps.sensorstation;

import android.content.res.Resources;
import android.hardware.SensorManager;
import android.widget.TextView;

import com.blueapps.sensorstation.databinding.ActivityMainBinding;
import com.blueapps.sensorstation.sensors.Temperature;

public class StartSensors {

    private SensorManager sensorManager;

    private Resources resources;

    // Sensor Classes
    Temperature temperature;


    // TextViews
    private TextView textTemp;


    public StartSensors(Resources resources, SensorManager sensorManager, ActivityMainBinding binding){

        this.sensorManager = sensorManager;
        this.resources = resources;

        // Bind the TextViews
        textTemp = binding.temperatureText;

        // Instance Sensor Classes
        temperature = new Temperature(resources, sensorManager, textTemp);

    }

    public void activate(){

        // Temperature
        temperature.activate();

    }

    public void deactivate(){

        // Temperature
        temperature.deactivate();

    }

}

Temperature

package com.blueapps.sensorstation.sensors;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.widget.TextView;

import com.blueapps.sensorstation.R;

public class Temperature implements SensorEventListener {

    private SensorManager sensorManager;

    private TextView text;

    private Resources resources;

    private Sensor temperature;

    public Temperature(Resources resources, SensorManager sensorManager, TextView text){

        this.sensorManager = sensorManager;
        this.text = text;
        this.resources = resources;

    }

    public void activate(){

        temperature = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
        sensorManager.registerListener(this, temperature, SensorManager.SENSOR_DELAY_UI);
        Log.d("startSensors", "Sensor Temperature is activated");

    }

    public void deactivate(){

        sensorManager.unregisterListener(this, temperature);
        Log.d("startSensors", "Sensor Temperature is deactivated");

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        double degree = sensorEvent.values[0];
        text.setText(String.format("%.1f", degree) + resources.getString(R.string.unit_temp));
        Log.d("startSensors", "Temperature is: " + String.format("%.1f", degree) + "°C");

    }

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

The other Classes that are mentioned (setOnClickListener, adaptLayout) are not relevant for the Sensors.

0

There are 0 best solutions below