Download Code

Android Bluetooth Example

In android bluetooth example you will learn how to implement/access Bluetooth through android application.Android provides 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.

  • ACTION_REQUEST_DISCOVERABLE
  • ACTION_REQUEST_ENABLE

Also provide different methods to configure Bluetooth.

  • getName()
  • setName(String name)
  • enable()
  • isEnabled()
  • disable()

Ok,Create new android bluetooth example application project.Go to activity_main.xml and add following components.

  • EditText
  • Buttons
  • ListView

activity_main.xml

bluetooth,bluetooth android tutorial, bluetooth getName(),bluetooth setName(), bluetooth enable in android, BluetoothAdapter android, bluetooth paried devices android, bluetooth in android source code,android bluetooth,android bluetooth example,bluetooth example,

Next go to MainActivity.java.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.java

package developer.codecbluetooth;


public class MainActivity extends Activity implements OnClickListener{

	Button onBtn,offBtn,visibleBtn,listBtn,change_Bluetooth_Name,display_Name;
	BluetoothAdapter blue;				//  adapter class variable
	Set pairDevices;    // set of paired devices
	ListView list_Of_Devices;           // list view for paired devices
	EditText bluetoothName;            // user name edit text
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialize();
		blue = BluetoothAdapter.getDefaultAdapter();       // getting bluetooth_default adapter
	}
	public void initialize(){
		onBtn = (Button) findViewById(R.id.onB);
		offBtn = (Button) findViewById(R.id.offB);
		visibleBtn = (Button) findViewById(R.id.visibleD);
		listBtn = (Button) findViewById(R.id.pairD);
		bluetoothName = (EditText) findViewById(R.id.name);
		change_Bluetooth_Name = (Button) findViewById(R.id.nameBtn);
		display_Name = (Button) findViewById(R.id.showName);
		list_Of_Devices = (ListView) findViewById(R.id.list_devices);
		onBtn.setOnClickListener(this);
		offBtn.setOnClickListener(this);
		visibleBtn.setOnClickListener(this);
		listBtn.setOnClickListener(this);
		change_Bluetooth_Name.setOnClickListener(this);
		display_Name.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.onB:
			if(!blue.enable()){
				Intent o = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
				startActivityForResult(o, 0);

			}
			Toast.makeText(getApplicationContext(), "Enabled", Toast.LENGTH_SHORT).show();
			break;
		case R.id.offB:
			if(blue.enable()){
				blue.disable();
				Toast.makeText(getApplicationContext(), "Bluetooth_Disabled", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.visibleD:
			Intent visible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			startActivityForResult(visible, 0);
			break;
		case R.id.pairD:
			pairDevices = blue.getBondedDevices();
			ArrayList list = new ArrayList();
			for(BluetoothDevice bt : pairDevices)
			list.add(bt.getName());
			
			final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
			list_Of_Devices.setAdapter(adapter);
			break;
		case R.id.nameBtn:
			if(!bluetoothName.getText().toString().equals(""))
			{
				String n = bluetoothName.getText().toString();
				blue.setName(n);
			}
			else{
				Toast.makeText(getApplicationContext(), "Please enter name", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.showName:
			Toast.makeText(getApplicationContext(), blue.getName().toString(), Toast.LENGTH_SHORT).show();
			break;
		}
	}


}

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

bluetooth,bluetooth android tutorial, bluetooth getName(),bluetooth setName(), bluetooth enable in android, BluetoothAdapter android, bluetooth paried devices android, bluetooth in android source code,android bluetooth,android bluetooth example,bluetooth example,

Run your android bluetooth example application project.

bluetooth,bluetooth android tutorial, bluetooth getName(),bluetooth setName(), bluetooth enable in android, BluetoothAdapter android, bluetooth paried devices android, bluetooth in android source code,android bluetooth,android bluetooth example,bluetooth example,

That’s it for this tutorial.

Thanks