Download Code

Xamarin Bluetooth Example Android

In this tutorial you will learn how to implement/access Bluetooth through xamarin bluetooth example android application.Android provides bluetooth API to perform certain operations.

  • Get Paired devices
  • Set Bluetooth Name
  • Get Bluetooth Name
  • Scan for devices
  • Connect with other devices

Bluetooth

The Android platform includes support for the network stack, which allows a device to wirelessly exchange data with other Bluetooth_devices.To learn in detail go to Official Website.

Bluetooth Adapter

Represents the local Bluetooth adapter. The BluetoothAdapter is the entry-point for all  interaction. Using this, you can discover other devices, query a list of paired devices.

Provides different constants like.

  • ActionRequestDiscoverable
  • ActionRequestEnable

Also provide different methods to configure Bluetooth.

  • Name
  • SetName(String name)
  • Enable()
  • IsEnabled()
  • Disable()

Ok,Create new xamarin bluetooth example android application project.Go to Main.axml and add following components.

  • EditText
  • Buttons
  • ListView

Main.axml

xamarin bluetooth,xamarin bluetooth example,xamarin bluetooth example android,bluetooth example android,

Next go to MainActivity.cs and do the following steps.

  • Create and refer UI Components(EditText,Button).
  • Apply click event on Buttons.
  • Create bluetoothadapter variable and get default adapter.
  • Then perform operations(enable,disable etc).

MainActivity.cs

namespace XamarinBluetooth
{
	[Activity(Label = "XamarinBluetooth", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity, Android.Views.View.IOnClickListener
	{

		Button onBtn, offBtn, visibleBtn, listBtn, change_Bluetooth_Name, display_Name;
		BluetoothAdapter blue;              // Bluetooth adapter class variable

		ListView list_Of_Devices;           // list view for paired devices
		EditText bluetoothName;            // user bluetooth name edit text

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

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			initialize();
			blue = BluetoothAdapter.DefaultAdapter;       // getting bluetooth default adapter
		}
		public void initialize()
		{
			onBtn = (Button)FindViewById(Resource.Id.onB);
			offBtn = (Button)FindViewById(Resource.Id.offB);
			visibleBtn = (Button)FindViewById(Resource.Id.visibleD);
			listBtn = (Button)FindViewById(Resource.Id.pairD);
			bluetoothName = (EditText)FindViewById(Resource.Id.name);
			change_Bluetooth_Name = (Button)FindViewById(Resource.Id.nameBtn);
			display_Name = (Button)FindViewById(Resource.Id.showName);
			list_Of_Devices = (ListView)FindViewById(Resource.Id.list_devices);
			onBtn.SetOnClickListener(this);
			offBtn.SetOnClickListener(this);
			visibleBtn.SetOnClickListener(this);
			listBtn.SetOnClickListener(this);
			change_Bluetooth_Name.SetOnClickListener(this);
			display_Name.SetOnClickListener(this);
		}

		public void OnClick(View v)
		{
			switch (v.Id)
			{
				case Resource.Id.onB:
					if (!blue.Enable())
					{
						Intent o = new Intent(BluetoothAdapter.ActionRequestEnable);
						StartActivityForResult(o, 0);

					}
					Toast.MakeText(this, "Enabled", ToastLength.Short).Show();
					break;
				case Resource.Id.offB:
					if (blue.Enable())
					{
						blue.Disable();
						Toast.MakeText(this, "Bluetooth Disabled", ToastLength.Short).Show();
					}
					break;
				case Resource.Id.visibleD:
					Intent visible = new Intent(BluetoothAdapter.ActionRequestDiscoverable);
					StartActivityForResult(visible, 0);
					break;
				case Resource.Id.pairD:
					
					ArrayList list = new ArrayList();
					foreach (BluetoothDevice bt in blue.BondedDevices)
					{
						list.Add(bt.Name);
					}

					ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, list);
					list_Of_Devices.SetAdapter(adapter);
					break;
				case Resource.Id.nameBtn:
					if (!bluetoothName.Text.ToString().Equals(""))
					{
						String n = bluetoothName.Text.ToString();
						blue.SetName(n);
					}
					else {
						Toast.MakeText(this, "Please enter name", ToastLength.Short).Show();
					}
					break;
				case Resource.Id.showName:
					Toast.MakeText(this, blue.Name.ToString(), ToastLength.Short).Show();
					break;
			}
		}
	}
}

Go to Android Manifest.xml file and add some permissions.

xamarin bluetooth,xamarin bluetooth example,xamarin bluetooth example android,bluetooth example android,

Run your xamarin bluetooth example android application project.

xamarin bluetooth,xamarin bluetooth example,xamarin bluetooth example android,bluetooth example android,

That’s it for xamarin bluetooth example android tutorial.

For Java example visit AndroidCodec.

Thanks