Open In App
Related Articles

How to Programmatically Enable/Disable Bluetooth in Android?

Improve Article
Save Article
Like Article

In Android Phone, it is very much easy to enable/disable Bluetooth by using the Bluetooth icon, but have you wondered how to do this task programmatically in Android. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Sample GIF

Steps for Programmatically Enable/Disable Bluetooth 

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the AndroidManifest.xml file

Go to the AndroidManifest.xml file and add two user-permissions: BLUETOOTH and BLUETOOTH_ADMIN.

<uses-permission android:name=”android.permission.BLUETOOTH”/>

<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>

Below is the code for the AndroidManifest.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--Put the permissions between the manifest and application opening tags-->
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Step 3: Working with the activity_main.xml file

When the setup is ready, go to the activity_main.xml file, which represents the UI of the project. Create a Button that changes the state of the Bluetooth on click and a TextView that shows the status of the Bluetooth state. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.


<?xml version="1.0" encoding="utf-8"?>
    <!--Changes the state of Bluetooth on button click-->
        android:text="Click" />
    <!--Displays the state of Bluetooth on button click-->
        android:hint="Bluetooth Status"
        android:textSize="30sp" />

Step 4: Working with the MainActivity.kt file

In the MainActivity.kt file, declare the Button, TextView, and a Bluetooth adapter (refer to the codes). While setting the on click listeners to the button, use the Bluetooth adapter to enable or disable the Bluetooth. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


import android.bluetooth.BluetoothAdapter
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring Button and TextView
        // 1. Changes the state of Bluetooth on button click
        // 2. Shows the state of the Bluetooth
        val btnBt = findViewById<Button>(
        val tvBt = findViewById<TextView>(
        // Declaring Bluetooth adapter
        val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
        // On button Click
        btnBt.setOnClickListener {
            // Enable or disable the Bluetooth and display
              // the state in Text View
            if (mBluetoothAdapter.isEnabled) {
                tvBt.text = "Bluetooth is OFF"
            } else {
                tvBt.text = "Bluetooth is ON"

Output: Run on Physical Device

Last Updated : 14 Oct, 2020
Like Article
Save Article
Similar Reads