Mahesh
Mahesh

Reputation: 1589

How do I open the Bluetooth Settings Activity programmatically?

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);
            }
        });

Upvotes: 43

Views: 49948

Answers (6)

Rehan Khan
Rehan Khan

Reputation: 1281

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))
};

Upvotes: 3

x0a
x0a

Reputation: 265

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

Upvotes: 4

Aj 27
Aj 27

Reputation: 2427

I think you should try this easier one :

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

Upvotes: 37

Ewoks
Ewoks

Reputation: 12435

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.

Upvotes: 69

Vasarla
Vasarla

Reputation: 57

adb shell am start -a android.settings.BLUETOOTH_SETTINGS

Upvotes: 2

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

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

Upvotes: 18

Related Questions