Check Bluetooth is turned on or off on Android device programmatically [Java Code]


If you are looking for code to programmatically check if Bluetooth is turned on or off on the Android device, then here are examples for both Java and Kotlin,

Java Example: Check if bluetoon is turned on or off

package com.code2care.demobluetooth;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class CheckBluetoothActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demobluetooth);

        Toast toast = Toast.makeText(CheckBluetoothActivity.this, 
		"Bluetooth Enabled: "+ isBluetoothEnabled() , Toast.LENGTH_SHORT);
        toast.show();

    }

    //This method checks if bluetooth is
    //enabled on the Android device or not
    public boolean isBluetoothEnabled(){
        BluetoothAdapter myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(myBluetoothAdapter.isEnabled()){
            return true;
        } else {
            return false;
        }
    }
}

Make sure you add the below permissions to access Bluetooth in your manifest XML file,

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

If you don't add the above line you will get the below error,

   java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.code2care.testing/com.code2care.testing.MainActivity}: 
java.lang.SecurityException: Need BLUETOOTH permission: 
Neither user 10123 nor current process has android.permission.BLUETOOTH.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2560)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2626)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)
     Caused by: java.lang.SecurityException: Need BLUETOOTH permission: 
   Neither user 10123 nor current process has android.permission.BLUETOOTH.
        at android.os.Parcel.readException(Parcel.java:1599)
        at android.os.Parcel.readException(Parcel.java:1552)
        at android.bluetooth.IBluetooth$Stub$Proxy.isEnabled(IBluetooth.java:853)
        at android.bluetooth.BluetoothAdapter.isEnabled(BluetoothAdapter.java:701)
        at com.code2care.testing.MainActivity.isBluetoothEnabled(MainActivity.java:26)
        at com.code2care.testing.MainActivity.onCreate(MainActivity.java:17)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766) 





      Topics: Pages | 2015 | Tutorials | Questions | Python