public void initializeSensor(Context context){ SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); magneticValues = new float[3]; accelerationValues = new float[3]; orientationValues = new float[3]; rotationMatrix = new float[9]; final SensorEventListener mEventListener = new SensorEventListener() { public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {} public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()) { case android.hardware.Sensor.TYPE_ACCELEROMETER: System.arraycopy(event.values, 0, accelerationValues, 0, 3); break; case android.hardware.Sensor.TYPE_MAGNETIC_FIELD: System.arraycopy(event.values, 0, magneticValues, 0, 3); break; } } }; setListeners(sensorManager, mEventListener); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); audioPlayer = new AudioPlayer(); audioPlayer.init(this, R.raw.ska_trek); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sensorEventListener = new SensorEventListener() { @Override public void onSensorChanged(final SensorEvent event) { Log.e(TAG, "" + event.timestamp); xValue.onNext(event.values[0]); yValue.onNext(event.values[1]); zValue.onNext(event.values[2]); } @Override public void onAccuracyChanged(final Sensor sensor, final int accuracy) { //No op } }; }
@SuppressLint("InlinedApi") public BeeSensorProcessor(Context context, SensorEventListener listener) { sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); // checking for sensors if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null) { accelerometerEnabled = false; } if (sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) == null) { gyroscopeEnabled = false; } if (accelerometerEnabled && gyroscopeEnabled) { sensorRotation = sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR); if (sensorRotation == null) { versionCompatible = false; } if (versionCompatible) { sensorEnabled = true; sensorManager.registerListener(listener, sensorRotation, 0); quaternionFromEuler(diffQuaternion, new float[] { 0, 90, 180 }); this.listener = listener; } } }
public KalmanProcessor(Context context, SensorEventListener listener) { sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); accelerationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); sensorManager.registerListener(listener, accelerationSensor, 0); gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); sensorManager.registerListener(listener, gyroSensor, 0); if (accelerationSensor != null && gyroSensor != null) { this.listener = listener; sensorEnabled = true; } gyroOrientation[0] = 0.0f; gyroOrientation[1] = 0.0f; gyroOrientation[2] = 0.0f; gyroMatrix[0] = 1.0f; gyroMatrix[1] = 0.0f; gyroMatrix[2] = 0.0f; gyroMatrix[3] = 0.0f; gyroMatrix[4] = 1.0f; gyroMatrix[5] = 0.0f; gyroMatrix[6] = 0.0f; gyroMatrix[7] = 0.0f; gyroMatrix[8] = 1.0f; }
private BroadcastReceiver setScreenStateReceiver(final SensorEventListener sensorEventListener) { return new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //HANDLES SCREEN ON/OFF ACTIONS if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { //RE-REGISTERS PROXIMITY SENSOR LISTENER WHEN SCREEN IS ON TO ACCEPT USER INPUT sensorManager.registerListener(sensorEventListener, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL); statusBarDown = false; } else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { //LOCKS THE SCREEN WHEN SCREEN IS OFF AND NOT IN A PHONE CALL if(audioManager.getMode() != AudioManager.MODE_IN_CALL) getSystemService(Context.DEVICE_POLICY_SERVICE); //UNREGISTERS PROXIMITY SENSOR LISTENER WHEN SCREEN IS OFF TO PREVENT USER INPUT sensorManager.unregisterListener(sensorEventListener, proximitySensor); } } }; }
@Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); final SensorEventListener activity = this; JoH.runOnUiThreadDelayed(new Runnable() { @Override public void run() { Log.d(TAG, "Unregistering proximity sensor listener"); try { mSensorManager.unregisterListener(activity); } catch (Exception e) { Log.d(TAG, "Error unregistering proximity listener: " + e); } } }, 10000); }
@Override protected void beforeHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable { SensorEventListener listener = (SensorEventListener) XposedHelpers.getObjectField(param.thisObject, "mListener"); int handle = (int) param.args[0]; Object mgr = XposedHelpers.getObjectField(param.thisObject, "mManager"); HashMap<Integer, Sensor> sensors = (HashMap<Integer, Sensor>) XposedHelpers.getObjectField(mgr, "mHandleToSensor"); Sensor s = sensors.get(handle); if (listener instanceof VirtualSensorListener) { float[] values = this.mSensorChange.handleListener(s, (VirtualSensorListener) listener, ((float[]) param.args[1]).clone(), (int) param.args[2], (long) param.args[3], XposedMod.ACCELEROMETER_RESOLUTION, XposedMod.MAGNETIC_RESOLUTION); if (values != null) { System.arraycopy(values, 0, param.args[1], 0, values.length); param.args[0] = XposedMod.sensorTypetoHandle.get(((VirtualSensorListener) listener).getSensor().getType()); }// else param.setResult(null); } }
@Override protected void beforeHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable { SensorEventListener listener = (SensorEventListener) XposedHelpers.getObjectField(param.thisObject, "mListener"); int handle = (int) param.args[0]; Object mgr = XposedHelpers.getObjectField(param.thisObject, "mManager"); SparseArray<Sensor> sensors = (SparseArray<Sensor>) XposedHelpers.getObjectField(mgr, "mHandleToSensor"); Sensor s = sensors.get(handle); if (listener instanceof VirtualSensorListener) { float[] values = this.mSensorChange.handleListener(s, (VirtualSensorListener) listener, ((float[]) param.args[1]).clone(), (int) param.args[2], (long) param.args[3], XposedMod.ACCELEROMETER_RESOLUTION, XposedMod.MAGNETIC_RESOLUTION); if (values != null) { System.arraycopy(values, 0, param.args[1], 0, values.length); param.args[0] = XposedMod.sensorTypetoHandle.get(((VirtualSensorListener) listener).getSensor().getType()); }// else param.setResult(null); } }
@SuppressWarnings("deprecation") public static void initialize(Context context) { mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); mListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { mDirection = event.values[0]; } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; }
@Override public void run() { mSensorEventListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { changed = System.currentTimeMillis(); handler.postDelayed(mRunnable1, 300); // ms } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // don't need it } }; IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_POWER_CONNECTED); filter.addAction(Intent.ACTION_POWER_DISCONNECTED); filter.addAction("android.intent.action.HDMI_PLUGGED"); registerReceiver(mReceiver, filter); register(); }
@Test public void testObserveSensor() throws Exception { setFinalStatic(Build.VERSION.class.getField("SDK_INT"), 9); when(mockSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)).thenReturn(mockSensor); final int numberOfEventsToSend = 5; final ArgumentCaptor<SensorEventListener> argumentCaptor = ArgumentCaptor.forClass(SensorEventListener.class); when(mockSensorManager.registerListener( argumentCaptor.capture(), eq(mockSensor), eq(0) )).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { for (int i = 0; i < numberOfEventsToSend; ++i) { argumentCaptor.getValue().onSensorChanged(mock(SensorEvent.class)); } return true; } }); rxSensorManager.observeSensor(Sensor.TYPE_ACCELEROMETER, 0) .test() .assertValueCount(numberOfEventsToSend); }
@Test public void testObserveSensorAccuracy() throws Exception { setFinalStatic(Build.VERSION.class.getField("SDK_INT"), 9); when(mockSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)).thenReturn(mockSensor); final int numberOfEventsToSend = 5; final ArgumentCaptor<SensorEventListener> argumentCaptor = ArgumentCaptor.forClass(SensorEventListener.class); when(mockSensorManager.registerListener( argumentCaptor.capture(), eq(mockSensor), eq(0) )).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { for (int i = 0; i < numberOfEventsToSend; ++i) { argumentCaptor.getValue().onAccuracyChanged(mockSensor, SensorManager.SENSOR_STATUS_ACCURACY_HIGH); } return true; } }); rxSensorManager.observeSensorAccuracy(Sensor.TYPE_ACCELEROMETER, 0) .test() .assertValueCount(numberOfEventsToSend); }
@NonNull public static Observable<SensorEvent> observeSensorChanged(final SensorManager sensorManager, final Sensor sensor, final int samplingPeriodUs) { return Observable.fromEmitter(new Action1<AsyncEmitter<SensorEvent>>() { @Override public void call(final AsyncEmitter<SensorEvent> sensorEventAsyncEmitter) { final SensorEventListener sensorListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent sensorEvent) { sensorEventAsyncEmitter.onNext(sensorEvent); } @Override public void onAccuracyChanged(Sensor originSensor, int i) { // do nothing } }; sensorEventAsyncEmitter.setCancellation(() -> sensorManager.unregisterListener(sensorListener, sensor)); sensorManager.registerListener(sensorListener, sensor, samplingPeriodUs); } }, AsyncEmitter.BackpressureMode.LATEST); }
@Override public void setup() { eventListener = new SensorEventListener() { @Override public void onSensorChanged(final SensorEvent sensorEvent) { update(new HashMap<String, Object>() {{ put("value", sensorEvent.values[0]); }}); } @Override public void onAccuracyChanged(Sensor sensor, int i) { } }; LinkService.INSTANCE.sensorManager.registerListener(eventListener, LinkService.INSTANCE.sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_NORMAL); setEnabled(); }
@Override public void setup() { eventListener = new SensorEventListener() { @Override public void onSensorChanged(final SensorEvent sensorEvent) { update(new HashMap<String, Object>() {{ put("X", sensorEvent.values[0]); put("Y", sensorEvent.values[1]); put("Z", sensorEvent.values[2]); }}); } @Override public void onAccuracyChanged(Sensor sensor, int i) { } }; LinkService.INSTANCE.sensorManager.registerListener(eventListener, LinkService.INSTANCE.sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL); setEnabled(); }
@Implementation public boolean listenSensor() { listener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; shadow.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL); if (shadow.hasListener(listener)) { return true; } else { return false; } }
/** * Method to enable disable Accelerometer Sensor in Android */ public void disableAccelerometerListener(SensorEventListener listenerFromActivity) { try { if(listenerFromActivity!=null) mSensorManager.unregisterListener(listenerFromActivity); else { if(enableDebugging) Log.d(TAG,"listenerFromActivity is null"); } } catch(NullPointerException e) { e.printStackTrace(); } }
/** * Method to disable Light Sensor in Android * @param listenerFromActivity * */ public void disableLightSensor(SensorEventListener listenerFromActivity) { try { if(mSensorManager != null) { if(listenerFromActivity!=null) mSensorManager.unregisterListener(listenerFromActivity); else { if(enableDebugging) Log.d(TAG,"listenerFromActivity is null"); } } } catch(NullPointerException e) { e.printStackTrace(); } }
/** * Method to disable Proximity Sensor in Android */ public void disableProximitySensor(SensorEventListener listenerFromActivity) { try { if(mSensorManager != null) { if(listenerFromActivity!=null) mSensorManager.unregisterListener(listenerFromActivity); else { if(enableDebugging) Log.d(TAG,"listenerFromActivity is null"); } } } catch(NullPointerException e) { e.printStackTrace(); } }
/** * Method to enable disable Gyroscope Sensor in Android */ public void disableGyroscopeListener(SensorEventListener listenerFromActivity) { try { if(listenerFromActivity!=null) mSensorManager.unregisterListener(listenerFromActivity); else { if(enableDebugging) Log.d(TAG,"listenerFromActivity is null"); } } catch(NullPointerException e) { e.printStackTrace(); } }
/** * Method to enable disable Magnetometer Sensor in Android */ public void disableMagnetometerListener(SensorEventListener listenerFromActivity) { try { if(listenerFromActivity!=null) mSensorManager.unregisterListener(listenerFromActivity); else { if(enableDebugging) Log.d(TAG,"listenerFromActivity is null"); } } catch(NullPointerException e) { e.printStackTrace(); } }
/** * To un-register the Gyroscope */ public final void unregisterMagnetometerService(SensorEventListener listenerfromMainApp) throws MagnetometerSensorException { if(listenerfromMainApp!=null) { if(gyroscope!=null) { gyroscope.disableGyroscopeListener(listenerfromMainApp); CAFConfig.setSensorGyroscope(false); if(enableDebugging) Log.d(TAG,"Unregister Gyroscope Sensor"); } } else { Log.d(TAG,"listenerfromMainApp is null"); } }
/** * To un-register the Gyroscope */ public final void unregisterGyroscopeService(SensorEventListener listenerfromMainApp) throws GyrometerSensorException { if(listenerfromMainApp!=null) { if(gyroscope!=null) { gyroscope.disableGyroscopeListener(listenerfromMainApp); CAFConfig.setSensorGyroscope(false); if(enableDebugging) Log.d(TAG,"Unregister Gyroscope Sensor"); } } else { Log.d(TAG,"listenerfromMainApp is null"); } }
/** * To un-register the Accelerometer */ public final void unregisterAccelerometerService(SensorEventListener listenerfromMainApp) throws AccelerometerSensorException { if(listenerfromMainApp!=null) { if(accel!=null) { accel.disableAccelerometerListener(listenerfromMainApp); CAFConfig.setSensorAccelerometer(false); if(enableDebugging) Log.d(TAG,"Unregister Accelerometer Sensor"); } } else { Log.d(TAG,"listenerfromMainApp is null"); } }
/** * To un-register the Proximity Service */ public final void unregisterProximityService(SensorEventListener listenerfromMainApp) throws ProximitySensorException { if(listenerfromMainApp!=null) { if(proximity!=null) { proximity.disableProximitySensor(listenerfromMainApp); CAFConfig.setSensorProximity(false); if(enableDebugging) Log.d(TAG,"Unregister Proximity Sensor"); } } else { Log.d(TAG,"listenerfromMainApp is null"); } }
/** * To un-register the Light sensor Service */ public final void unregisterLightService(SensorEventListener listenerfromMainApp) throws LightSensorException { if(listenerfromMainApp!=null) { if(light!=null) { light.disableLightSensor(listenerfromMainApp); CAFConfig.setSensorLight(false); if(enableDebugging) Log.d(TAG,"Unregister Light Sensor"); } } else { Log.d(TAG,"listenerfromMainApp is null"); } }
/** * Method to disable Gyroscope Sensor in Android */ public void disableGyroscopeListener(SensorEventListener listenerFromActivity) { try { if(listenerFromActivity!=null) { mSensorManager.unregisterListener(listenerFromActivity); //if(enableDebugging) //Log.d(TAG,"unregistering Gyroscope Sensor"); } else { if(enableDebugging) Log.d(TAG,"listenerFromActivity is null"); } } catch(NullPointerException e) { e.printStackTrace(); } }
/** * To un-register the Magnetometer */ public final void unregisterMagnetometerService(SensorEventListener listenerfromMainApp) throws MagnetometerSensorException { if(listenerfromMainApp!=null) { if(magnetometer!=null) { magnetometer.disableMagnetometerListener(listenerfromMainApp); CAFConfig.setSensorMagnetometer(false); if(enableDebugging) Log.d(TAG,"Unregister Magnetometer Sensor"); } } else { Log.d(TAG,"listenerfromMainApp is null"); } }
/** * Registers to the orientation sensors of the phone */ public void registerToSensors() { mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor orientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); mSensorManager.registerListener(new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { //if the Start button is released, do not send data to the server if (mSendData) { jsonQuadruple = new Quadruple<>(COORDINATES, event.values[0], event.values[2], event.values[1]); jsonIntent.putExtra("json", JSONUtils.serialize(jsonQuadruple, jsonQuadrupleTypes)); sendBroadcast(jsonIntent); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL); }
public void startSensor() { pressure_hPa_filter_ = new KalmanFilter(KF_VAR_ACCEL); sensor_manager_ = (SensorManager) getSystemService(SENSOR_SERVICE); sensor_pressure_ = sensor_manager_.getDefaultSensor(Sensor.TYPE_PRESSURE); pressure_hPa_ = 1013.0912; pressure_hPa_filter_.reset(pressure_hPa_); last_measurement_time_ = SystemClock.elapsedRealtime() / 1000.; // Start listening to the pressure sensor. mSensorListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { pressure_hPa_=event.values[0]; } public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; }