Get data from Linear Acceleration Sensor

Hey there,

as you can read in the title, I am looking for a way to get data from the linear acceleration sensor. At the moment I am using the "simple" acceleration, but it contains the gravity (details).
Is there a way to get it?

I am new on Xamarin, so this is hopefully not a stupid question.
Thanks for help.
Sam

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    It is not different from the gravity sensor, the only thing you need to change is the type of the sensor when you ask the SensorManager for the sensor.

    So in your Activity you request the SensorManager with:

    sensorManager = (SensorManager)GetSystemService (SensorService);
    

    Then you get the actual Sensor with:

    linearSensor = sensorManager.GetDefaultSensor (SensorType.LinearAcceleration);
    

    Then you need the Activity to implement ISensorEventListener:

    public void OnSensorChanged (SensorEvent e)
    {
        if (e.Sensor.Type != SensorType.LinearAcceleration)
            return;
    
        // values are in e.Values[0..2]
        // handle them here
    }
    

    Then somewhere, when you want to start listening to the sensor changes you fire:

    sensorManager.RegisterListener(this, linearSensor, SensorDelay.Ui);
    

    And when you don't want to listen to them anymore:

    sensorManager.UnregisterListener(this);
    
  • samuellersamueller USMember

    Thanks a lot for your help, Cheesebaron.
    I just had a problem with my API Level. It was to low and didnt support these types of sensors. Your example showed me that it has to work like this. Thanks again.

    PS:
    Sorry for my English!

Sign In or Register to comment.