1

This is my first question in SO so I'll accept all the critics to improve my question. I am developing an Android App that needs to measure the distance to an object from the camera ( external camera, always in the same place ), with the help of others questions on this site I achieved this functionality on my Huawei P9, but when I try it on other devices the sensor doesn't call onSensorChanged(SensorEvent event).

I need this function to be called because I need the XYZ from the event .

This is how I declare my sensor and register it.

Sensor accelerometer;
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sm.registerListener(new SensorEventListener()
    {
        @Override
        public void onSensorChanged(SensorEvent event)
        {
            setXYZ(event.values[0], event.values[1], event.values[2]);

            int rotation = (int) Math.round(Math.toDegrees(Math.atan2(x, y)));

            if(checkDistance)
            {
                distance = getDistance();
                distanceOutput.setText(distance + "m");
                distanceAngleOutput.setText("Angle: " + distanceAngle);
            }
            if(checkHeight)
            {
                height = getHeight();
                heightOutput.setText(height + "m");
                heightAngleOutput.setText("Angle: " + heightAngle);

            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy)
        {
          // nothing here
        }
    }, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);

Any change or information you need in order to understand my problem I'll try to provide it to you.

FoxyError
  • 694
  • 1
  • 4
  • 19
Gerard E
  • 27
  • 9

1 Answers1

0

You have to register a listener for 'OnSensorChanged' in OnResume method, without registering a listener it not gonna be called.

Here an example how you can register listener for the

_sensorManager.RegisterListener(this, _accelerometer, SensorDelay.Normal);

it takes context , sansor type , sensor delay

Raouf
  • 21
  • 1
  • 4