简体   繁体   English

Android App中的旋转矢量传感器无法在我的Moto G上运行

[英]Rotation Vector Sensor in Android App not Working on my Moto G

So I have an app written in Android Eclipse that displays a map over a fixed marker and then moves around based on the change in rotation using a rotation vector sensor. 所以我有一个用Android Eclipse编写的应用程序,它在固定标记上显示一个地图,然后根据旋转变化使用旋转矢量传感器移动。 When I ran it on my device, the screen would not move when the phone was tilted. 当我在我的设备上运行时,屏幕在手机倾斜时不会移动。 I tried everything and even had my professor look at it. 我尝试了一切,甚至让我的教授看着它。 He could not find the problem in the code. 他在代码中找不到问题。 Finally we ran it on his device and it worked perfectly! 最后我们在他的设备上运行它,它完美地工作! I have a Moto G and I was wondering if anyone new why this code works on other android devices but not mine. 我有一个Moto G,我想知道是否有人为什么这个代码在其他Android设备上工作,但不是我的。 Below is the code for the project. 以下是该项目的代码。

package com.example.flymap_awc;

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.view.Menu;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity implements SensorEventListener {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

  private SensorManager mSensorManager;

  private double oldx = 0, oldy=0, oldz = 0;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();
    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
        .title("Hamburg"));
    Marker kiel = map.addMarker(new MarkerOptions()
        .position(KIEL)
        .title("Kiel")
        .snippet("Kiel is cool")
        .icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ic_launcher)));

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub


    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
        {

            if(oldx==0 && oldy ==0 && oldz ==0 ){
            // if this is the first run (oldx/y/z=0, get the initial orientation
                oldx = event.values[0];
                oldy = event.values[1];
                oldz =event.values[2];

               //set oldx to event.values[0], etc.
            }

            // get change in rotation amount store as dx,dy,dz
            double dx = oldx - event.values[0];
            double dy = oldy - event.values[1];
            double dz = oldz - event.values[2];
            double dTotal = Math.abs(dx) + Math.abs(dy) + Math.abs(dz);
            if (dTotal > 0.05)
            {
                double lat = map.getCameraPosition().target.latitude;
                double lng = map.getCameraPosition().target.longitude;

                lat +=dx;
                lng -=dy;

                Log.println(100,"100","lat, lng, dx, dy" + lat + lng+dx+dy);

                LatLng loc = new LatLng(lat, lng);
                map.moveCamera(CameraUpdateFactory.newLatLng(loc));

                oldx = event.values[0];
                oldy = event.values[1];
                oldz = event.values[2];

            }

            //filter out noise where Math.abs(dx) + Math.abs(dy)+ Math.abs(dz) > 0.05
            // get current lat lng
            //that my map camera is point at (or targeting)


            // add to the lat and lng based on dx dy
            //may want to compensate for zoom inverse proportionally

            //may have to do lng -=dy to move intuitively
            //move camera using camerupdatefactory to the new LatLng(lat,lng)

            // update old oldy oldz

        }
    }







protected void onResume(){
        super.onResume();
        // register this class as a listener for the orientation sensor
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
                SensorManager.SENSOR_DELAY_NORMAL);

    }
    protected void onPause(){
        super.onPause();
        //unregister listener
        mSensorManager.unregisterListener(this);

    }
} 

And here is the manifest code 这是清单代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flymap_awc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

 <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
         >

        <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.example.flymap_awc.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyDC4pK6_RmiSsDoWFAEmTUO2DRynTR0lik"/>
    </application>

</manifest>

ROTATION_VECTOR requires gyroscope to work, that's why. ROTATION_VECTOR要求陀螺仪工作,这就是原因。

Underlying base sensor(s): Accelerometer, Gyroscope AND Magnetometer 底层基础传感器:加速度计,陀螺仪和磁力计

One of my app stopped working too due to that, on the Moto G. 我的一个应用程序也因此停止工作,在Moto G上。

I just had a quick glance over your code. 我只是快速浏览一下你的代码。 I think the problem is that your code is assuming that the sensor is present and successfully registered. 我认为问题是你的代码假设传感器存在并成功注册。 In my limited experience, the sensor may be unavailable due to either not existing in the device or being faulty. 在我有限的经验中,由于设备中不存在或者有故障,传感器可能不可用。

Try doing the following checks and see what you get: 尝试进行以下检查,看看你得到了什么:

  • Ensure mSensorManager.registerListener returns true . 确保mSensorManager.registerListener返回true
  • Ensure mSensorManager.getDefaultSensor doesn't return null . 确保mSensorManager.getDefaultSensor不返回null
  • Ensure onSensorChanged gets called. 确保onSensorChanged You could put a breakpoint in it or add a line to write a log message. 您可以在其中放置断点或添加一行来写入日志消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM