How to check if bluetooth is turned on programmatically?

I want to check if Bluetooth is enabled on any Android device. Are there any intentions that I could catch using BroadcastReceiver, or are there other ways to do this?

+63
android bluetooth
Oct 06 2018-11-11T00:
source share
6 answers

There you go:

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

With uses-permission

  <uses-permission android:name="android.permission.BLUETOOTH" android:required="false" /> 
+153
Oct 06 2018-11-11T00:
source share

Here I have another alternative as an answer to this question.

First add the following lines to the manifest file.

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

Now that you want to check Bluetooth support, use the following code.

 boolean isBluetoothSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); 
+7
Feb 11 '14 at 6:58
source share
 public boolean isBluetoothEnabled() { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return mBluetoothAdapter.isEnabled(); } 

with permission in the manifest file:

 <uses-permission android:name="android.permission.BLUETOOTH" /> 
+6
Mar 19 '15 at 8:40
source share

To check the status of Bluetooth 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 can also listen to the Intent action:

BluetoothAdapter.ACTION_STATE_CHANGED

+4
Apr 09 '14 at 13:05
source share

use can use

 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

for checking bt connected

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

to check bt is disabled

 mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_DISCONNECTED 
+2
Mar 14 '14 at 8:34
source share

Here's how I did it with @xjaphx's answer, a slightly simplified version:

  private boolean getBlueToothOn(){ BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); return btAdapter != null && btAdapter.isEnabled(); } <uses-permission android:name="android.permission.BLUETOOTH" /> 
+1
Apr 30 '14 at 3:56
source share



All Articles