How to Programmatically Enable/Disable Bluetooth in Android? - GeeksforGeeks

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. 
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.BluetoothAdapterimport android.os.Bundleimport android.widget.Buttonimport android.widget.TextViewimport 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"            }        }    }}
android
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
Services in Android with Example
CardView in Android With Example
Broadcast Receiver in Android With Example
Android UI Layouts
Android RecyclerView in Kotlin
Services in Android with Example