Android 14 蓝牙主从模式切换

切换蓝牙的A2DP(高级音频分布配置文件)和AVRCP(音频/视频远程控制配置文件)的源(source)和汇点(sink)模式。

这里,SystemProperties.get尝试获取bluetooth.profile.a2dp.sink.enabled属性的值。如果该属性不存在,它将返回默认值"false"。然后,它检查这个值是否等于"true"。

  1. 如果当前是A2DP Sink模式(即isA2dpSink为true):
复制代码

java复制代码

|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| | if(isA2dpSink){ |
| | // 切换成A2DP Source和AVRCP Target模式 |
| | SystemProperties.set("bluetooth.profile.a2dp.sink.enabled","false"); SystemProperties.set("bluetooth.profile.avrcp.controller.enabled","false"); SystemProperties.set("bluetooth.profile.a2dp.source.enabled","true"); SystemProperties.set("bluetooth.profile.avrcp.target.enabled","true"); |
| | } |

代码试图切换到A2DP Source和AVRCP Target模式。

  1. 如果当前不是A2DP Sink模式(即isA2dpSink为false):
复制代码

java复制代码

|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| | else{ |
| | // 这里也写着"切换成主模式",但实际上是切换回A2DP Sink和AVRCP Controller模式 |
| | SystemProperties.set("bluetooth.profile.a2dp.sink.enabled","true"); SystemProperties.set("bluetooth.profile.avrcp.controller.enabled","true"); SystemProperties.set("bluetooth.profile.a2dp.source.enabled","false"); SystemProperties.set("bluetooth.profile.avrcp.target.enabled","false"); |
| | } |

但是,这里的注释还是写着"切换成主模式",但实际上它是将蓝牙设置切换回A2DP Sink和AVRCP Controller模式。

修改建议:

  1. 修正注释以更准确地描述代码的功能。

  2. 考虑到代码可能在一个上下文中运行,其中A2DP Source和AVRCP Target被认为是"主模式",而A2DP Sink和AVRCP Controller被认为是"从模式"。如果是这样,那么注释应该是这样的:

java 复制代码
       boolean isA2dpSink = SystemProperties.get("bluetooth.profile.a2dp.sink.enabled","false").equals("true");

        if(isA2dpSink){//切换成主模式
            SystemProperties.set("bluetooth.profile.a2dp.sink.enabled","false");
            SystemProperties.set("bluetooth.profile.avrcp.controller.enabled","false");
            SystemProperties.set("bluetooth.profile.a2dp.source.enabled","true");
            SystemProperties.set("bluetooth.profile.avrcp.target.enabled","true");
        }else{//切换成主模式
            SystemProperties.set("bluetooth.profile.a2dp.sink.enabled","true");
            SystemProperties.set("bluetooth.profile.avrcp.controller.enabled","true");
            SystemProperties.set("bluetooth.profile.a2dp.source.enabled","false");
            SystemProperties.set("bluetooth.profile.avrcp.target.enabled","false");
        }
相关推荐
艾莉丝努力练剑33 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
人生游戏牛马NPC1号2 小时前
学习 Flutter (三):玩安卓项目实战 - 上
android·学习·flutter
小馬佩德罗4 小时前
Android系统的问题分析笔记 - Android上的调试方式 debuggerd
android·调试
橡晟4 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子4 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
倔强青铜35 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
清霜之辰5 小时前
安卓基于 FirebaseAuth 实现 google 登录
android·google·auth·firebase
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
企鹅与蟒蛇5 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
GitLqr5 小时前
数码洞察 | Apple VS DMA、三星新品、Android 16KB Page Size
android·ios·samsung