Java Code Examples for android.hardware.Sensor#TYPE_MAGNETIC_FIELD
The following examples show how to use
android.hardware.Sensor#TYPE_MAGNETIC_FIELD .
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: ControllerFragment.java From Bluefruit_LE_Connect_Android_V2 with MIT License | 6 votes |
@Override public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); if (sensorType == Sensor.TYPE_ACCELEROMETER) { mSensorData[kSensorType_Accelerometer].values = event.values; updateOrientation(); // orientation depends on Accelerometer and Magnetometer mControllerAdapter.notifySensorChanged(kSensorType_Accelerometer); mControllerAdapter.notifySensorChanged(kSensorType_Quaternion); } else if (sensorType == Sensor.TYPE_GYROSCOPE) { mSensorData[kSensorType_Gyroscope].values = event.values; mControllerAdapter.notifySensorChanged(kSensorType_Gyroscope); } else if (sensorType == Sensor.TYPE_MAGNETIC_FIELD) { mSensorData[kSensorType_Magnetometer].values = event.values; updateOrientation(); // orientation depends on Accelerometer and Magnetometer mControllerAdapter.notifySensorChanged(kSensorType_Magnetometer); mControllerAdapter.notifySensorChanged(kSensorType_Quaternion); } }
Example 2
Source File: MainActivity.java From Android-9-Development-Cookbook with MIT License | 6 votes |
private void calculateCompassDirection(SensorEvent event) { switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: mAccelerationValues = event.values.clone(); break; case Sensor.TYPE_MAGNETIC_FIELD: mGravityValues = event.values.clone(); break; } boolean success = SensorManager.getRotationMatrix(mRotationMatrix, null, mAccelerationValues, mGravityValues); if (success) { float[] orientationValues = new float[3]; SensorManager.getOrientation(mRotationMatrix, orientationValues); float azimuth = (float) Math.toDegrees(-orientationValues[0]); RotateAnimation rotateAnimation = new RotateAnimation(mLastDirectionInDegrees, azimuth, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(50); rotateAnimation.setFillAfter(true); mImageViewCompass.startAnimation(rotateAnimation); mLastDirectionInDegrees = azimuth; } }
Example 3
Source File: MagAccelListener.java From prayer-times-android with Apache License 2.0 | 6 votes |
@Override public void onAccuracyChanged(Sensor sensor, int accuracy) { if (accuracy == 0) accuracy = SensorManager.SENSOR_STATUS_ACCURACY_LOW; switch (sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: mAccelerometerAccuracy = accuracy; break; case Sensor.TYPE_MAGNETIC_FIELD: mMagneticAccuracy = accuracy; break; default: break; } if (mAccelerometerAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW || mMagneticAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW) mRotationUpdateDelegate.onAccuracyChanged(SensorManager.SENSOR_STATUS_ACCURACY_LOW); else if (mAccelerometerAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM || mMagneticAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) { mRotationUpdateDelegate.onAccuracyChanged(SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM); } else if (mAccelerometerAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH && mMagneticAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH) { mRotationUpdateDelegate.onAccuracyChanged(SensorManager.SENSOR_STATUS_ACCURACY_HIGH); } }
Example 4
Source File: Engine.java From 30-android-libraries-in-30-days with Apache License 2.0 | 6 votes |
@Override public void onAccuracyChanged(final Sensor pSensor, final int pAccuracy) { if(this.mRunning) { switch(pSensor.getType()) { case Sensor.TYPE_ACCELEROMETER: if(this.mAccelerationData != null) { this.mAccelerationData.setAccuracy(pAccuracy); this.mAccelerationListener.onAccelerationAccuracyChanged(this.mAccelerationData); } else if(this.mOrientationData != null) { this.mOrientationData.setAccelerationAccuracy(pAccuracy); this.mOrientationListener.onOrientationAccuracyChanged(this.mOrientationData); } break; case Sensor.TYPE_MAGNETIC_FIELD: this.mOrientationData.setMagneticFieldAccuracy(pAccuracy); this.mOrientationListener.onOrientationAccuracyChanged(this.mOrientationData); break; } } }
Example 5
Source File: CameraFocusActivity.java From MeasureCam with MIT License | 6 votes |
public void onSensorChanged(SensorEvent event) { switch(event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: mGravity = event.values.clone(); //onAccelerometerChanged(values[0],values[1],values[2]); break; case Sensor.TYPE_MAGNETIC_FIELD: mMagnetic = event.values.clone(); break; case Sensor.TYPE_PRESSURE: pressure = event.values[0]; pressure = pressure*100; default: return; } if(mGravity != null && mMagnetic != null) { getDirection(); } }
Example 6
Source File: MagAccelListener.java From prayer-times-android with Apache License 2.0 | 6 votes |
@Override public void onSensorChanged(@NonNull SensorEvent event) { onAccuracyChanged(event.sensor, event.accuracy); switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: smooth(event.values, mAccelVals, mAccelVals); break; case Sensor.TYPE_MAGNETIC_FIELD: smooth(event.values, mMagVals, mMagVals); mIsReady = true; break; default: break; } // wait until we have both a new accelerometer and magnetometer sample if (mIsReady) { mIsReady = false; fuseValues(); } }
Example 7
Source File: CalibrationFragment.java From prayer-times-android with Apache License 2.0 | 5 votes |
@Override public void onAccuracyChanged(Sensor sensor, int accuracy) { if (accuracy == 0) accuracy = SensorManager.SENSOR_STATUS_ACCURACY_LOW; switch (sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: mAccelerometerAccuracy = accuracy; break; case Sensor.TYPE_MAGNETIC_FIELD: mMagneticAccuracy = accuracy; break; default: break; } if (mAccelerometerAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW || mMagneticAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW) mAccuracy.setText(Html.fromHtml( String.format("%s: <font color='#ff0000'>%s</font>", getString(R.string.accuracy), getString(R.string.accuracy_low)))); else if (mAccelerometerAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM || mMagneticAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) { mAccuracy.setText(Html.fromHtml( String.format("%s: <font color='#e6e600'>%s</font>", getString(R.string.accuracy), getString(R.string.accuracy_medium)))); } else if (mAccelerometerAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH && mMagneticAccuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH) { mAccuracy.setText(Html.fromHtml( String.format("%s: <font color='#00ff00'>%s</font>", getString(R.string.accuracy), getString(R.string.accuracy_high)))); mAccuracy.postDelayed(() -> { if (!isStateSaved()) getParentFragment().getChildFragmentManager().beginTransaction().remove(CalibrationFragment.this).commit(); }, 3000); } }
Example 8
Source File: SensorsActivity.java From android-motion-detector with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { if (sensor == null) throw new NullPointerException(); if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD && accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) { Log.e(TAG, "Compass data unreliable"); } }
Example 9
Source File: Engine.java From tilt-game-android with MIT License | 5 votes |
@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 10
Source File: DeviceOrientation.java From ARCore-Location with MIT License | 5 votes |
@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 11
Source File: SensorsActivity.java From android-motion-detector with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void onSensorChanged(SensorEvent evt) { if (!computing.compareAndSet(false, true)) return; if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { grav[0] = evt.values[0]; grav[1] = evt.values[1]; grav[2] = evt.values[2]; } else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { mag[0] = evt.values[0]; mag[1] = evt.values[1]; mag[2] = evt.values[2]; } float gravity = grav[0] + grav[1] + grav[2]; float magnetic = mag[0] + mag[1] + mag[2]; float gravDiff = Math.abs(gravity - prevGrav); float magDiff = Math.abs(magnetic - prevMag); // Log.i(TAG, "gravDiff="+gravDiff+" magDiff="+magDiff); if ((Float.compare(prevGrav, 0.0f) != 0 && Float.compare(prevMag, 0.0f) != 0) && (gravDiff > gravThreshold || magDiff > magThreshold)) { GlobalData.setPhoneInMotion(true); } else { GlobalData.setPhoneInMotion(false); } prevGrav = gravity; prevMag = magnetic; computing.set(false); }
Example 12
Source File: OrientationManager.java From PTVGlass with MIT License | 5 votes |
@Override public void onAccuracyChanged(Sensor sensor, int accuracy) { if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { mHasInterference = (accuracy < SensorManager.SENSOR_STATUS_ACCURACY_HIGH); notifyAccuracyChanged(); } }
Example 13
Source File: RadarScanView.java From beaconloc with Apache License 2.0 | 5 votes |
private synchronized void calcBearing(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length); mLastAccelerometerSet = true; } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length); mLastMagnetometerSet = true; } if (mLastAccelerometerSet && mLastMagnetometerSet) { /* Create rotation Matrix */ float[] rotationMatrix = new float[9]; if (SensorManager.getRotationMatrix(rotationMatrix, null, mLastAccelerometer, mLastMagnetometer)) { SensorManager.getOrientation(rotationMatrix, mOrientation); float azimuthInRadians = mOrientation[0]; angleLowpassFilter.add(azimuthInRadians); mLast_bearing = (float) (Math.toDegrees(angleLowpassFilter.average()) + 360) % 360; postInvalidate(); //Log.d(Constants.TAG, "orientation bearing: " + mLast_bearing); } } }
Example 14
Source File: MainActivity.java From Form-N-Fun with MIT License | 5 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final FrameLayout layout = new FrameLayout(this); layout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); setContentView(layout); mOpenCvCameraView = new CameraView(this,mCameraIndex); //mCameraIndex = 0 indicates rear camera mOpenCvCameraView.setCvCameraViewListener(this); mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); mOpenCvCameraView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); layout.addView(mOpenCvCameraView); //add CameraView to frame layout gs = new GraphicSurface(this); // create graphic surface gs.getHolder().setFormat( PixelFormat.TRANSPARENT); //set graphic surface to transparent gs.setZOrderOnTop(true); //graphic surface as top layer gs.setLayoutParams(new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); layout.addView(gs); //add graphic surface to frame layout bt = new Button(this); // create button bt.setText("Play"); bt.setId(12345); bt.getBackground().setAlpha(64); //button to transparent bt.setOnClickListener(this); bt.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER)); layout.addView(bt); //add button to frame layout sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); accelerometerSensor = Sensor.TYPE_ACCELEROMETER; //accelerometer magneticSensor = Sensor.TYPE_MAGNETIC_FIELD; //magentic sensor if(Build.VERSION.SDK_INT >= 23) askForPermission(Manifest.permission.CAMERA,CAMERA); //ask for camera permission }
Example 15
Source File: ARSurfaceView.java From geoar-app with Apache License 2.0 | 5 votes |
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: MainActivity.java From SensorAnnotations with Apache License 2.0 | 4 votes |
@OnSensorChanged(Sensor.TYPE_MAGNETIC_FIELD) void testMagneticFieldSensorChanged(@NonNull SensorEvent event) { updateTextViewWithEventData(mMagneticFieldEventOutputTextView, event); }
Example 17
Source File: XSensorManager.java From XPrivacy with GNU General Public License v3.0 | 4 votes |
@SuppressWarnings("deprecation") private boolean isRestricted(XParam param, int type) throws Throwable { if (type == Sensor.TYPE_ALL) return false; else if (type == Sensor.TYPE_ACCELEROMETER || type == Sensor.TYPE_LINEAR_ACCELERATION) { if (isRestricted(param, "acceleration")) return true; } else if (type == Sensor.TYPE_GRAVITY) { if (isRestricted(param, "gravity")) return true; } else if (type == Sensor.TYPE_RELATIVE_HUMIDITY) { if (isRestricted(param, "humidity")) return true; } else if (type == Sensor.TYPE_LIGHT) { if (isRestricted(param, "light")) return true; } else if (type == Sensor.TYPE_MAGNETIC_FIELD || type == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED) { if (isRestricted(param, "magnetic")) return true; } else if (type == Sensor.TYPE_SIGNIFICANT_MOTION) { if (isRestricted(param, "motion")) return true; } else if (type == Sensor.TYPE_ORIENTATION || type == Sensor.TYPE_GYROSCOPE || type == Sensor.TYPE_GYROSCOPE_UNCALIBRATED) { if (isRestricted(param, "orientation")) return true; } else if (type == Sensor.TYPE_PRESSURE) { if (isRestricted(param, "pressure")) return true; } else if (type == Sensor.TYPE_PROXIMITY) { if (isRestricted(param, "proximity")) return true; } else if (type == Sensor.TYPE_GAME_ROTATION_VECTOR || type == Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR || type == Sensor.TYPE_ROTATION_VECTOR) { if (isRestricted(param, "rotation")) return true; } else if (type == Sensor.TYPE_TEMPERATURE || type == Sensor.TYPE_AMBIENT_TEMPERATURE) { if (isRestricted(param, "temperature")) return true; } else if (type == Sensor.TYPE_STEP_COUNTER || type == Sensor.TYPE_STEP_DETECTOR) { if (isRestricted(param, "step")) return true; } else if (type == Sensor.TYPE_HEART_RATE) { if (isRestricted(param, "heartrate")) return true; } else if (type == 22) { // 22 = TYPE_TILT_DETECTOR // Do nothing } else if (type == 23 || type == 24 || type == 25) { // 23 = TYPE_WAKE_GESTURE // 24 = TYPE_GLANCE_GESTURE // 25 = TYPE_PICK_UP_GESTURE // 23/24 This sensor is expected to only be used by the system ui // 25 Expected to be used internally for always on display } else Util.log(this, Log.WARN, "Unknown sensor type=" + type); return false; }
Example 18
Source File: SKAbstractNativeSensorModule.java From SensingKit-Android with GNU Lesser General Public License v3.0 | 4 votes |
@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); } }
Example 19
Source File: AccelerometerSucker.java From CameraV with GNU General Public License v3.0 | 4 votes |
@SuppressWarnings("unchecked") public AccelerometerSucker(Context context) { super(context); setSucker(this); df.setMaximumFractionDigits(1); df.setPositivePrefix("+"); sm = (SensorManager)context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE); availableSensors = sm.getSensorList(Sensor.TYPE_ALL); for(Sensor s : availableSensors) { switch(s.getType()) { case Sensor.TYPE_ACCELEROMETER: hasAccelerometer = true; sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME); break; case Sensor.TYPE_MAGNETIC_FIELD: sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME); hasOrientation = true; break; } } setTask(new TimerTask() { @Override public void run() { try { if(hasAccelerometer) readAccelerometer(); if(hasOrientation) readOrientation(); } catch(JSONException e){} } }); getTimer().schedule(getTask(), 0, Accelerometer.LOG_RATE); }
Example 20
Source File: AndroidSensorsDriver.java From sensorhub with Mozilla Public License 2.0 | 4 votes |
@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(); }