I need to send post request with change in sensor value . It is sending too many requests as the method is called with every change in timestamp(nanosecond). I need to send a request only in change of sensor value. I want to compare current event.value with 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);
}
}
}
Consider using Sensor Batching, more and more smartphones are supporting it. Basically the events are stored in a queue in hardware and periodically are sent to your application all at once. There, you can compare all the results in a single call.
The only line in your code you have to change is:
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST);
adding a fourth parameter you define:
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST, max_report_latency_ns);
Where this max_report_value is:
When max_report_latency_ns>0, sensor events do not need to be reported as soon as they are detected. They can be temporarily stored in the hardware FIFO and reported in batches, as long as no event is delayed by more than max_report_latency_ns nanoseconds. That is, all events since the previous batch are recorded and returned at once. This reduces the amount of interrupts sent to the SoC and allows the SoC to switch to a lower power mode (idle) while the sensor is capturing and batching data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With