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

Android 12上Twilio Video因缺少BLUETOOTH_CONNECT权限崩溃

Android 12集成Twilio Video时BLUETOOTH_CONNECT权限缺失导致崩溃的解决方案

问题描述

在Android 12(API 31及以上)系统中,集成Twilio Video的应用启动后崩溃,抛出SecurityException,核心错误提示为Need android.permission.BLUETOOTH_CONNECT permission,完整错误日志如下:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abc.app, PID: 21262
java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10292, packageName = com.homemedics.app, attributionTag = null, token = android.os.BinderProxy@1342821, next = null }: HeadsetService
at android.os.Parcel.createExceptionOrNull(Parcel.java:2438)
at android.os.Parcel.createException(Parcel.java:2422)
at android.os.Parcel.readException(Parcel.java:2405)
at android.os.Parcel.readException(Parcel.java:2347)
at android.bluetooth.IBluetoothHeadset$Stub$Proxy.getConnectedDevicesWithAttribution(IBluetoothHeadset.java:1518)
at android.bluetooth.BluetoothHeadset.getConnectedDevices(BluetoothHeadset.java:682)
at com.twilio.audioswitch.bluetooth.BluetoothHeadsetManager.onServiceConnected(BluetoothHeadsetManager.kt:86)
at android.bluetooth.BluetoothHeadset$3.handleMessage(BluetoothHeadset.java:1640)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

原因分析

Android 12对蓝牙权限体系做了拆分,原有的BLUETOOTH和BLUETOOTH_ADMIN权限被拆分为BLUETOOTH_SCAN、BLUETOOTH_ADVERTISE和BLUETOOTH_CONNECT三个细分权限。Twilio的AudioSwitch组件(日志中com.twilio.audioswitch.bluetooth.BluetoothHeadsetManager)在访问蓝牙连接设备时,必须持有BLUETOOTH_CONNECT权限,否则会触发权限异常。

解决方案

1. 在Manifest中声明权限

在AndroidManifest.xml中添加权限声明,同时兼容低版本系统:

<!-- Android 12及以上所需权限 -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" android:minSdkVersion="31" />

<!-- 兼容Android 11及以下系统 -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />

2. 运行时动态申请权限

BLUETOOTH_CONNECT属于危险权限,必须在运行时向用户发起申请。示例代码(Kotlin):

private const val REQUEST_BLUETOOTH_CONNECT = 1001

fun requestBluetoothConnectPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val permissions = arrayOf(Manifest.permission.BLUETOOTH_CONNECT)
        ActivityCompat.requestPermissions(this, permissions, REQUEST_BLUETOOTH_CONNECT)
    }
}

// 处理权限申请结果
override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == REQUEST_BLUETOOTH_CONNECT) {
        if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // 权限授予成功,初始化Twilio Video相关功能
        } else {
            // 权限被拒绝,提示用户开启权限以使用蓝牙音频功能
        }
    }
}

3. 升级Twilio组件版本

确保Twilio Video SDK和AudioSwitch组件为最新稳定版,新版本通常会针对Android 12的权限变更做适配,减少因组件内部逻辑未处理权限导致的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 04:15:42