87

I would like to check if bluetooth is enabled on any Android device periodically. Is there any intents that I could catch using BroadcastReceiver to do so, or is there other ways to do it?

1
  • Just check out this link for Bluetooth and follow all the step. I hope this help.
    – Uttam
    Commented Oct 6, 2011 at 9:31

8 Answers 8

202

There you go:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
} else if (!mBluetoothAdapter.isEnabled()) {
    // Bluetooth is not enabled :)
} else {
    // Bluetooth is enabled 
}

With uses-permission

 <uses-permission android:name="android.permission.BLUETOOTH" android:required="false" />
3
  • 8
    If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
    – Murphy
    Commented Jan 16, 2013 at 1:21
  • Is internet required to check bluetooth? Commented Aug 26, 2015 at 19:30
  • 7
    nope, you don't need internet to check bluetooth status. well, aside from getting the code from this SO post ;)
    – dasch88
    Commented Feb 3, 2016 at 14:19
8

Here I have other alternative as an answer for this question.

First add following lines in your Manifest file.

<uses-feature android:name="android.hardware.BLUETOOTH" android:required="false"/>

Now, where you want to check Bluetooth supportability, use following code.

boolean isBluetoothSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
1
  • how it's different with "BluetoothAdapter.getDefaultAdapter()" ?
    – CoDe
    Commented May 1, 2014 at 5:16
7
public boolean isBluetoothEnabled()
    {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        return mBluetoothAdapter.isEnabled();

    }

with the permission in manifest file:

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

To check Bluetooth state, ON or OFF, programmatically:

    BluetoothAdapter btAdapter = ((Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
           ?((BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter()
               :(BluetoothAdapter.getDefaultAdapter()));

       if(btAdapter==null){
        return;
       }
       if(btAdapter.getState()==BluetoothAdapter.STATE_ON){
            //Bluetooth is ON
       }

You may also listen to Intent action:

BluetoothAdapter.ACTION_STATE_CHANGED

2

use can use

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

for check bt connected

mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED

for check bt disconnected

mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_DISCONNECTED
0
1

This is how I did it with the help of @xjaphx's answer, slightly simplified version:

 private boolean getBlueToothOn(){
    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 
      return btAdapter != null && btAdapter.isEnabled();
    }

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

First allow the Permissions from Manifest.

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

public class MainActivity extends AppCompatActivity {
    
    private BluetoothAdapter mAdapter;
    private BluetoothDevice mDevice;
    private static final int ACCESS_REQUEST_CODE=100;
    private static final int REQUEST_ENABLE_CODE=1;
    private AudioRecord mRecord;

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

        if (mAdapter!=null && mAdapter.isEnabled()){

        }
        else {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_ENABLE_CODE){
            if (resultCode==RESULT_OK){
                println("Bluettoth Enabled");
            }
            else if (resultCode==RESULT_CANCELED){
                println("Bluetooth Not permitted");
                finish();
            }
        }
    }
}
2
  • To check Bluetooth whether is turned on or off if turned off will reqest a runtime permission to enable bluetooth and if they deny the access it will close the activity.
    – Suresh B B
    Commented Jan 11, 2021 at 9:25
  • 1
    If this post is an update of this answer, you should use edit link instead of adding another post or adding in comments
    – Suraj Rao
    Commented Jan 11, 2021 at 9:26
0

Since BluetoothAdapter.getDefaultAdapter() is deprecated, you can instead retrieve the bluetooth adapter from BluetoothManager like so:

val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter = bluetoothManager.adapter
bluetoothAdapter?.isEnabled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.