Android needs to compare the current and previous event. onSensorChanged method values

I need to send a post request with a change in sensor value. It sends too many requests on method invocation with every timestamp (nanosecond) change. I only need to send a request when the sensor value changes. I want to compare the current event.value with the previous event.value

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

public class MainActivity extends Activity implements SensorEventListener{
    TextView metrics,post;
    private SensorManager sensorManager;
    private float timestamp;
    RequestQueue queue;
    //private Sensor sensor;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        metrics = (TextView) findViewById(R.id.metrics);
        post = (TextView) findViewById(R.id.post);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        //sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

    }

    protected void onResume()
    {
        super.onResume();
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST);
    }

    protected void onStop()
    {
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        //Do nothing.
    }

    public void onSensorChanged(SensorEvent event)
    {
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        {
            return;
        }

        metrics.setText("Orientation X (Roll) :"+ Float.toString(event.values[0]) +"\n"+
                "Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
                "Orientation Z (Yaw) :"+ Float.toString(event.values[2]));

        if ( event.values[0] != || event.values[1] != || event.values[2] != ) {

            RequestQueue queue = MySingleton.getInstance(this.getApplicationContext()).
                    getRequestQueue();

            String url ="http://10.46.2.179:8080/?X=" + event.values[0] + "&&Y=" + event.values[1] + "&&Z=" + event.values[2];

            StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //post.setText(response);
                    //Log.i("VOLLEY", response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //post.setText(error.toString());
                    //Log.e("VOLLEY", error.toString());
                }
            });

            MySingleton.getInstance(this).addToRequestQueue(stringRequest);
        }

    }
}

      

+3


source to share


2 answers


Do it in your onSensorChanged

private float last_x, last_y, last_z;
  @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            long curTime = System.currentTimeMillis();
            // only allow one update every 100ms.

            //  if ((curTime - lastUpdate) > 150) {


                x = 0;
                y = 0;
                z = 0;
                x = sensorEvent.values[0];
                y = sensorEvent.values[1];
                z = sensorEvent.values[2];

                float speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;

                    last_x = x;
                    last_y = y;
                    last_z = z;
                }
            }

      



last_x

. last_y

, last_z

will have your previous values. Hope it helps

0


source


Consider using batch dispensing , more and more smartphones support it. Basically, events are stored in a queue on the hardware and are periodically sent to your application at the same time. There you can compare all the results in one go.

The only line in the code that you have to change:

sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST);

      

adding the fourth parameter that you define:



sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST, max_report_latency_ns);

      

Where is this max_report_value:

When max_report_latency_ns> 0, sensor events do not need to be reported immediately after they are detected. They can be temporarily stored in the hardware FIFO and reported in packets if no event is delayed more than max_report_latency_ns nanoseconds. That is, all events starting from the previous batch are recorded and returned immediately. This reduces the number of interrupts sent to the SoC and allows the SoC to switch to low power (standby) mode while the sensor is capturing and batch processing data.

+1


source







All Articles