我尝试了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
所以陀螺仪在那里。那么为什么加速度计只显示值? 请帮助我
修改
我正在使用HCL ME Y3平板电脑和android 4.0.4
代码
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标签中尝试了相同的代码。它正在发挥作用。但我希望它也能在这里工作。
仅为Acceleromter调用函数onSensorChanged
。它没有被要求任何其他传感器。为什么?
我是否在平板电脑设置中遗漏了任何内容?要启用gyrosocpe需要做什么?
答案 0 :(得分:0)
您的代码看起来很好。唯一想到的是,在onSensorChanged中,event.accuracy在您的设备上始终是不可靠的。尝试添加Log.d(&#34; MYTAG&#34;,&#34;准确度不可靠&#34;)以确定是否属于这种情况:
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
Log.d("MYTAG", "Accuracy unreliable");
return;
}
另请注意,从API级别8开始,不推荐使用Sensor.TYPE_ORIENTATION。请改用SensorManager.getOrientation()或Sensor.TYPE_GYROSCOPE。查看文档here。