How to enable/disable bluetooth programmatically in android

In this android programming source code example, we are going to enable/disable bluetooth programmatically in android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to enable/disable bluetooth programmatically in android.

activity_main.xml

<?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=".BluetoothAndNavigation.BluetoothAndNavigationActivity2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/onBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Turn Bluetooth On"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/offBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@color/colorAccent"
            android:text="Turn Bluetooth Off"
            android:textColor="#ffffff" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class BluetoothAndNavigationActivity2 extends AppCompatActivity {
    private Button onBtn;
    private Button offBtn;
    private BluetoothAdapter bluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_and_navigation2);
        onBtn = (Button)findViewById(R.id.onBtn);
        offBtn = (Button)findViewById(R.id.offBtn);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        onBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bluetoothAdapter == null){
                    Toast.makeText(BluetoothAndNavigationActivity2.this, "Bluetooth not supported", Toast.LENGTH_LONG).show();
                }else{
                    if(!bluetoothAdapter.isEnabled()){
                        startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
                        Toast.makeText(BluetoothAndNavigationActivity2.this, "Bluetooth turned on", Toast.LENGTH_LONG).show();
                    }
                }

            }
        });

        offBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bluetoothAdapter.disable();
                Toast.makeText(BluetoothAndNavigationActivity2.this, "Bluetooth turned off", Toast.LENGTH_LONG).show();
            }
        });
    }
}

AndroidManifest.xml

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

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment