【发布时间】:2013-12-02 10:16:34
【问题描述】:
我尝试了this link的陀螺仪代码
问题是只有当传感器是加速度计时它才会显示值。 我尝试使用
显示所有传感器List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : sensors) {
Log.d("Sensors", "" + sensor.getName());
}
我得到了结果
12-02 15:44:20.816: D/Sensors(15757): AMI304 3-axis Magnetic Field sensor
12-02 15:44:20.816: D/Sensors(15757): ADXL345 3-axis Accelerometer
12-02 15:44:20.817: D/Sensors(15757): bh1620fvc Light Sensor
12-02 15:44:20.817: D/Sensors(15757): MPU3000 gyroscope Sensor
12-02 15:44:20.817: D/Sensors(15757): Rotation Vector Sensor
12-02 15:44:20.817: D/Sensors(15757): Gravity Sensor
12-02 15:44:20.817: D/Sensors(15757): Linear Acceleration Sensor
12-02 15:44:20.817: D/Sensors(15757): Orientation Sensor
12-02 15:44:20.817: D/Sensors(15757): Corrected Gyroscope Sensor
所以陀螺仪就在那里。那为什么加速度计只显示值? 请帮助我
编辑:
我正在使用带有 android 4.0.4 的 HCL ME Y3 平板电脑
代码
package fortyonepost.com.ag;//Created by: DimasTheDriver on Apr/27/2010. Available at:
http://www.41post.com/?p=3745
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;
public class AccessGyroscope extends Activity implements SensorEventListener
{
//a TextView
private TextView tv;
//the Sensor Manager
private SensorManager sManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the TextView from the layout file
tv = (TextView) findViewById(R.id.tv);
//get a hook to the sensor service
sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
* callbacks defined in this class, and gather the sensor information as
* quick as possible*/
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
}
//When this Activity isn't visible anymore
@Override
protected void onStop()
{
//unregister the sensor listener
sManager.unregisterListener(this);
super.onStop();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing
}
@Override
public void onSensorChanged(SensorEvent event)
{
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
//else it will output the Roll, Pitch and Yawn values
tv.setText("Orientation X (Roll) :"+ Float.toString(event.values[2]) +"\n"+
"Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
"Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
}
}
编辑 2
我在 Galaxy 标签中尝试了相同的代码。它正在工作。但我希望它也能在这里工作。
函数onSensorChanged 只为Acceleromter 调用。它没有被任何其他传感器调用。为什么?
我是否遗漏了平板电脑设置中的任何内容?需要做些什么来启用陀螺仪?
【问题讨论】:
-
您是否下载了项目并按原样运行,或者您是否修改了源代码?如果是这样,请发布您的代码。这个项目在我的手机上运行良好。
-
我没有修改就运行它。但没有读数。
-
也许操作系统在内核级别看不到硬件?
-
无论如何请发布您的代码,那里一定有问题。另外,你在什么手机上测试它?
-
在 onSensorChanged 中记录您的
event.sensor.getType(),看看您收到了哪种传感器。也可能发生这种情况,因为它返回的准确性不可靠。请先校准陀螺仪,然后重试。