Java Code Examples for android.hardware.Sensor#TYPE_ACCELEROMETER

The following examples show how to use android.hardware.Sensor#TYPE_ACCELEROMETER . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HostDeviceOrientationProfile.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
private void processSensorData(final SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mAccellX = sensorEvent.values[0];
        mAccellY = sensorEvent.values[1];
        mAccellZ = sensorEvent.values[2];

        mIsAccellReady.compareAndSet(false, true);
    } else if (sensorEvent.sensor.getType() == Sensor.TYPE_GRAVITY) {
        mGravityX = sensorEvent.values[0];
        mGravityY = sensorEvent.values[1];
        mGravityZ = sensorEvent.values[2];

        mIsGravityReady.compareAndSet(false, true);
    } else if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        mGyroX = Math.toDegrees(sensorEvent.values[0]);
        mGyroY = Math.toDegrees(sensorEvent.values[1]);
        mGyroZ = Math.toDegrees(sensorEvent.values[2]);

        mIsGyroReady.compareAndSet(false, true);
    }
}
 
Example 2
Source File: ShakeHelper.java    From MyBlogDemo with Apache License 2.0 6 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    int type = event.sensor.getType();
    if (type == Sensor.TYPE_ACCELEROMETER) {//accelerometer
        //获取三个方向值
        float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];

        if (!dialog.isShowing() && (Math.abs(x) > 17 || Math.abs(y) > 17 || Math
                .abs(z) > 17)) {
            handleFragment();
        }
    }
}
 
Example 3
Source File: OrientationListener.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
/**
 * SensorEventListener method called when sensor values are updated. Reads gravitational and
 * magnetic field information, and when both are available computes the orientation values
 * and calls the delegate with them.
 */
@Override public void onSensorChanged(SensorEvent event) {
    switch(event.sensor.getType()) {
    case Sensor.TYPE_MAGNETIC_FIELD:
        mags = event.values.clone();
        break;
    case Sensor.TYPE_ACCELEROMETER:
        accels = event.values.clone();
        break;
    }

    if (mags!=null && accels!=null) {
        SensorManager.getRotationMatrix(R, I, accels, mags);
        SensorManager.getOrientation(R, orientationValues);
        delegate.receivedOrientationValues(
                orientationValues[0], orientationValues[1], orientationValues[2]);
    }
}
 
Example 4
Source File: WearAppService.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onSensorChanged(final SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        long time = System.currentTimeMillis();
        long interval = time - mStartTime;
        mStartTime = time;

        float accelX = sensorEvent.values[0];
        float accelY = sensorEvent.values[1];
        float accelZ = sensorEvent.values[2];
        final String data = accelX + "," + accelY + "," + accelZ
                + "," + mGyroX + "," + mGyroY + "," + mGyroZ + "," + interval;
        mExecutorService.execute(() -> {
            synchronized (mIds) {
                for (String id : mIds) {
                    sendSensorEvent(data, id);
                }
            }
        });
    } else if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        mGyroX = (float) (sensorEvent.values[0] * RAD2DEG);
        mGyroY = (float) (sensorEvent.values[1] * RAD2DEG);
        mGyroZ = (float) (sensorEvent.values[2] * RAD2DEG);
    }
}
 
Example 5
Source File: SynthCircle.java    From Circle-Synth with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {

	if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
		return;
	SharedPreferences getPrefs1 = PreferenceManager
			.getDefaultSharedPreferences(getBaseContext());
	accel = getPrefs1.getBoolean("accel", true);
	currentAccelx = lowPass(event.values[0]);
	currentAccely = lowPass(event.values[1]);
	float y = Math.abs(currentAccelx / 10);
	float z = Math.abs(currentAccely / 10);

	if (y > z)
		z = y;
	else
		y = z;

	if (accel == true)
		PdBase.sendFloat("pd_accely", (1 - y));

}
 
Example 6
Source File: MeiFireflyActivity.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        float x = event.values[0];
        float y = event.values[1] * 2.0f;
        mMobikeView.onSensorChanged(-x, y);
    }
}
 
Example 7
Source File: StepDcretor.java    From JkStepSensor with Apache License 2.0 5 votes vote down vote up
public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;
    synchronized (this) {
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            calc_step(event);
        }
    }
}
 
Example 8
Source File: DeviceOrientation.java    From ARCore-Location with MIT License 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {

    // Get the device heading
    float degree = Math.round( event.values[0] );
    currentDegree = -degree;

    switch (event.sensor.getType()) {
        case Sensor.TYPE_MAGNETIC_FIELD:
            mags = event.values.clone();
            break;
        case Sensor.TYPE_ACCELEROMETER:
            accels = event.values.clone();
            break;
    }

    if (mags != null && accels != null) {
        gravity = new float[9];
        magnetic = new float[9];
        SensorManager.getRotationMatrix(gravity, magnetic, accels, mags);
        float[] outGravity = new float[9];
        SensorManager.remapCoordinateSystem(gravity, SensorManager.AXIS_X,SensorManager.AXIS_Z, outGravity);
        SensorManager.getOrientation(outGravity, values);

        azimuth = values[0] * 57.2957795f;
        pitch = values[1] * 57.2957795f;
        roll = values[2] * 57.2957795f;
        mags = null;
        accels = null;
    }
}
 
Example 9
Source File: DeviceOrientation.java    From ARCore-Location with MIT License 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {

    // Get the device heading
    float degree = Math.round( event.values[0] );
    currentDegree = -degree;

    switch (event.sensor.getType()) {
        case Sensor.TYPE_MAGNETIC_FIELD:
            mags = event.values.clone();
            break;
        case Sensor.TYPE_ACCELEROMETER:
            accels = event.values.clone();
            break;
    }

    if (mags != null && accels != null) {
        gravity = new float[9];
        magnetic = new float[9];
        SensorManager.getRotationMatrix(gravity, magnetic, accels, mags);
        float[] outGravity = new float[9];
        SensorManager.remapCoordinateSystem(gravity, SensorManager.AXIS_X,SensorManager.AXIS_Z, outGravity);
        SensorManager.getOrientation(outGravity, values);

        azimuth = values[0] * 57.2957795f;
        pitch = values[1] * 57.2957795f;
        roll = values[2] * 57.2957795f;
        mags = null;
        accels = null;
    }
}
 
Example 10
Source File: StepThread.java    From healthgo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        stepDetector.updateModel(isActivity);

        stepDetector.updateStep(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
    }

}
 
Example 11
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void listing16_9() {
  final SensorEventListener mySensorEventListener = new SensorEventListener() {
    // Listing 16-10: Calculating the device orientation using the rotation vector
    public void onSensorChanged(SensorEvent sensorEvent) {
      float[] rotationMatrix = new float[9];
      float[] orientation = new float[3];

      // Convert the result Vector to a Rotation Matrix.
      SensorManager.getRotationMatrixFromVector(rotationMatrix,
        sensorEvent.values);

      // Extract the orientation from the Rotation Matrix.
      SensorManager.getOrientation(rotationMatrix, orientation);
      Log.d(TAG, "Yaw: " + orientation[0]); // Yaw
      Log.d(TAG, "Pitch: " + orientation[1]); // Pitch
      Log.d(TAG, "Roll: " + orientation[2]); // Roll
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) { }
  };

  // Listing 16-9: Monitoring an accelerometer sensor
  SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  int sensorType = Sensor.TYPE_ACCELEROMETER;
  sm.registerListener(mySensorEventListener,
    sm.getDefaultSensor(sensorType),
    SensorManager.SENSOR_DELAY_NORMAL);
}
 
Example 12
Source File: Engine.java    From tilt-game-android with MIT License 5 votes vote down vote up
@Override
public void onSensorChanged(final SensorEvent pEvent) {
	if (this.mRunning) {
		final Sensor sensor = pEvent.sensor;
		final int sensorType = sensor.getType();
		switch (sensorType) {
			case Sensor.TYPE_ACCELEROMETER:
				if (this.mAccelerationData != null) {
					this.mAccelerationData.setDisplayRotation(this.getDisplayOrientation());
					this.mAccelerationData.setValues(pEvent.values);
					this.mAccelerationListener.onAccelerationChanged(this.mAccelerationData);
				} else if (this.mOrientationData != null) {
					this.mOrientationData.setDisplayRotation(this.getDisplayOrientation());
					this.mOrientationData.setAccelerationValues(pEvent.values);
					this.mOrientationListener.onOrientationChanged(this.mOrientationData);
				}
				break;
			case Sensor.TYPE_MAGNETIC_FIELD:
				this.mOrientationData.setDisplayRotation(this.getDisplayOrientation());
				this.mOrientationData.setMagneticFieldValues(pEvent.values);
				this.mOrientationListener.onOrientationChanged(this.mOrientationData);
				break;
			default:
				throw new IllegalArgumentException("Unexpected " + Sensor.class.getSimpleName() + " of Type: '" + sensorType + "'.");
		}
	}
}
 
Example 13
Source File: ShakeByShakeActivity.java    From AndroidFrame with Apache License 2.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    int type = event.sensor.getType();

    if (type == Sensor.TYPE_ACCELEROMETER) {
        //获取三个方向值
        float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];

        if ((Math.abs(x) > 17 || Math.abs(y) > 17 || Math
                .abs(z) > 17) && !isShake) {
            isShake = true;
            // TODO: 2016/10/19 实现摇动逻辑, 摇动后进行震动
            Thread thread = new Thread() {
                @Override
                public void run() {
                    super.run();
                    try {
                        Log.d(TAG, "onSensorChanged: 摇动");

                        //开始震动 发出提示音 展示动画效果
                        sHandler.obtainMessage(START_SHAKE).sendToTarget();
                        Thread.sleep(500);
                        //再来一次震动提示
                        sHandler.obtainMessage(AGAIN_SHAKE).sendToTarget();
                        Thread.sleep(500);
                        sHandler.obtainMessage(END_SHAKE).sendToTarget();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();
        }
    }
}
 
Example 14
Source File: TiltControllerActivity.java    From android-robocar with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED)
        mGeomagnetic = event.values;
    if ((mGravity == null) || (mGeomagnetic == null))
        return;

    float[] R = new float[9];
    float[] I = new float[9];
    if (!SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic))
        return;

    float[] orientation = new float[3];
    SensorManager.getOrientation(R, orientation);

    if (orientation == null)
        return;

    double rollAngle = orientation[1] * 180 / Math.PI;
    double pitchAngle = orientation[2] * 180 / Math.PI;

    if(notWithinAngleTolerance((int)rollAngle, mLastRoll))
        this.mRollTextView.setText(String.format("%.0f", (rollAngle)));
    if(notWithinAngleTolerance((int)pitchAngle,mLastPitch))
        this.mPitchTextView.setText(String.format("%.0f", (pitchAngle)));

    mLastPitch = (int)pitchAngle;
    mLastRoll = (int) rollAngle;

    calculateAndSetSpeed(rollAngle,pitchAngle);
}
 
Example 15
Source File: ARSurfaceView.java    From geoar-app with Apache License 2.0 5 votes vote down vote up
public void onSensorChanged(SensorEvent event) {
	if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
		if (updateMagneticVector) {
			magnetValues.put(event.values);
		}
	} else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
		accelValues.put(event.values);
	}
	sensorValuesChanged = true;
}
 
Example 16
Source File: IntegratedTapDetector.java    From talkback with Apache License 2.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
  switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
      onAccelerometerChanged(System.nanoTime(), event.values);
      break;
    case Sensor.TYPE_GYROSCOPE:
      onGyroscopeChanged(System.nanoTime(), event.values);
      break;
    default: // fall out
  }
}
 
Example 17
Source File: SensorRawAccelerometerActivity.java    From coursera-android with MIT License 4 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        long actualTime = System.currentTimeMillis();

        if (actualTime - mLastUpdate > UPDATE_THRESHOLD) {

            mLastUpdate = actualTime;

            float x = event.values[0], y = event.values[1], z = event.values[2];

            mXValueView.setText(String.valueOf(x));
            mYValueView.setText(String.valueOf(y));
            mZValueView.setText(String.valueOf(z));

        }
    }
}
 
Example 18
Source File: AccelerometerStepDetectorService.java    From privacy-friendly-pedometer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int getSensorType() {
    return Sensor.TYPE_ACCELEROMETER;
}
 
Example 19
Source File: Orientation.java    From WhereYouGo with GNU General Public License v3.0 4 votes vote down vote up
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
  case Sensor.TYPE_MAGNETIC_FIELD:
    break;
  case Sensor.TYPE_ACCELEROMETER:
    float filter = getFilter();
    aboveOrBelow =
            (float) ((event.values[SensorManager.DATA_Z] * filter) + (aboveOrBelow * (1.0 - filter)));
    break;
  case Sensor.TYPE_ORIENTATION:
    float valueOr = event.values[SensorManager.DATA_X];
    // Logger.d(TAG, "sensorOrientation:" + valueOr + ", " + event.values[SensorManager.DATA_Y]
    // + ", " + event.values[SensorManager.DATA_Z] + ", " + getDeclination());
    // fix to true bearing
    if (Preferences.SENSOR_BEARING_TRUE) {
      valueOr += getDeclination();
    }
    orient = filterValue(valueOr, orient);
    pitch = filterValue(event.values[SensorManager.DATA_Y], pitch);

    roll = filterValue(event.values[SensorManager.DATA_Z], roll);
    float rollDef;
    if (aboveOrBelow < 0) {
      if (roll < 0) {
        rollDef = -180 - roll;
      } else {
        rollDef = 180 - roll;
      }
    } else {
      rollDef = roll;
    }
    this.mLastAziSensor = orient;

    // do some orientation change by settings
    int rotation = A.getMain().getWindowManager().getDefaultDisplay().getRotation();
    switch (rotation) {
      case Surface.ROTATION_0:
        // no need for change
        break;
      case Surface.ROTATION_90:
        mLastAziSensor += 90;
        break;
      case Surface.ROTATION_180:
        mLastAziSensor -= 180;
        break;
      case Surface.ROTATION_270:
        mLastAziSensor -= 90;
        break;
    }

    sendOrientation(pitch, rollDef);
    break;
}
}
 
Example 20
Source File: AccelerometerManager.java    From SensorAnnotations with Apache License 2.0 4 votes vote down vote up
@OnAccuracyChanged(Sensor.TYPE_ACCELEROMETER)
void testTemperatureAccuracyChanged(@NonNull Sensor sensor, int accuracy) {
    mMainActivity.logAccuracyChangedForSensor(sensor, accuracy);
}