Android - SensorManager getOrientation 的奇怪行为
我需要检索手机的方向。目前我写了这个:
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()){
case Sensor.TYPE_MAGNETIC_FIELD:
this.mag_vals = event.values.clone();
this.sensorReady = true;
break;
case Sensor.TYPE_ACCELEROMETER:
this.acc_vals = event.values.clone();
break;
}
if (this.mag_vals != null && this.acc_vals != null && this.sensorReady) {
this.sensorReady = false;
float[] R = new float[ProjectConstants.SIZE_MATRIX];
float[] I = new float[ProjectConstants.SIZE_MATRIX];
SensorManager.getRotationMatrix(R, I, this.acc_vals, this.mag_vals);
SensorManager.getOrientation(R, this.actual_orientation);
...
如果我将手机放在平坦的表面上并在该表面上旋转它,此代码允许我获取手机的方向。
我不明白的是,为什么如果我向上移动手机 this.actual_orientation[0]
的值,即 zed 轴上的旋转,如[此处][1]所述,该值会增加虽然没有轮换。
有人知道会发生什么吗?
编辑
另一个奇怪的事情..
我在办公室尝试了我的应用程序,它有我之前描述的奇怪行为.. 我在同一个办公室(相同的情况)尝试了从市场上购买的指南针应用程序,它的行为与我的相同。当我向上移动手机时,值会持续变化。 我在同一个办公室(相同的情况)尝试了 iPhone 指南针,它没有那种奇怪的行为!
然后,当我到家时,我尝试了我的应用程序和 Android 手机的指南针应用程序,它们都工作了!即使我向上移动手机,该值也很稳定......
非常感谢。
[1]: http://developer.android. com/reference/android/hardware/SensorManager.html#getOrientation(float[], float[])
I need to retrieve the orientation of my phone. At the moment i wrote this :
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()){
case Sensor.TYPE_MAGNETIC_FIELD:
this.mag_vals = event.values.clone();
this.sensorReady = true;
break;
case Sensor.TYPE_ACCELEROMETER:
this.acc_vals = event.values.clone();
break;
}
if (this.mag_vals != null && this.acc_vals != null && this.sensorReady) {
this.sensorReady = false;
float[] R = new float[ProjectConstants.SIZE_MATRIX];
float[] I = new float[ProjectConstants.SIZE_MATRIX];
SensorManager.getRotationMatrix(R, I, this.acc_vals, this.mag_vals);
SensorManager.getOrientation(R, this.actual_orientation);
...
This code allows me to get the orientation of the phone if i leave the phone on a flat surface and i rotate it over the surface.
What i did not understand is why if i move the phone upwards the value of this.actual_orientation[0]
,which is the rotation on the zed axis as described [here][1], the value increases although there was no rotation.
Did someone know what happens?
EDIT
Another strange thing..
I tried my application in the office at work and it had the strange behaviour i described before..
I tried in the same office(same situation) a compass app that i took from the market and it has the same behaviour of mine..when i moved the phone upwards the value changed consistently..
I tried in the same office(same situation) the i-phone compass and it didn't have that strange behaviour!
Then when i arrived at home i tried both, my application and the compass app of my android phone, and they worked!!even if i move upwards the phone the value are stable...
Thanks a lot.
[1]: http://developer.android.com/reference/android/hardware/SensorManager.html#getOrientation(float[], float[])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设备获得的方向估计部分取决于局部磁场方向。在建筑物内、铁结构附近和电器附近,感测到的磁场可能会随着以英尺为单位的距离而显着变化。使用手持式磁力计或高斯计可以轻松观察到这种效应。
我怀疑这些扭曲是导致您的应用程序出现奇怪行为的原因。
The orientation estimate obtained by the device depends in part upon the local magnetic field direction. Within buildings, near iron structures and near electrical appliances the magnetic field sensed may vary significantly over distances measured in feet. This effect can be easily seen with a hand held magnetometer or Gauss-meter.
I suspect that these distortions are responsible for your applications strange behavior.
当您抬起它时,您会引入一个旋转角度,这会影响 getOrientation 中的返回值。旋转角度测量设备相对于正常位置的旋转量,即对于大多数设备来说,正常位置意味着纵向,那么如果手机处于横向方向,则旋转角度将为 90 或 -90。
因此,如果您想要正确的读数,则必须在调用 getOrientation 之前调用 remapCooperativeSystem。
When you lift it up you introduce a rotation angle and this affect the return values in getOrientation. The rotation angle measure the amount of rotation of your device from normal position, i.e. for most device where normal position means Portrait then if the phone is in landscape orientation, the rotation angle would be 90 or -90.
So if you want the correct reading you have to call remapCoordinateSystem before the call to getOrientation.