2017-05-13 8 views
2

私はサービスを通じて地軸にある加速度センサーデータを取得したいと思います。私はstackoverflowの投稿の1つでも同様にそれを行う方法について読んでいます。しかし、私には問題があります。重力センサーリスナーが動作しない

@Override 
public void onSensorChanged(SensorEvent event) { 
    Toast.makeText(this, "Hollyy shiiitt",Toast.LENGTH_SHORT); 
    String acceldata = ""; 

    float[] gravityValues = null; 
    float[] magneticValues = null; 

    if (event.sensor.getType() == Sensor.TYPE_GRAVITY) { 
     gravityValues = event.values.clone(); 
     Toast.makeText(this, "The gra data is " + String.valueOf(gravityValues), Toast.LENGTH_SHORT); 
    } 

    if ((gravityValues != null) && (magneticValues != null) 
      && (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)) { 

     // float for use in conversion device relative acceleration to earth axis acceleration 
     float[] deviceRelativeAcceleration = new float[4]; 
     deviceRelativeAcceleration[0] = event.values[0]; 
     deviceRelativeAcceleration[1] = event.values[1]; 
     deviceRelativeAcceleration[2] = event.values[2]; 
     deviceRelativeAcceleration[3] = 0; 

     // Change the device relative acceleration values to earth relative values 
     // X axis -> East 
     // Y axis -> North Pole 
     // Z axis -> Sky 

     float[] R = new float[16], I = new float[16], earthAcc = new float[16]; 

     SensorManager.getRotationMatrix(R, I, gravityValues, magneticValues); 

     float[] inv = new float[16]; 

     android.opengl.Matrix.invertM(inv, 0, R, 0); 
     android.opengl.Matrix.multiplyMV(earthAcc, 0, inv, 0, deviceRelativeAcceleration, 0); 

     acceldata = String.valueOf(lat) + "," + String.valueOf(lon) + "," + String.valueOf(speed)+","+ String.valueOf(earthAcc[0]) + "," + String.valueOf(earthAcc[1]) + "," + String.valueOf(earthAcc[2])+ "," + String.valueOf(event.values[0])+ "," + String.valueOf(event.values[1])+ "," + String.valueOf(event.values[2]); 

    } 

    //SDK version check 
    if(Build.VERSION.SDK_INT > 18) { 
     File file; 
     FileWriter filew; // Internal Storage writing 
     try { 
      file = new File(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) + "/" + filename); //Android 4.4 and above 
      filew = new FileWriter(file, true); //true for append 
      filew.write(acceldata + String.valueOf(gravityValues) + String.valueOf(magneticValues) +  "\n"); 
      filew.close(); 


     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
    } 
    else{ 
     String data = acceldata + "\n" ; 
     try { 
      FileOutputStream fOut = openFileOutput("/" + filename,Context.MODE_APPEND); 
      fOut.write(data.getBytes()); 
      fOut.close(); 
      Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Toast.makeText(getBaseContext(),"file not saved API < 19",Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

位置に重力センサのステートメントはgravityValuesにNULL値があるように、次の文も機能しないように任意の値を返さない変更された場合ザ。登録

センサーは、彼らはその後、

int SamplingFrequency = 2 * 100000; // delay in microsecond. in this case 0.2 second 
sensorManager.registerListener(this, mAccelerometer,SamplingFrequency); 
sensorManager.registerListener(this, mMagnetsensor, SamplingFrequency); 
sensorManager.registerListener(this,mGravity,SamplingFrequency); 

任意の助けをいただければ幸いですので、私はアプリ開発の初心者ですとして登録されている

mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
mMagnetsensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 
mGravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); 

です。

答えて

1

お使いのデバイスにはおそらく重力センサーがありません。使用可能かどうかを確認してください

if (mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null) { 
     Toast.makeText(ActivitySensor1Availability.this, "Gravity AVAILABLE", Toast.LENGTH_SHORT).show(); 
    } else { 
     // Failure! No Gravity Sensor. 
     Toast.makeText(ActivitySensor1Availability.this, "Failure! No Gravity Sensor", Toast.LENGTH_SHORT).show(); 
    } 
+0

アプリケーションでこのコードを試してみると、AVAILIBLEトーストが画面に表示されます。その他の提案はありますか? –

+0

あなたは '重力センサー から値を取得している場合は、[はい、複雑な何もせずに、チェックした場合(event.sensor.getType()== Sensor.TYPE_GRAVITY){ \t \t \tのSystem.out.println( "X:" + event.values [0]); \t \t \t System.out.println( "Y:" + event.values [1]); \t \t \t System.out.println( "Z:" + event.values [2]); \t \t \t} – Thracian

+0

わかりませんが、Linear_Accelerationセンサーを追加したとき、重力センサーが値をスローアップし始めました。 –

関連する問題