How to enable/disable bluetooth programmatically in android

HI all,

I want to enable/disable bluetooth through the program..I has the following code.

  • android classcastexception on activity startup
  • Using wait in AsyncTask
  • android - OkHttp interceptor - response already “consumed”
  • An error occurred during creation of android sub-project because a path is not found
  • Change Line Color of EditText - Android
  • Android - set TextView TextStyle programmatically?
  • BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
    if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    

    But this sort of code is not working in SDK 1.5..How can i do the same in SDK 1.5.?

    Related posts:

    Jacoco with Gradle 0.10.0: Remote object doesn't exist
    Why is my searchable activity's Intent.getAction() null?
    screen support multiple devices using layout-large,layout-normal and layout-xlarge folders
    How would I display one view as an overlay of another?
    Running Android apps in Linux
    malayalam font in android?
  • requestFeature() must be called before adding content
  • Taking picture from camera without preview
  • Google Cloud Messaging in Delphi XE5?
  • Android: Overlay on Android Camera Preview
  • How to determine C code is compiled for Android/NDK or iOS
  • avoid lint when gradle execute check
  • 7 Solutions collect form web for “How to enable/disable bluetooth programmatically in android”

    Android BluetoothAdapter docs say it has been available since API Level 5. API Level 5 is Android 2.0.

    You can try using a backport of the Bluetooth API (have not tried it personally): http://code.google.com/p/backport-android-bluetooth/

    this code worked for me..

    //Disable bluetooth
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
    if (mBluetoothAdapter.isEnabled()) {
        mBluetoothAdapter.disable(); 
    } 
    

    For this to work, you must have the following permissions:

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

    Here is a bit more robust way of doing this, also handling the return values of enable()\disable() methods:

    public static boolean setBluetooth(boolean enable) {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        boolean isEnabled = bluetoothAdapter.isEnabled();
        if (enable && !isEnabled) {
            return bluetoothAdapter.enable(); 
        }
        else if(!enable && isEnabled) {
            return bluetoothAdapter.disable();
        }
        // No need to change bluetooth state
        return true;
    }
    

    And add the following permissions into your manifest file:

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

    But remember these important points:

    This is an asynchronous call: it will return immediately, and clients
    should listen for ACTION_STATE_CHANGED to be notified of subsequent
    adapter state changes. If this call returns true, then the adapter
    state will immediately transition from STATE_OFF to STATE_TURNING_ON,
    and some time later transition to either STATE_OFF or STATE_ON. If
    this call returns false then there was an immediate problem that will
    prevent the adapter from being turned on – such as Airplane mode, or
    the adapter is already turned on.

    To Enable the Bluetooth you could use either of the following functions:

     public void enableBT(View view){
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()){
            Intent intentBtEnabled = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
            // The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in your onActivityResult() 
            // implementation as the requestCode parameter. 
            int REQUEST_ENABLE_BT = 1;
            startActivityForResult(intentBtEnabled, REQUEST_ENABLE_BT);
            }
      }
    

    The second function is:

    public void enableBT(View view){
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()){
            mBluetoothAdapter.enable();
        }
    }
    

    The difference is that the first function makes the app ask the user a permission to turn on the Bluetooth or to deny. The second function makes the app turn on the Bluetooth directly.

    To Disable the Bluetooth use the following function:

    public void disableBT(View view){
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter.isEnabled()){
            mBluetoothAdapter.disable();
        }
    }
    

    NOTE/ The first function needs only the following permission to be defined in the AndroidManifest.xml file:

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

    While, the second and third functions need the following permissions:

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

    The solution of prijin worked perfectly for me. It is just fair to mention that two additional permissions are needed:

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

    When these are added, enabling and disabling works flawless with the default bluetooth adapter.

    I used the below code to disable BT when my app launches and works fine. Not sure if this the correct way to implement this as google recommends not using “bluetooth.disable();” without explicit user action to turn off Bluetooth.

        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
        bluetooth.disable();
    

    I only used the below permission.

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

    Add the following permissions into your manifest file:

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

    Enable bluetooth use this

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
    if (!mBluetoothAdapter.isEnabled()) {
        mBluetoothAdapter.enable(); 
    }else{Toast.makeText(getApplicationContext(), "Bluetooth Al-Ready Enable", Toast.LENGTH_LONG).show();}
    

    Disable bluetooth use this

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
    if (mBluetoothAdapter.isEnabled()) {
        mBluetoothAdapter.disable(); 
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.