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

Android Kotlin应用移除蓝牙绑定失败及参数报错求助

问题解决:移除蓝牙配对设备时参数错误

错误原因

报错Wrong number of arguments; expected 0, got 1是因为BluetoothDevice的removeBond方法是无参方法,但你在调用Method.invoke()时额外传递了null as Array<Any?>?作为参数,导致参数数量不匹配。

修复后的代码

pairedDevices = mBluetoothAdapter?.bondedDevices
pairedDevices?.forEach { device ->
    if (device.address == d.macAddress) {
        try {
            val removeBondMethod = device.javaClass.getMethod("removeBond")
            removeBondMethod.invoke(device) // 仅传递目标对象,无额外参数
        } catch (e: Exception) {
            Log.e("RemoveBondError", "移除配对失败: ${e.message}", e)
        }
    }
}

额外优化建议

  • 避免使用!!非空断言,改用安全调用?.和空集合处理,防止空指针异常
  • 使用forEach替代传统for循环,代码更简洁
  • 优化Log标签和信息,便于定位问题
  • 注意:removeBond是隐藏API,部分Android版本可能限制调用,需确保应用有相应权限(如BLUETOOTH_ADMIN),高版本Android(12+)还需要BLUETOOTH_CONNECT权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 21:44:50