How do I open the Bluetooth Settings Activity programmatically?

40,702

Solution 1

use

ComponentName cn = new ComponentName("com.android.settings", 
                   "com.android.settings.bluetooth.BluetoothSettings");

instead of

final ComponentName cn = new ComponentName("com.android.settings", 
                              "com.android.settings.bluetoothSettings");

to launch BluetoothSettings settings

Solution 2

Maybe I missed something but isn't this simpler future proof solution?

Intent intentOpenBluetoothSettings = new Intent();
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); 
startActivity(intentOpenBluetoothSettings); 

It is definitely not possible to "remove" the other settings. On phones just one category of settings is shown. On tablets, because of some extra space, settings are shown in master-detail layout so there is no empty space on more the half of the tablet screen. This is how Android is designed and just by writing one app that can not be changed.

As suggested by @zelanix the BLUETOOTH_ADMIN permission in manifest is required.

Solution 3

I think you should try this easier one :

startActivity(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS));

Solution 4

If you want to open up the scan dialog (without leaving your app).

    Intent bluetoothPicker = new Intent("android.bluetooth.devicepicker.action.LAUNCH");
    startActivity(bluetoothPicker);

BluetoothScanDialog

Solution 5

adb shell am start -a android.settings.BLUETOOTH_SETTINGS

Share:
40,702
Mahesh
Author by

Mahesh

Updated on July 28, 2022

Comments

  • Mahesh
    Mahesh almost 2 years

    I want to open bluetooth settings on button click like this see imagebluetooth image

    HomeActivity.java

    button.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    final Intent intent = new Intent(Intent.ACTION_MAIN, null);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.bluetoothSettings");
                    intent.setComponent(cn);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity( intent);
                }
            });
    
  • Mahesh
    Mahesh over 11 years
    but is also display the other settings i want show only bluetooth settings all other must be hide
  • Mahesh
    Mahesh over 11 years
    and i am use the android 4.0.3 tablet
  • ρяσѕρєя K
    ρяσѕρєя K over 11 years
    @AndroidNewc: this code for launching BluetoothSettings settings Activity and it's not possible i think to remove all other Options from Setting activity
  • zelanix
    zelanix about 10 years
    This seems to be the better solution to me. Just a comment (this applies to both solutions) that this requires the BLUETOOTH_ADMIN permission.
  • Ted
    Ted about 10 years
    Is there any way to get this settings window to open 'non-modally' from your app? The only way to get back to the app was via the 'back' button next to the home button, rather than the two windows appearing separately in the 'task list' view of apps as I would have expected. (Perhaps this 'modal' method is the preferred, standard, technique though? Apologies, I'm a v new newb when it comes to Android as a user & developer)
  • Kevin Lam
    Kevin Lam over 9 years
    Ted, did you ever figure out a better way?
  • Totoro
    Totoro over 9 years
    The question seems to be about doing it programmatically, not through command line.
  • Learn OpenGL ES
    Learn OpenGL ES almost 9 years
    This should not require BLUETOOTH_ADMIN and doesn't on stock Android -- may be a manufacturer bug. I reported this one to Samsung.
  • Ewoks
    Ewoks over 8 years
    IMHO this can crash with any new android version
  • lsrom
    lsrom almost 7 years
    This is indeed correct answer. Tested on Android 5.0.
  • DukeDidntNukeEm
    DukeDidntNukeEm over 6 years
    I think starting with sdk v23 / Android 6 you need at least <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> and perhaps the _ADMIN one as well
  • Johnny Five
    Johnny Five over 6 years
    Works on API 26 (Android 8). "setAction" solution doesn't work on API 26.
  • Johnny Five
    Johnny Five over 6 years
    Does't work on API 26 (android 8). No activity found to handle the intent ACTION_BLUETOOTH_SETTINGS. Accepted answer works on API 26.
  • bcr
    bcr over 5 years
    android.settings.BLUETOOTH_SETTINGS works for me on Android 8.0.
  • Marcus Wolschon
    Marcus Wolschon about 5 years
    In Android "Q" we get Settings.Panel . developer.android.com/reference/android/provider/… It does not support bluetooth yet (just NFC) but this is something to look out for to "remove the other settings" and get just a Bluetooth ON/OFF -switch.
  • drindt
    drindt over 3 years
    startActivity(Intent(android.provider.Settings.ACTION_BLUETO‌​OTH_SETTINGS))