correcting gravity error in acceleration sensor when simulate rotation of map according to reality using Google Maps

I create an application in Android Studio and in the app I added a map that rotates according to the rotation of the device in reality using the accelerometer sensor and animateCamera () function which is called within the OnSensorChange () function.

Everything works right on Walking But on Car the map loses direction and rotate is not match reality when I travel more than 30 km (20 miles).

I searched for answers everywhere and found that the solution is using the High-pass Filter which filters the effect of gravity.

I can not implement the solution so my problem will be solved.

This is the information I have found on the Internet about the problem:

Android Low pass filter and High pass filter

I attach some relevant code parts and I will be happy to help you solve the problem. image

private SensorManager mSensorManager;
private GoogleMap mMap;

    public void onSensorChanged(SensorEvent event) {

    bearing = Math.round(event.values[0]); 

    private void updateCamera(float brng) {
    CameraPosition oldPos = mMap.getCameraPosition();

    CameraPosition pos = CameraPosition.builder(oldPos).bearing(brng)



    protected void onResume() {

    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),

public void onAccuracyChanged(Sensor sensor, int accuracy) {