Skip to content Skip to sidebar Skip to footer

How To Check If Bluetooth Is Enabled Programmatically?

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 t

Solution 1:

There you go:

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

With uses-permission

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

Solution 2:

publicbooleanisBluetoothEnabled()
    {
        BluetoothAdaptermBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
        return mBluetoothAdapter.isEnabled();

    }

with the permission in manifest file:

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

Solution 3:

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

First add following lines in your Manifest file.

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

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

booleanisBluetoothSupported= getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

Solution 4:

To check Bluetooth state, ON or OFF, programmatically:

BluetoothAdapterbtAdapter= ((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:

use can use

BluetoothAdaptermBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();

for check bt connected

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

for check bt disconnected

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

Post a Comment for "How To Check If Bluetooth Is Enabled Programmatically?"