How to Programmatically Enable/Disable Bluetooth in Android?
Last Updated :
14 Oct, 2020
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.

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
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wifi">
<!--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"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--Changes the state of Bluetooth on button click-->
<Button
android:id="@+id/BtBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click" />
<!--Displays the state of Bluetooth on button click-->
<TextView
android:id="@+id/BtTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/BtBtn"
android:layout_centerHorizontal="true"
android:hint="Bluetooth Status"
android:textSize="30sp" />
</RelativeLayout>
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.
Kotlin
import android.bluetooth.BluetoothAdapter
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring Button and TextView
// 1. Changes the state of Bluetooth on button click
// 2. Shows the state of the Bluetooth
val btnBt = findViewById<Button>(R.id.BtBtn)
val tvBt = findViewById<TextView>(R.id.BtTv)
// 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) {
mBluetoothAdapter.disable()
tvBt.text = "Bluetooth is OFF"
} else {
mBluetoothAdapter.enable()
tvBt.text = "Bluetooth is ON"
}
}
}
}
Output: Run on Physical Device
Similar Reads
How to Programmatically Enable/Disable Wi-Fi in Android? In Android Phone, it is very much easy to enable/disable WiFi by using the WiFi 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 usin
3 min read
How to Display Bluetooth Paired Devices Programmatically in Android? Bluetooth's technology is a high-speed, low-powered wireless technology link designed to connect devices such as phones or other portable equipment. It has a specification (IEEE 802.15.1) for low-power radio communications to link computers, phones, and other network devices over a short distance in
5 min read
How to Disable Dark Mode Programmatically in Android? In this article, we will learn how to disable Dark Mode programmatically in Android. Changing the theme of your app is a simple task that includes changing the code in the XML file. Step by Step Implementation Step 1: Create a New Project in Android Studio Create a new project by clicking on the fil
2 min read
How to Enable/Disable Button in Android? The Enable/Disable Feature is used in many Android apps so the basic use of that feature is to prevent the user from clicking on a button until they will add all the required fields that have been asked. We are considering an example of email and password if the user has entered the email and passwo
3 min read
How to Check the Battery Level in Android Programmatically? Sometimes, it is useful to determine the current battery level. One may choose to reduce the rate of your background updates if the battery charge is below a certain level. But one cannot continuously monitor the battery state. In general, the impact of constantly monitoring the battery level has a
2 min read
How to Make Bluetooth Discoverable to Other Devices in Android? In Android devices, Bluetooth is a communication network protocol that allows devices to connect wirelessly to exchange data with other Bluetooth devices. In general, we can make use of Bluetooth API to implement Bluetooth functionalities, such as enabling or disabling Bluetooth, searching for avail
5 min read
How to Detect Tablet or Phone in Android Programmatically? A Mobile is a portable electronic device that allows you to make calls, send messages, and access the internet, among other functions. A tablet is a mobile computing device with a touchscreen display and typically a larger screen size than a smartphone. Both devices are designed to be portable and a
3 min read
How to Check GPS is On or Off in Android Programmatically? GPS (Global Positioning System) is a satellite-based navigation system that accommodates radio signals between satellite and device to process the device's location in the form of coordinates. GPS gives latitude and longitude values of the device. Recent mobile phones are equipped with GPS modules t
2 min read
How to programmatically hide Android soft keyboard In this article, we will learn about how to hide soft keyboard programmatically. The keyboard generally hides but there are certain instances when it does not hide. So for better user experience, the keyboard is hidden programmatically. Without using below approach, the default response of the app i
2 min read
How to Invoke Keyboard Programmatically in Android? Android System by defaults shows an on-screen keyboard when any UI element such as an Input Text element receives focus. For a better experience, a developer can explicitly specify the desired characteristics or any methods to invoke. Desired characteristics could be characters such as allowing only
2 min read