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");
        }
相关推荐
呆呆的小草1 小时前
Cesium距离测量、角度测量、面积测量
开发语言·前端·javascript
uyeonashi1 小时前
【QT系统相关】QT文件
开发语言·c++·qt·学习
鹏码纵横2 小时前
已解决:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 异常的正确解决方法,亲测有效!!!
java·python·mysql
仙人掌_lz2 小时前
Qwen-3 微调实战:用 Python 和 Unsloth 打造专属 AI 模型
人工智能·python·ai·lora·llm·微调·qwen3
冬天vs不冷2 小时前
Java分层开发必知:PO、BO、DTO、VO、POJO概念详解
java·开发语言
sunny-ll2 小时前
【C++】详解vector二维数组的全部操作(超细图例解析!!!)
c语言·开发语言·c++·算法·面试
猎人everest3 小时前
快速搭建运行Django第一个应用—投票
后端·python·django
猎人everest3 小时前
Django的HelloWorld程序
开发语言·python·django
嵌入式@秋刀鱼3 小时前
《第四章-筋骨淬炼》 C++修炼生涯笔记(基础篇)数组与函数
开发语言·数据结构·c++·笔记·算法·链表·visual studio code
嵌入式@秋刀鱼3 小时前
《第五章-心法进阶》 C++修炼生涯笔记(基础篇)指针与结构体⭐⭐⭐⭐⭐
c语言·开发语言·数据结构·c++·笔记·算法·visual studio code