下面列出了android.hardware.Sensor#TYPE_ROTATION_VECTOR 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onSensorChanged(final SensorEvent event) {
if (isOn && event.accuracy != 0){
if (getParams().mSensorListener != null){
getParams().mSensorListener.onSensorChanged(event);
}
int type = event.sensor.getType();
switch (type){
case Sensor.TYPE_ROTATION_VECTOR:
// post
VRUtil.sensorRotationVector2Matrix(event, windowManager.getDefaultDisplay().getRotation(), mSensorMatrix);
// mTmpMatrix will be used in multi thread.
synchronized (mMatrixLock){
System.arraycopy(mSensorMatrix, 0, mTmpMatrix, 0, 16);
}
getParams().glHandler.post(updateSensorRunnable);
break;
}
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
// Get the current heading from the sensor, then notify the listeners of the
// change.
SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X,
SensorManager.AXIS_Z, mRotationMatrix);
SensorManager.getOrientation(mRotationMatrix, mOrientation);
// Store the pitch (used to display a message indicating that the user's head
// angle is too steep to produce reliable results.
mPitch = (float) Math.toDegrees(mOrientation[1]);
// Convert the heading (which is relative to magnetic north) to one that is
// relative to true north, using the user's current location to compute this.
float magneticHeading = (float) Math.toDegrees(mOrientation[0]);
mHeading = MathUtils.mod(computeTrueNorth(magneticHeading), 360.0f)
- ARM_DISPLACEMENT_DEGREES;
notifyOrientationChanged();
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if( event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR ){
// calculate th rotation matrix
SensorManager.getRotationMatrixFromVector(rMat, event.values);
// get the azimuth value (orientation[0]) in degree
newAzimuth = (int) ((((( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[0] ) + 360 ) % 360) -
( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[2] ))) +360) % 360);
//dont react to changes smaller than the filter value
if (Math.abs(mAzimuth - newAzimuth) < mFilter) {
return;
}
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("headingUpdated", (int) newAzimuth);
mAzimuth = newAzimuth;
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
// Get the current heading from the sensor, then notify the listeners of the
// change.
SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X,
SensorManager.AXIS_Z, mRotationMatrix);
SensorManager.getOrientation(mRotationMatrix, mOrientation);
// Store the pitch (used to display a message indicating that the user's head
// angle is too steep to produce reliable results.
mPitch = (float) Math.toDegrees(mOrientation[1]);
// Convert the heading (which is relative to magnetic north) to one that is
// relative to true north, using the user's current location to compute this.
float magneticHeading = (float) Math.toDegrees(mOrientation[0]);
mHeading = MathUtils.mod(computeTrueNorth(magneticHeading), 360.0f)
- ARM_DISPLACEMENT_DEGREES;
notifyOrientationChanged();
}
}
@Override
public void onSensorChanged(final SensorEvent event) {
if (isOn && event.accuracy != 0){
if (getParams().mSensorListener != null){
getParams().mSensorListener.onSensorChanged(event);
}
int type = event.sensor.getType();
switch (type){
case Sensor.TYPE_ROTATION_VECTOR:
// post
VRUtil.sensorRotationVector2Matrix(event, windowManager.getDefaultDisplay().getRotation(), mSensorMatrix);
// mTmpMatrix will be used in multi thread.
synchronized (mMatrixLock){
System.arraycopy(mSensorMatrix, 0, mTmpMatrix, 0, 16);
}
getParams().glHandler.post(updateSensorRunnable);
break;
}
}
}
@Override public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ROTATION_VECTOR || !mBeerView.isMax()) {
return;
}
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
float[] adjustedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
adjustedRotationMatrix);
float[] orientation = new float[3];
SensorManager.getOrientation(adjustedRotationMatrix, orientation);
float roll = orientation[2] * -57;
if (roll < ROLL_LIMIT && roll > -ROLL_LIMIT) {
mBeerView.drawGlass(-roll);
mBeerView.drawGlassFroth(-roll, 1);
mOldRoll = -roll;
} else {
mBeerView.drawGlass(mOldRoll);
mBeerView.drawGlassFroth(mOldRoll, 1);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_GAME_ROTATION_VECTOR:
case Sensor.TYPE_ROTATION_VECTOR:
processSensorOrientation(event.values);
break;
default:
Log.e("DeviceOrientation", "Sensor event type not supported");
break;
}
}
@Override
public void onSensorChanged(SensorEvent event) {
// Check that we received the proper sensor event
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
logOrientation(event);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
// that we received the proper event
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
// convert the rotation-vector to a 4x4 matrix. the matrix
// is interpreted by Open GL as the inverse of the
// rotation-vector, which is what we want.
SensorManager.getRotationMatrixFromVector(currentOrientationRotationMatrix, event.values);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
if (event.values.length > 4) {
float[] truncatedRotationVector = new float[4];
System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
update(truncatedRotationVector);
} else {
update(event.values);
}
}
}
private void populateTypeField( int type ) {
if( type == 0 || mTypeRow == null || mType == null )
return;
String typeName;
switch( type ) {
case Sensor.TYPE_ACCELEROMETER: {
typeName = "Accelerometer";
break;
}
case Sensor.TYPE_AMBIENT_TEMPERATURE: {
typeName = "Ambient Temperature";
break;
}
case Sensor.TYPE_GAME_ROTATION_VECTOR: {
typeName = "Game Rotation Vector";
break;
}
case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR: {
typeName = "Geomagnetic Rotation Vector";
break;
}
case Sensor.TYPE_GRAVITY: {
typeName = "Gravity";
break;
}
case Sensor.TYPE_GYROSCOPE: {
typeName = "Gyroscope";
break;
}
case Sensor.TYPE_GYROSCOPE_UNCALIBRATED: {
typeName = "Uncalibrated Gyroscope";
break;
}
case Sensor.TYPE_LIGHT: {
typeName = "Light";
break;
}
case Sensor.TYPE_LINEAR_ACCELERATION: {
typeName = "Linear Acceleration";
break;
}
case Sensor.TYPE_MAGNETIC_FIELD: {
typeName = "Magnetic Field";
break;
}
case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED: {
typeName = "Uncalibrated Magnetic Field";
break;
}
case Sensor.TYPE_PRESSURE: {
typeName = "Pressure";
break;
}
case Sensor.TYPE_PROXIMITY: {
typeName = "Proximity";
break;
}
case Sensor.TYPE_RELATIVE_HUMIDITY: {
typeName = "Relative Humidity";
break;
}
case Sensor.TYPE_ROTATION_VECTOR: {
typeName = "Rotation Vector";
break;
}
case Sensor.TYPE_SIGNIFICANT_MOTION: {
typeName = "Significant Motion";
break;
}
case Sensor.TYPE_STEP_COUNTER: {
typeName = "Step Counter";
break;
}
case Sensor.TYPE_STEP_DETECTOR: {
typeName = "Step Detector";
break;
}
default: {
typeName = "Other";
}
}
mType.setText( typeName );
mTypeRow.setVisibility( View.VISIBLE );
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
float[] rotationMatrixFromVector = new float[16];
float[] rotationMatrix = new float[16];
getRotationMatrixFromVector(rotationMatrixFromVector, sensorEvent.values);
final int screenRotation = this.getWindowManager().getDefaultDisplay()
.getRotation();
switch (screenRotation) {
case ROTATION_90:
remapCoordinateSystem(rotationMatrixFromVector,
AXIS_Y,
AXIS_MINUS_X, rotationMatrix);
break;
case ROTATION_270:
remapCoordinateSystem(rotationMatrixFromVector,
AXIS_MINUS_Y,
AXIS_X, rotationMatrix);
break;
case ROTATION_180:
remapCoordinateSystem(rotationMatrixFromVector,
AXIS_MINUS_X, AXIS_MINUS_Y,
rotationMatrix);
break;
default:
remapCoordinateSystem(rotationMatrixFromVector,
AXIS_X, AXIS_Y,
rotationMatrix);
break;
}
float[] projectionMatrix = arCamera.getProjectionMatrix();
float[] rotatedProjectionMatrix = new float[16];
Matrix.multiplyMM(rotatedProjectionMatrix, 0, projectionMatrix, 0, rotationMatrix, 0);
this.arOverlayView.updateRotatedProjectionMatrix(rotatedProjectionMatrix);
//Heading
float[] orientation = new float[3];
getOrientation(rotatedProjectionMatrix, orientation);
double bearing = Math.toDegrees(orientation[0]) + declination;
tvBearing.setText(String.format("Bearing: %s", bearing));
}
}
private void loadSensorData(SensorEvent event) {
int sensorType = event.sensor.getType();
if (sensorType == Sensor.TYPE_ACCELEROMETER) mGravity = event.values;
if (sensorType == Sensor.TYPE_MAGNETIC_FIELD) mGeomagnetic = event.values;
if (sensorType == Sensor.TYPE_ROTATION_VECTOR) mRotationVector = event.values;
}
@SuppressLint("InlinedApi") // There is a check in STEP_DETECTOR and STEP_COUNTER
private static int getSensorType(SKSensorModuleType sensorType) throws SKException{
switch (sensorType) {
case ACCELEROMETER:
return Sensor.TYPE_ACCELEROMETER;
case GRAVITY:
return Sensor.TYPE_GRAVITY;
case LINEAR_ACCELERATION:
return Sensor.TYPE_LINEAR_ACCELERATION;
case GYROSCOPE:
return Sensor.TYPE_GYROSCOPE;
case ROTATION:
return Sensor.TYPE_ROTATION_VECTOR;
case MAGNETOMETER:
return Sensor.TYPE_MAGNETIC_FIELD;
case AMBIENT_TEMPERATURE:
return Sensor.TYPE_AMBIENT_TEMPERATURE;
case STEP_DETECTOR:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return Sensor.TYPE_STEP_DETECTOR;
}
else
{
throw new SKException(TAG, "STEP_DETECTOR requires Android KitKat or greater.", SKExceptionErrorCode.UNKNOWN_ERROR);
}
case STEP_COUNTER:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return Sensor.TYPE_STEP_COUNTER;
}
else
{
throw new SKException(TAG, "STEP_COUNTER requires Android KitKat or greater.", SKExceptionErrorCode.UNKNOWN_ERROR);
}
case LIGHT:
return Sensor.TYPE_LIGHT;
case LOCATION:
case ACTIVITY:
case BATTERY:
throw new SKException(TAG, "Not a native SensorModule.", SKExceptionErrorCode.UNKNOWN_ERROR);
default:
throw new SKException(TAG, "Unknown SensorModule", SKExceptionErrorCode.UNKNOWN_ERROR);
}
}
public static String[] getLabelsForSensor(Context context, Sensor sensor) {
String[] labels;
switch (sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
labels = context.getResources().getStringArray(R.array.accelerometer_values);
break;
case Sensor.TYPE_AMBIENT_TEMPERATURE:
labels = context.getResources().getStringArray(R.array.ambient_temperature_values);
break;
case Sensor.TYPE_GAME_ROTATION_VECTOR:
labels = context.getResources().getStringArray(R.array.game_rotation_vector_values);
break;
case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
labels = context.getResources().getStringArray(R.array.rotation_vector_values);
break;
case Sensor.TYPE_GRAVITY:
labels = context.getResources().getStringArray(R.array.gravity_values);
break;
case Sensor.TYPE_GYROSCOPE:
labels = context.getResources().getStringArray(R.array.gyroscore_values);
break;
case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
labels = context.getResources().getStringArray(R.array.gyroscore_uncalibrated_values);
break;
case Sensor.TYPE_HEART_RATE:
labels = context.getResources().getStringArray(R.array.heart_rate_values);
break;
case Sensor.TYPE_LIGHT:
labels = context.getResources().getStringArray(R.array.light_values);
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
labels = context.getResources().getStringArray(R.array.linear_acceleration_values);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
labels = context.getResources().getStringArray(R.array.magnetic_values);
break;
case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
labels = context.getResources().getStringArray(R.array.magnetic_field_uncalibrated_values);
break;
case Sensor.TYPE_PRESSURE:
labels = context.getResources().getStringArray(R.array.pressure_values);
break;
case Sensor.TYPE_PROXIMITY:
labels = context.getResources().getStringArray(R.array.proximity_values);
break;
case Sensor.TYPE_RELATIVE_HUMIDITY:
labels = context.getResources().getStringArray(R.array.relative_humidity_values);
break;
case Sensor.TYPE_ROTATION_VECTOR:
labels = context.getResources().getStringArray(R.array.rotation_vector_values);
break;
case Sensor.TYPE_STEP_COUNTER:
labels = context.getResources().getStringArray(R.array.step_counter_values);
break;
default:
labels = new String[]{};
}
return labels;
}
@Nullable
public static String getHumanStringType(Sensor sensor) {
switch (sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
return "Accelerometer";
case Sensor.TYPE_AMBIENT_TEMPERATURE:
return "Ambient Temperature";
case Sensor.TYPE_GAME_ROTATION_VECTOR:
return "Game Rotation Vector";
case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
return "Geomagnetic Rotation Vector";
case Sensor.TYPE_GRAVITY:
return "Gravity";
case Sensor.TYPE_GYROSCOPE:
return "Gyroscope";
case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
return "Gyroscope (Uncalibrated)";
case Sensor.TYPE_HEART_RATE:
return "Heart Rate";
case Sensor.TYPE_LIGHT:
return "Light";
case Sensor.TYPE_LINEAR_ACCELERATION:
return "Linear Acceleration";
case Sensor.TYPE_MAGNETIC_FIELD:
return "Magnetic Field";
case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
return "Magnetic Field (Uncalibrated)";
case Sensor.TYPE_PRESSURE:
return "Pressure";
case Sensor.TYPE_PROXIMITY:
return "Proximity";
case Sensor.TYPE_RELATIVE_HUMIDITY:
return "Relative Humidity";
case Sensor.TYPE_ROTATION_VECTOR:
return "Rotation Vector";
case Sensor.TYPE_SIGNIFICANT_MOTION:
return "Significant Motion";
case Sensor.TYPE_STEP_COUNTER:
return "Step Counter";
case Sensor.TYPE_STEP_DETECTOR:
return "Step Detector";
case Sensor.TYPE_ORIENTATION:
return "Orientation";
case Sensor.TYPE_TEMPERATURE:
return "Temperature";
}
return null;
}
public static String getDescription(Sensor sensor) {
switch (sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
return "Measures the acceleration force in m/s² that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.";
case Sensor.TYPE_AMBIENT_TEMPERATURE:
return "Measures the ambient room temperature in degrees Celsius (°C).";
case Sensor.TYPE_GRAVITY:
return "Measures the force of gravity in m/s² that is applied to a device on all three physical axes (x, y, z).";
case Sensor.TYPE_GYROSCOPE:
return "Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z).";
case Sensor.TYPE_HEART_RATE:
return "Measures heart rate.";
case Sensor.TYPE_LIGHT:
return "Measures the ambient light level (illumination) in lx.";
case Sensor.TYPE_LINEAR_ACCELERATION:
return "Measures the acceleration force in m/s² that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity.";
case Sensor.TYPE_MAGNETIC_FIELD:
return "Measures the ambient geomagnetic field for all three physical axes (x, y, z) in μT.";
case Sensor.TYPE_PRESSURE:
return "Measures the ambient air pressure in hPa or mbar.";
case Sensor.TYPE_PROXIMITY:
return "Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person's ear.";
case Sensor.TYPE_RELATIVE_HUMIDITY:
return "Measures the relative ambient humidity in percent (%).";
case Sensor.TYPE_ROTATION_VECTOR:
return "Measures the orientation of a device by providing the three elements of the device's rotation vector.";
case Sensor.TYPE_ORIENTATION:
return "Measures degrees of rotation that a device makes around all three physical axes (x, y, z). ";
case Sensor.TYPE_TEMPERATURE:
return "Measures the temperature of the device in degrees Celsius (°C). ";
default:
return "Information about this sensor is unavailable.";
}
}
@Override
public void start() throws SensorException
{
// we call stop() to cleanup just in case we weren't properly stopped
stop();
Context androidContext = config.androidContext;
// create data interfaces for sensors
this.sensorManager = (SensorManager)androidContext.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> deviceSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor: deviceSensors)
{
log.debug("Detected sensor " + sensor.getName());
switch (sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
if (config.activateAccelerometer)
useSensor(new AndroidAcceleroOutput(this, sensorManager, sensor), sensor);
break;
case Sensor.TYPE_GYROSCOPE:
if (config.activateGyrometer)
useSensor(new AndroidGyroOutput(this, sensorManager, sensor), sensor);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
if (config.activateMagnetometer)
useSensor(new AndroidMagnetoOutput(this, sensorManager, sensor), sensor);
break;
case Sensor.TYPE_ROTATION_VECTOR:
if (config.activateOrientationQuat)
useSensor(new AndroidOrientationQuatOutput(this, sensorManager, sensor), sensor);
if (config.activateOrientationEuler)
useSensor(new AndroidOrientationEulerOutput(this, sensorManager, sensor), sensor);
break;
}
}
// create data interfaces for location providers
if (androidContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION))
{
this.locationManager = (LocationManager)androidContext.getSystemService(Context.LOCATION_SERVICE);
List<String> locProviders = locationManager.getAllProviders();
for (String provName: locProviders)
{
log.debug("Detected location provider " + provName);
LocationProvider locProvider = locationManager.getProvider(provName);
// keep only GPS for now
if ( (locProvider.requiresSatellite() && config.activateGpsLocation) ||
(locProvider.requiresNetwork() && config.activateNetworkLocation))
useLocationProvider(new AndroidLocationOutput(this, locationManager, locProvider), locProvider);
}
}
// create data interfaces for cameras
if (androidContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
createCameraOutputs(androidContext);
// init all outputs
for (ISensorDataInterface o: this.getAllOutputs().values())
((IAndroidOutput)o).init();
// update sensorml description
updateSensorDescription();
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ROTATION_VECTOR:
SensorManager.getQuaternionFromVector(latestQuaternion, event.values);
if (!haveRotVecData) {
initialiseDefaultFilters(SMOOTHING_FACTOR_HIGH_ACC);
}
haveRotVecData = true;
break;
case Sensor.TYPE_GRAVITY:
if (haveRotVecData) {
// rotation vector sensor data is better
sensorManager.unregisterListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY));
break;
}
System.arraycopy(event.values, 0, latestAccelerations, 0, 3);
haveGravData = true;
break;
case Sensor.TYPE_ACCELEROMETER:
if (haveGravData || haveRotVecData) {
// rotation vector / gravity sensor data is better!
// let's not listen to the accelerometer anymore
sensorManager.unregisterListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
break;
}
System.arraycopy(event.values, 0, latestAccelerations, 0, 3);
haveAccelData = true;
break;
case Sensor.TYPE_MAGNETIC_FIELD:
if (haveRotVecData) {
// rotation vector sensor data is better
sensorManager.unregisterListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD));
break;
}
System.arraycopy(event.values, 0, latestMagFields, 0, 3);
haveMagData = true;
break;
}
if (haveDataNecessaryToComputeOrientation()) {
computeOrientation();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
Log.i(TAG, "Rotation sensor accuracy changed to: " + accuracy);
}
}