我正在尝试将传感器API用于测试手机(Nexus S)上的加速度计。随着电话坐在板凳上什么都不做,我得到一些非常奇怪的读数(附图)。还有其他人看到过这种行为吗?Glitches in accel reading?知道怎么处理吗?
package com.example.m6;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Calendar;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
//import android.widget.Toast;
public class HelloAndroidActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private TextView tv;
private float accel[] = {0,0,0};
private float gyro[] = {0,0,0};
InetAddress addr;
int port = 1337;
DatagramSocket s = null;
String out = "";
PowerManager.WakeLock wl;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try{
//Lock the screen
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Set up the output screen
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textview);
tv.setBackgroundColor(Color.BLACK);
tv.setTextColor(Color.GREEN);
//Set up the telemetry feed
addr = InetAddress.getByName("192.168.0.12");
s = new DatagramSocket();
//Turning off the power management!!
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
wl.acquire();
//Set up the sensors
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
out += "Done\n";
tv.setText(out);
} catch (Exception e) {
out += "\nInit Error:" + e.getMessage() + "\n";
tv.setText(out);
}
}
private static final float NS2S = 1.0f / 1000000000.0f;
private float timestamp;
int down = 0;
float tstart = 0;
@Override
public void onSensorChanged(SensorEvent event) {
String type = "";
float dT = 0;
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
if (timestamp != 0) {
accel = event.values;
dT = (event.timestamp - timestamp);
tstart += dT;
}
timestamp = event.timestamp;
type = "G";
}
if(event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
if (timestamp != 0) {
gyro = event.values;
dT = (event.timestamp - timestamp);
tstart += dT;
}
timestamp = event.timestamp;
type = "A";
}
String out_packet = tstart / 1000 / 1000 + " "+ type + " " + dT + " " + accel[0] + " " + accel[1] + " " + accel[2] + " " + gyro[0] + " " + gyro[1] + " " + gyro[2] + "\n\0";
try{
DatagramPacket p = new DatagramPacket(out_packet.getBytes(),out_packet.length(), addr,port);
s.send(p);
}
catch (Exception e) {
out += "Packet send error: " + e.getMessage() + "\n";
}
tv.setText(timestamp + "\n" + out);
}
@Override
protected void onResume() {
super.onResume();
out += "on resume\n";
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL);
tv.setText(out);
}
@Override
protected void onPause() {
out += "on pause";
sensorManager.unregisterListener(this);
tv.setText(out);
super.onStop();
}
@Override
protected void onDestroy() {
out += "on destroy";
wl.release();
tv.setText(out);
super.onDestroy();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
代码会很好..试试这个演示代码。我知道它有效并制作图表。
http://www.5laida.com/docs/android/samples/Sensors.html
我已经在几个设备上测试了这个代码..所以,如果它显示相同的故障,那么它可能是你的设备。
答案 1 :(得分:0)
我已经知道发生了什么。我需要包括这一行:
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE ){
return;
}
然而,通过这样做,我现在没有从陀螺仪中获得任何值。嗯...