Last Updated on August 21, 2023
Hi and welcome to another tutorial from Codingdemos. In this tutorial, you will learn how to Android enable Bluetooth programmatically. Open Android Studio and start coding 🙂
By the end of this tutorial, you will have an app that looks like this. (Large preview)
In this tutorial we will be using the following:
Android Studio version 4.1.2
Minimum SDK API 23
Please make sure to use a physical device for testing instead of the emulator.
1- Open Android Studio.
Android studio 4.1.2 welcome screen. (Large preview)
2- Open activity_main.xml file, and add a Android button. This button will be used to enable and disable Bluetooth.
< Button android:id="@+id/btnBlue" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
3- Now open MainActivity.java file, and here you will declare and initialize the button, as well as you will be doing most of the work in enabling and disabling the bluetooth.
Button buttonBlue = findViewById(R.id.btnBlue);
4- Before you connect the device to Bluetooth, you will need to check whether the device supports Bluetooth. You can perform this type of checking using one of the Android functions called BluetoothAdapter. You will show an Android toast message to the user when the device doesn’t support Bluetooth.
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null){
Toast.makeText(MainActivity.this, "This device doesn't support Bluetooth",
Toast.LENGTH_LONG).show();
}
5- Android Studio will give a warning for the BluetoothAdapter that request you to include the following permission inside the AndroidManifest.xml file.
< uses-permission android:name="android.permission.BLUETOOTH" />
6- Next, you will need to perform another checking whether the device is currently connected or not to Bluetooth. You will change the label of buttonBlue based on the Bluetooth connection status.
if(!bluetoothAdapter.isEnabled()){
buttonBlue.setText("Turn Bluetooth ON");
}else{
buttonBlue.setText("Turn Bluetooth OFF");
}
7- Let’s work on making the button clickable so that you can turn ON/OFF the Bluetooth.
buttonBlue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
8- Inside the button onClick method, you will check whether or not the device is connected to Bluetooth. You will turn the Bluetooth ON using Android intent and startActivityForResult
if(!bluetoothAdapter.isEnabled()){
Intent blueToothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(blueToothIntent, BLUETOOTH_REQ_CODE);
}
9- If the device is currently connected to Bluetooth, you can turn it OFF and change the label of the button to indicate that the status is OFF.
bluetoothAdapter.disable();
buttonBlue.setText("Turn Bluetooth ON");
10- Android Studio will give another warning message for bluetoothAdapter.disable() that request you to include the following permission inside the AndroidManifest.xml file.
< uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
11- Now you will need to override onActivityResult. Here you will check the status of the device whether or not it’s successfully connected to Bluetooth by showing a simple toast message to the user.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Toast.makeText(MainActivity.this, "Bluetooth is ON", Toast.LENGTH_SHORT).show();
buttonBlue.setText("Turn Bluetooth OFF");
}else
if(resultCode == RESULT_CANCELED){
Toast.makeText(MainActivity.this, "Bluetooth operation is cancelled",
Toast.LENGTH_SHORT).show();
}
}
12- Now, build and run the app to see the result.
Android connect to bluetooth device programmatically. (Large preview)
Enable Android Bluetooth on physical device. (Large preview)
13- Here is the full code for activity_main.xml file.
< ?xml version="1.0" encoding="utf-8"?>
< androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
< Button android:id="@+id/btnBlue" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
< /androidx.constraintlayout.widget.ConstraintLayout>
14- Here is the full code for MainActivity.java file.
package com.codingdemos.codingdemos;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public static final int BLUETOOTH_REQ_CODE = 1;
Button buttonBlue;
BluetoothAdapter bluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonBlue = findViewById(R.id.btnBlue);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null){
Toast.makeText(MainActivity.this, "This device doesn't support Bluetooth",
Toast.LENGTH_LONG).show();
}
if(!bluetoothAdapter.isEnabled()){
buttonBlue.setText("Turn Bluetooth ON");
}else{
buttonBlue.setText("Turn Bluetooth OFF");
}
buttonBlue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!bluetoothAdapter.isEnabled()){
Intent blueToothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(blueToothIntent, BLUETOOTH_REQ_CODE);
}else{
bluetoothAdapter.disable();
buttonBlue.setText("Turn Bluetooth ON");
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Toast.makeText(MainActivity.this, "Bluetooth is ON", Toast.LENGTH_SHORT).show();
buttonBlue.setText("Turn Bluetooth OFF");
}else
if(resultCode == RESULT_CANCELED){
Toast.makeText(MainActivity.this, "Bluetooth operation is cancelled",
Toast.LENGTH_SHORT).show();
}
}
}
15- I hope you find this tutorial helpful, and if you have any questions, please post them in the comment below.