40

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);
            }
        });
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
Mahesh
  • 1,559
  • 6
  • 27
  • 57

6 Answers6

68

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.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
Ewoks
  • 12,285
  • 8
  • 58
  • 67
  • 3
    This seems to be the better solution to me. Just a comment (this applies to both solutions) that this requires the `BLUETOOTH_ADMIN` permission. – zelanix Mar 06 '14 at 11:24
  • 1
    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) – Ted Mar 07 '14 at 13:34
  • Ted, did you ever figure out a better way? – Kevin Lam Nov 11 '14 at 04:21
  • 2
    This should not require BLUETOOTH_ADMIN and doesn't on stock Android -- may be a manufacturer bug. I reported this one to Samsung. – Learn OpenGL ES Aug 03 '15 at 15:42
  • I think starting with sdk v23 / Android 6 you need at least and perhaps the _ADMIN one as well – DukeDidntNukeEm Sep 25 '17 at 20:48
  • Does't work on API 26 (android 8). No activity found to handle the intent ACTION_BLUETOOTH_SETTINGS. Accepted answer works on API 26. – Johnny Five Nov 27 '17 at 11:59
  • android.settings.BLUETOOTH_SETTINGS works for me on Android 8.0. – bcr Jan 08 '19 at 19:07
  • 2
    In Android "Q" we get Settings.Panel . https://developer.android.com/reference/android/provider/Settings.Panel.html#ACTION_NFC 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. – Marcus Wolschon Mar 14 '19 at 09:57
34

I think you should try this easier one :

startActivity(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS));
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Aj 27
  • 2,316
  • 21
  • 29
18

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

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
3

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

x0a
  • 255
  • 4
  • 17
2

adb shell am start -a android.settings.BLUETOOTH_SETTINGS

Vasarla
  • 57
  • 2
1

Go to Bluetooth Setting:

Java:

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

As Below->

BtnOpenSetting.setOnClickListener (v->{
startActivity(new Intent().setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS));
});

Kotlin:

startActivity(Intent().setAction(Settings.ACTION_BLUETOOTH_SETTINGS))

As Below->

BtnOpenSetting.setOnClickListener {
startActivity(Intent().setAction(Settings.ACTION_BLUETOOTH_SETTINGS))
};
Rehan Khan
  • 1,031
  • 13
  • 10