如何以编程方式打开蓝牙设置活动?

Mah*_*esh 25 settings android android-intent

我想打开按钮点击的蓝牙设置,如图所示蓝牙图像

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);
            }
        });
Run Code Online (Sandbox Code Playgroud)

Ewo*_*oks 53

也许我错过了一些东西,但这不是更简单的未来证明解决方案吗

Intent intentOpenBluetoothSettings = new Intent();
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); 
startActivity(intentOpenBluetoothSettings); 
Run Code Online (Sandbox Code Playgroud)

绝对不可能"删除"其他设置.在手机上只显示一类设置.在平板电脑上,由于一些额外的空间,设置以主 - 细节布局显示,因此平板电脑屏幕的一半以上没有空白空间.这就是Android的设计方式,只需编写一个无法更改的应用程序.

正如@zelanix所建议的那样BLUETOOTH_ADMIN,清单中的权限是必需的.

  • 这对我来说似乎是更好的解决方案.只需注释(这适用于两种解决方案),这需要"BLUETOOTH_ADMIN"权限. (3认同)
  • 这应该不需要BLUETOOTH_ADMIN并且没有库存Android - 可能是制造商的错误.我向三星报了这个. (2认同)
  • 在 Android“Q”中,我们得到 Settings.Panel 。https://developer.android.com/reference/android/provider/Settings.Panel.html#ACTION_NFC 它还不支持蓝牙(仅 NFC),但需要注意“删除其他设置”并获取只是一个蓝牙开/关开关。 (2认同)

Aj *_* 27 21

我想你应该尝试这个更简单的一个:

startActivity(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS));
Run Code Online (Sandbox Code Playgroud)


ρяσ*_*я K 16

使用

ComponentName cn = new ComponentName("com.android.settings", 
                   "com.android.settings.bluetooth.BluetoothSettings");
Run Code Online (Sandbox Code Playgroud)

代替

final ComponentName cn = new ComponentName("com.android.settings", 
                              "com.android.settings.bluetoothSettings");
Run Code Online (Sandbox Code Playgroud)

启动BluetoothSettings设置

  • @AndroidNewc:此代码用于启动BluetoothSettings设置活动,我想不可能从设置活动中删除所有其他选项 (2认同)