重力传感器监听器不工作
Posted
技术标签:
【中文标题】重力传感器监听器不工作【英文标题】:Gravity sensor listener not working 【发布时间】:2017-10-12 19:44:23 【问题描述】:我想通过服务获取地球轴上的加速度计传感器数据。我在 *** 上的一篇文章中也读到了一种方法。但我有一个问题。
@Override
public void onSensorChanged(SensorEvent event)
Toast.makeText(this, "Hollyy shiiitt",Toast.LENGTH_SHORT);
String acceldata = "";
float[] gravityValues = null;
float[] magneticValues = null;
if (event.sensor.getType() == Sensor.TYPE_GRAVITY)
gravityValues = event.values.clone();
Toast.makeText(this, "The gra data is " + String.valueOf(gravityValues), Toast.LENGTH_SHORT);
if ((gravityValues != null) && (magneticValues != null)
&& (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER))
// float for use in conversion device relative acceleration to earth axis acceleration
float[] deviceRelativeAcceleration = new float[4];
deviceRelativeAcceleration[0] = event.values[0];
deviceRelativeAcceleration[1] = event.values[1];
deviceRelativeAcceleration[2] = event.values[2];
deviceRelativeAcceleration[3] = 0;
// Change the device relative acceleration values to earth relative values
// X axis -> East
// Y axis -> North Pole
// Z axis -> Sky
float[] R = new float[16], I = new float[16], earthAcc = new float[16];
SensorManager.getRotationMatrix(R, I, gravityValues, magneticValues);
float[] inv = new float[16];
android.opengl.Matrix.invertM(inv, 0, R, 0);
android.opengl.Matrix.multiplyMV(earthAcc, 0, inv, 0, deviceRelativeAcceleration, 0);
acceldata = String.valueOf(lat) + "," + String.valueOf(lon) + "," + String.valueOf(speed)+","+ String.valueOf(earthAcc[0]) + "," + String.valueOf(earthAcc[1]) + "," + String.valueOf(earthAcc[2])+ "," + String.valueOf(event.values[0])+ "," + String.valueOf(event.values[1])+ "," + String.valueOf(event.values[2]);
//SDK version check
if(Build.VERSION.SDK_INT > 18)
File file;
FileWriter filew; // Internal Storage writing
try
file = new File(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) + "/" + filename); //Android 4.4 and above
filew = new FileWriter(file, true); //true for append
filew.write(acceldata + String.valueOf(gravityValues) + String.valueOf(magneticValues) + "\n");
filew.close();
catch (Exception e)
e.printStackTrace();
else
String data = acceldata + "\n" ;
try
FileOutputStream fOut = openFileOutput("/" + filename,Context.MODE_APPEND);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
catch (IOException e)
e.printStackTrace();
Toast.makeText(getBaseContext(),"file not saved API < 19",Toast.LENGTH_SHORT).show();
位置发生变化的重力传感器的 if 语句不返回任何值,因此下一个语句也不起作用,因为重力值具有空值。
注册的传感器是
mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetsensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mGravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
然后他们被注册为
int SamplingFrequency = 2 * 100000; // delay in microsecond. in this case 0.2 second
sensorManager.registerListener(this, mAccelerometer,SamplingFrequency);
sensorManager.registerListener(this, mMagnetsensor, SamplingFrequency);
sensorManager.registerListener(this,mGravity,SamplingFrequency);
我是应用程序开发的新手,因此我们将不胜感激。
【问题讨论】:
【参考方案1】:您的设备可能没有重力传感器。检查它是否可用
if (mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null)
Toast.makeText(ActivitySensor1Availability.this, "Gravity AVAILABLE", Toast.LENGTH_SHORT).show();
else
// Failure! No Gravity Sensor.
Toast.makeText(ActivitySensor1Availability.this, "Failure! No Gravity Sensor", Toast.LENGTH_SHORT).show();
【讨论】:
当我在我的应用程序中尝试此代码时,屏幕上会出现 Gravity AVAILIBLE toast。还有其他建议吗? 是的,不做任何复杂的事情,检查你是否从重力传感器if (event.sensor.getType() == Sensor.TYPE_GRAVITY) System.out.println("X: " + event.values[0]); System.out.println("Y: " + event.values[1]); System.out.println("Z: " + event.values[2]);
获取值
不确定,但是当我添加 Linear_Acceleration 传感器时,重力传感器也开始抛出值。
你怎么能不确定。如果您使用 sn-p 并获取 x、y 和 z 值,那么它没有任何问题,下一步是您忽略这些值的轨道。如果您是新手,请将代码剥离到核心并检查每个步骤。创建传感器管理器,从传感器管理器获取重力传感器,注册它 onResume 和 onSensorChanged 检查是否从重力传感器获取输出以上是关于重力传感器监听器不工作的主要内容,如果未能解决你的问题,请参考以下文章