So, I have been working on fixing this bug in my Android Compass for 2 days now. Basically I am trying to build a Qibla Compass using Xamarin.Android . The First step should be to build a Compass that points to North.
I have translated the JAVA code from this tutorial for my Compass to show proper North .
I am simply rotating my Relative Layout which contains my Compass Image whenever Sensor reading Changes.
I have tried tweaking code by intuition but this simple Compass doesn't love staying at one place. I am just trying to hold the North of my Compass Image aligned to the Magnetic north.
However my compass never shows the correct North also the North keeps changing.
To check my device Compass indeed works I tested an App from Playstore and it ran pretty smooth.
My Activity :
public class QiblaDirectionActivity : Activity, ISensorEventListener
//all the right inherits done
My design code Snippet :
<RelativeLayout
android:layout_width="wrap_content"
android:id="@+id/qiblaRL"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/qiblaCompass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_centerInParent="true"
android:src="@drawable/compass" />
<ImageView
android:id="@+id/qiblaArrow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_centerInParent="true"
android:src="@drawable/needle" />
</RelativeLayout>
My BackEnd :
float currentCompassDegree = 0f;
public void OnSensorChanged(SensorEvent e)
{
//I have checked the accuracy its high
RotateAnimation ra = new RotateAnimation(currentCompassDegree,-e.Values[0], Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);
ra.Duration = 120;
ra.FillAfter = true;
qiblaRL.StartAnimation(ra);
currentCompassDegree = -e.Values[0];
}
I register my listener here :
protected override void OnResume()
{
base.OnResume();
if (_sensorManager != null)
{
var mF = _sensorManager.GetDefaultSensor(SensorType.MagneticField);
_sensorManager.RegisterListener(this, mF, SensorDelay.Ui);
qiblaAdvice.Text= "(Align Device Properly)";
}
else
{
qiblaAdvice.Text = "(Device doesn't support Compass)";
}
}
And I unregister it here :
protected override void OnPause()
{
base.OnPause();
if (_sensorManager != null)
{
_sensorManager.UnregisterListener(this);
}
}