android.hardware.SensorEventListener Java Examples

The following examples show how to use android.hardware.SensorEventListener. 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: RxSensor.java    From AndroidSchool with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void listing16_13() {
  // Listing 16-13: Calculating an orientation change using the gyroscope Sensor
  final float nanosecondsPerSecond = 1.0f / 100000000.0f;
  final float[] angle = new float[3];

  SensorEventListener myGyroListener = new SensorEventListener() {
    public void onSensorChanged(SensorEvent sensorEvent) {
      if (lastTime != 0) {
        final float dT = (sensorEvent.timestamp - lastTime) *
                           nanosecondsPerSecond;
        angle[0] += sensorEvent.values[0] * dT;
        angle[1] += sensorEvent.values[1] * dT;
        angle[2] += sensorEvent.values[2] * dT;
      }
      lastTime = sensorEvent.timestamp;
    }

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

  SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

  int sensorType = Sensor.TYPE_GYROSCOPE;
  sm.registerListener(myGyroListener, sm.getDefaultSensor(sensorType), SensorManager.SENSOR_DELAY_NORMAL);
}
 
Example #3
Source File: GravitySensor.java    From SensorsAndAi with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gravity_sensor);
    //FOR CREATING SENSOR MANAGER
    SM =(SensorManager)getSystemService(SENSOR_SERVICE);
    //ACCELEROMETER
    mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    //REGISTER SENSOR LISTENER
    SM.registerListener((SensorEventListener) this,mySensor,SensorManager.SENSOR_DELAY_NORMAL);


    //ASSIGN TEXTVIEW
    xText =(TextView) findViewById(R.id.xtext);
    yText =(TextView) findViewById(R.id.ytext);
    zText =(TextView) findViewById(R.id.ztext);
}
 
Example #4
Source File: API24.java    From VirtualSensor with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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);
    }
}
 
Example #5
Source File: API23.java    From VirtualSensor with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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);
    }
}
 
Example #6
Source File: Reminders.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: SKAbstractNativeSensorModule.java    From SensingKit-Android with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected SKAbstractNativeSensorModule(final Context context, final SKSensorModuleType sensorModuleType) throws SKException {
    super(context, sensorModuleType);

    mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(getSensorType(sensorModuleType));

    mSensorEventListener = new SensorEventListener() {

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // Ignore
        }

        @Override
        public void onSensorChanged(SensorEvent event) {

            // Build the data object
            SKAbstractData data = buildData(event);

            // Submit sensor data object
            submitSensorData(data);
        }
    };
}
 
Example #8
Source File: AeyriumSensorPlugin.java    From aeyrium-sensor with MIT License 6 votes vote down vote up
SensorEventListener createSensorEventListener(final EventChannel.EventSink events) {
  return new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
      if (mLastAccuracy != accuracy) {
        mLastAccuracy = accuracy;
      }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
      if (mLastAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
        return;
      }

      updateOrientation(event.values, events);
    }
  };
}
 
Example #9
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 #10
Source File: DeviceMotionAndOrientation.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void unregisterListener(SensorEventListener listener, int sensorType) {
    List<Sensor> sensors = mSensorManager.getSensorList(sensorType);
    if (!sensors.isEmpty()) {
        mSensorManager.unregisterListener(listener, sensors.get(0));
    }
}
 
Example #11
Source File: PedometerPlugin.java    From flutter-plugins with MIT License 5 votes vote down vote up
SensorEventListener createSensorEventListener(final EventChannel.EventSink events) {
  return new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    @Override
    public void onSensorChanged(SensorEvent event) {
      int stepCount = (int) event.values[0];
      events.success(stepCount);
    }
  };
}
 
Example #12
Source File: BrightnessTracker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public void registerSensorListener(Context context,
        SensorEventListener sensorListener, Handler handler) {
    SensorManager sensorManager = context.getSystemService(SensorManager.class);
    Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    sensorManager.registerListener(sensorListener,
            lightSensor, SensorManager.SENSOR_DELAY_NORMAL, handler);
}
 
Example #13
Source File: LightPlugin.java    From flutter-plugins with MIT License 5 votes vote down vote up
SensorEventListener createSensorEventListener(final EventChannel.EventSink events) {
  return new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    @Override
    public void onSensorChanged(SensorEvent event) {
      int lux = (int) event.values[0];
      events.success(lux);
    }
  };
}
 
Example #14
Source File: PGyroscope.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            ReturnObject r = new ReturnObject();
            r.put("x", event.values[0]);
            r.put("y", event.values[1]);
            r.put("z", event.values[2]);
            mCallback.event(r);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #15
Source File: LightSensorUtil.java    From camerademo with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * 反注册光线传感器监听器
 * @param sensorManager
 * @param listener
 */
public static void unregisterLightSensor(SensorManager sensorManager,SensorEventListener listener) {
    if(sensorManager == null || listener == null){
        return;
    }
    sensorManager.unregisterListener(listener);
}
 
Example #16
Source File: PGravity.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (mCallback != null) {
                ReturnObject r = new ReturnObject();
                r.put("x", event.values[0]);
                r.put("y", event.values[1]);
                r.put("z", event.values[2]);
                float force = (float) Math.sqrt(Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2));
                r.put("force", force);
                mCallback.event(r);
            }
        }


        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #17
Source File: PLightIntensity.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Start the light sensor. Returns the intensity. The value per device might vary", example = "")
@PhonkMethodParam(params = {"function(intensity)"})
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            ReturnObject r = new ReturnObject();
            r.put("intensity", event.values[0]);
            mCallbackLightChange.event(r);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #18
Source File: PMagneticField.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Start the magneticField sensor", example = "")
@PhonkMethodParam(params = {"function(value)"})
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            ReturnObject r = new ReturnObject();
            r.put("x", event.values[0]);
            r.put("y", event.values[1]);
            r.put("z", event.values[2]);
            mCallbackMagneticChange.event(r);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #19
Source File: PProximity.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Start the proximity sensor. Returns a proximity value. It might differ per device", example = "")
@PhonkMethodParam(params = {"function(proximity)"})
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            ReturnObject r = new ReturnObject();
            r.put("distance", event.values[0]);
            mCallback.event(r);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #20
Source File: PLinearAcceleration.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (mCallback != null) {
                ReturnObject r = new ReturnObject();
                r.put("x", event.values[0]);
                r.put("y", event.values[1]);
                r.put("z", event.values[2]);
                float force = (float) Math.sqrt(Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2));
                r.put("force", force);
                mCallback.event(r);
            }
        }


        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #21
Source File: PAccelerometer.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (mCallback != null) {
                ReturnObject r = new ReturnObject();
                r.put("x", event.values[0]);
                r.put("y", event.values[1]);
                r.put("z", event.values[2]);
                float force = (float) Math.sqrt(Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2));
                r.put("force", force);
                mCallback.event(r);
                MLog.d(TAG, "accelerometer changed");
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #22
Source File: PRotationVector.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (mCallback != null) {
                ReturnObject r = new ReturnObject();
                r.put("x", event.values[0]);
                r.put("y", event.values[1]);
                r.put("z", event.values[2]);
                mCallback.event(r);
            }
        }


        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #23
Source File: PHumidity.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Start the proximity sensor. Returns a proximity value. It might differ per device", example = "")
@PhonkMethodParam(params = {"function(proximity)"})
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            ReturnObject r = new ReturnObject();
            r.put("humidity", event.values[0]);
            mCallback.event(r);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #24
Source File: PStep.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Start the step counter. Not superacurate and only few devices", example = "")
@PhonkMethodParam(params = {"function(value)"})
public void start() {
    super.start();

    mListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            mCallback.event(null);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #25
Source File: PBarometer.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public void start() {
    super.start();

    mListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            ReturnObject r = new ReturnObject();
            r.put("bar", event.values[0]);
            mCallback.event(r);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            switch (accuracy) {
                case SensorManager.SENSOR_STATUS_UNRELIABLE:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
                    break;
                case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
                    break;
            }
        }

    };

    isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
 
Example #26
Source File: DeviceMotionAndOrientation.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean registerListener(SensorEventListener listener, int sensorType, int rate,
        Handler handler) {
    List<Sensor> sensors = mSensorManager.getSensorList(sensorType);
    if (sensors.isEmpty()) {
        return false;
    }
    return mSensorManager.registerListener(listener, sensors.get(0), rate, handler);
}
 
Example #27
Source File: SensorEventsController.java    From applivery-android-sdk with Apache License 2.0 5 votes vote down vote up
private void sensorAction(int action, AndroidSensorWrapper androidSensorWrapper) {
  SensorEventListener sensorEventListener = androidSensorWrapper.getSensorEventListener();
  Sensor sensor = sensorManager.getDefaultSensor(androidSensorWrapper.getAndroidSensorType());
  //if (action == REGISTER && androidSensorWrapper.isEnabled()){
  if (action == REGISTER){
    sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    androidSensorWrapper.setRegistered(true);
  }else if (action == UNREGISTER){
    //}else if (action == UNREGISTER
    //    && androidSensorWrapper.isEnabled() && androidSensorWrapper.isRegistered()){
    sensorManager.unregisterListener(sensorEventListener, sensor);
    androidSensorWrapper.setRegistered(false);
  }
}
 
Example #28
Source File: SensorDataManager.java    From Sensor-Data-Logger with Apache License 2.0 5 votes vote down vote up
public SensorEventListener getSensorEventListener(int sensorType) {
    SensorEventListener sensorEventListener = sensorEventListeners.get(sensorType);
    if (sensorEventListener == null) {
        sensorEventListener = createSensorEventListener(sensorType);
        sensorEventListeners.put(sensorType, sensorEventListener);
    }
    return sensorEventListener;
}
 
Example #29
Source File: DeviceSensors.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void unregisterListener(SensorEventListener listener, int sensorType) {
    List<Sensor> sensors = mSensorManager.getSensorList(sensorType);
    if (sensors.isEmpty()) {
        return;
    }
    try {
        mSensorManager.unregisterListener(listener, sensors.get(0));
    } catch (IllegalArgumentException e) {
        // Suppress occasional exception on Digma iDxD* devices:
        // Receiver not registered: android.hardware.SystemSensorManager$1
        // See crbug.com/596453.
        Log.w(TAG, "Failed to unregister device sensor " + sensors.get(0).getName());
    }
}
 
Example #30
Source File: DeviceSensors.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public boolean registerListener(
        SensorEventListener listener, int sensorType, int rate, Handler handler) {
    List<Sensor> sensors = mSensorManager.getSensorList(sensorType);
    if (sensors.isEmpty()) {
        return false;
    }
    return mSensorManager.registerListener(listener, sensors.get(0), rate, handler);
}