Download Code

In this tutorial you’ll learn about sensors, sensorManager in android Xamarin. Let’s have a look upon sensors in android Xamarin.

Sensors

Most Android devices have built-in sensors.Measure motion, orientation, and various environmental conditions. These sensors capable of providing raw data with high precision and accuracy.Different categories of Sensor.

Xamarin Hardware and Software based Sensors
SensorManager

SensorManager allows access to device’s sensors. Get instance of class by calling Context.getSystemService() with argument SENSOR_SERVICE.

Alright create new android application project.Basically you will implement ACCELEROMETER which gives X,Y co-ordinates of device movement.

Don’t touch Main.axml because you don’t need it.

Sensors xamarin,sensors,sensors android xamarin,accelerometer android xamarin, SesnorManager xamarin android,SensorType, how to select sensor type in xamarin android, how to delay any sensor type xamarin android, onSensorChanged,onAccuracyChanged, how to register sensor in xamarin android, getdefaultsensor,getsystemservice,

Go to your MainActivity.cs class.

namespace XamarinSensor
{
	[Activity(Label = "XamarinSensor", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity,ISensorEventListener
	{

		SensorManager sm;
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
		}
	}
} 

Implement an interface called ISensorEventListener and add unimplemented methods.

You will create  SensorManager variable to access your device Sensors.After that register SensorListener.Get the values of movement in OnSensorChanged() method.

MainActivity.cs

namespace XamarinSensor
{
	[Activity(Label = "XamarinSensor", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity,ISensorEventListener
	{

		SensorManager sm;
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			sm = (SensorManager) this.GetSystemService(SensorService);
			if (sm.GetSensorList(SensorType.Accelerometer).Count != 0)
			{
				Sensor s = sm.GetDefaultSensor(SensorType.Accelerometer);
				sm.RegisterListener(this, s, SensorDelay.Normal);

			}


		}
		public void OnSensorChanged(SensorEvent e)
		{
			Toast.MakeText(this, e.Values[0] + " " + e.Values[1], ToastLength.Short).Show();
		}
		public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
		{
			throw new NotImplementedException();
		}




	}
}



Run android application project.When app is on,it will show you X,Y co-ordinates in Toast.

SensorManager,Sensors xamarin,sensors,sensors android xamarin,accelerometer android xamarin, SesnorManager xamarin android,SensorType, how to select sensor type in xamarin android, how to delay any sensor type xamarin android, onSensorChanged,onAccuracyChanged, how to register sensor in xamarin android, getdefaultsensor,getsystemservice,

That’s it for this tutorial.

Thanks.