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

发送ACL_DISCONNECTED广播遇权限拒绝问题原因咨询

问题分析:发送蓝牙ACL_DISCONNECTED广播触发SecurityException

错误信息

Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.bluetooth.device.action.ACL_DISCONNECTED

已配置的清单权限

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" tools:ignore="ProtectedPermissions" />

<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />

广播接收器注册代码

private fun setBluetoothReceiver() {
    val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
    filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED)
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
    registerReceiver(bluetoothReceiver, filter)
}

发送广播的代码

val local = Intent()
local.setAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
local.putExtra("number", device.name)
if (context != null) {
    context.sendBroadcast(local)
}

---

### 问题原因
1. **系统广播权限限制**:`BluetoothDevice.ACTION_ACL_DISCONNECTED`是系统级广播Action,仅允许系统进程或拥有系统签名的应用发送,普通应用无权限操作。
2. **无效权限申请**:`BLUETOOTH_PRIVILEGED`属于受保护的系统权限,普通应用即使在清单中声明并忽略编译警告,运行时系统也不会实际授予该权限,无法以此绕过限制。
3. **误用系统Action**:你的场景是收到系统蓝牙断开通知后向应用内部传递自定义数据,完全不需要复用系统广播Action,属于用法错误。

### 解决办法
改用**自定义广播Action**实现应用内部信息传递:
1. 定义自定义Action常量:
```kotlin
// 替换为你的应用包名前缀
const val ACTION_CUSTOM_BLUETOOTH_DISCONNECTED = "com.your.app.action.CUSTOM_BLUETOOTH_DISCONNECTED"
  1. 修改发送广播的代码:
val localIntent = Intent(ACTION_CUSTOM_BLUETOOTH_DISCONNECTED)
localIntent.putExtra("number", device.name)
context?.sendBroadcast(localIntent)
  1. 若仅为应用内通信,也可考虑用LiveData、Flow等组件替代广播,实现更高效的内部状态传递。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 04:25:17