How to check if bluetooth is enabled programmatically?

后端 未结 6 1108
小蘑菇
小蘑菇 2020-12-13 23:46

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

相关标签:
6条回答
  • 2020-12-13 23:59
    public boolean isBluetoothEnabled()
        {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            return mBluetoothAdapter.isEnabled();
    
        }
    

    with the permission in manifest file:

    <uses-permission android:name="android.permission.BLUETOOTH" />
    
    0 讨论(0)
  • 2020-12-14 00:02

    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" />
    
    0 讨论(0)
  • 2020-12-14 00:06

    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);
    
    0 讨论(0)
  • 2020-12-14 00:06

    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 讨论(0)
  • 2020-12-14 00:22

    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

    0 讨论(0)
  • 2020-12-14 00:23

    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" />
    
    0 讨论(0)
提交回复
热议问题