使用磁传感器
我想获取手机传感器测量的磁场的三个坐标值。为此,我使用 sm=(SensorManager)getApplicationContext().getSystemService(Context.SENSOR_SERVICE)
获取 SensorManager
的句柄,然后使用 获取传感器cm=sm.getDefaultSensor(SensorManager.SENSOR_MAGNETIC_FIELD)
。然后,我使用 sm.registerListener(new SensorListener(),cm,SensorManager.SENSOR_DELAY_UI)
将 SensorEventListener
注册到 SensorManager
。
SensorListener
类是我自己实现 SensorEventListener
接口的类。在它的 OnSensorChanged 方法中,我从传感器获取值并显示它们。问题是我只得到值 1,0 和 0。而且它们很少更新(我在 onSensorChanged
调用上放置了一个计数器来查看更新发生的频率)。将时间更改为 SENSOR_DELAY_NORMAL
不会改善任何情况。
为了检查问题是否与磁传感器有关,我以同样的方式添加了加速度计传感器的侦听器。结果非常令人困惑:现在,磁传感器生成更新,但加速度计不生成更新。如果我删除加速度计传感器事件侦听器,我仍然会收到在添加加速度计传感器事件侦听器之前丢失的磁传感器事件。(????????????)
关于我的代码中有什么问题的任何想法?
I want to get the three coordinates values of the magnetic field measured by the sensor of my phone. For this, I get a handle to the SensorManager
by using sm=(SensorManager)getApplicationContext().getSystemService(Context.SENSOR_SERVICE)
, then get the sensor with cm=sm.getDefaultSensor(SensorManager.SENSOR_MAGNETIC_FIELD)
. I then register a SensorEventListener
to the SensorManager
with sm.registerListener(new SensorListener(),cm,SensorManager.SENSOR_DELAY_UI)
.
The classSensorListener
is a class of my own implementing the SensorEventListener
interface. In it's OnSensorChanged
method, I get the values from the sensor and I display them. The problem is that I only get the values 1,0 and 0. And they are rarely updated (I have put a counter on the onSensorChanged
calls to see how often the update takes place). Changing the time to SENSOR_DELAY_NORMAL
doesnot improve anything.
To check if the problem was related to the magnetic sensor, I have added, in the same way, a listener to an accelerometer sensor. The result is very confusing : now, the magnetic sensor generates updates, but not the accelerometer one. And if I remove the accelerometer sensor event listener, I still receive the magnetic sensor events which where missing before adding the accelerometer sensor event listener.(???????????)
Any idea about what is wrong in my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将其放在一起,它就可以在我的手机(HTC Desire,2.2)上正常工作,请检查您的手机是否遇到此问题...在这种情况下,您的设备可能有问题。
您的布局文件将需要三个 TextView:valMag_X、valMag_Y、valMag_Z。
Just put this together and it works fine on my handset (HTC Desire, 2.2), please check if you face an issue on your phone with this... in which case there may be a problem with your device.
Your layout file will need three TextViews, valMag_X, valMag_Y, valMag_Z.