You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android API 33中如何正确实现蓝牙禁用?

Android 13(API 33+)蓝牙禁用替代方案

原BluetoothAdapter.disable()方法在API 33(对应Android 13,Build.VERSION_CODES.TIRAMISU)已被弃用:

  • 针对API 33及以上版本的应用,调用该方法会直接返回false,无法完成蓝牙禁用操作;
  • 仅针对API 31(Android 12,Build.VERSION_CODES.S)及以下版本的应用,仍可正常使用该方法直接禁用蓝牙。

正确实现蓝牙禁用的方式

从Android 13开始,系统不再允许应用直接控制蓝牙开关,只能引导用户前往系统蓝牙设置页面手动关闭蓝牙。结合版本兼容的完整实现如下:

import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Build
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity

fun AppCompatActivity.disableBluetooth(bluetoothAdapter: BluetoothAdapter?) {
    when {
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> {
            // Android 13及以上,跳转至系统蓝牙设置页面
            val bluetoothSettingsIntent = Intent(Settings.ACTION_BLUETOOTH_SETTINGS)
            startActivity(bluetoothSettingsIntent)
        }
        bluetoothAdapter?.isEnabled == true -> {
            // Android 12及以下,调用旧API直接禁用
            bluetoothAdapter.disable()
        }
    }
}

补充说明

  • 蓝牙启用仍可沿用你之前的实现,通过BluetoothAdapter.ACTION_REQUEST_ENABLE唤起系统弹窗让用户确认启用;
  • 系统没有提供类似启用的ACTION_REQUEST_DISABLE弹窗接口,因此Android 13+只能引导用户到设置页面操作。

内容的提问来源于stack exchange,提问作者yoman796

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 10:40:31