How to check if bluetooth is enabled programmatically?

AndroidBluetooth

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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" />

Solution 2 - Android

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);

Solution 3 - Android

public boolean isBluetoothEnabled()
	{
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		return mBluetoothAdapter.isEnabled();
		
	}

with the permission in manifest file:

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

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

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" />

Solution 7 - Android

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();
            }
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZerhinneView Question on Stackoverflow
Solution 1 - AndroidPete HoustonView Answer on Stackoverflow
Solution 2 - AndroidHari Babu MandalaView Answer on Stackoverflow
Solution 3 - AndroidArun kumarView Answer on Stackoverflow
Solution 4 - AndroidJayesh TembhekarView Answer on Stackoverflow
Solution 5 - AndroidsrinivasView Answer on Stackoverflow
Solution 6 - AndroidUshal NaidooView Answer on Stackoverflow
Solution 7 - AndroidSuresh B BView Answer on Stackoverflow