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");
        }
相关推荐
咬_咬11 小时前
C++仿mudo库高并发服务器项目:Buffer模块
服务器·开发语言·c++·缓冲区·buffer·muduo库
江公望11 小时前
Qt qmlplugindump浅谈
开发语言·qt·qml
曦樂~11 小时前
【Qt】文件操作/事件--mainwindow做编辑器
开发语言·qt
zzzyulin11 小时前
huggingface transformers调试问题--加载本地路径模型时pdb断点消失
python·transformer
敲代码的瓦龙11 小时前
西邮移动应用开发实验室2025年二面题解
开发语言·c++·算法
laocooon52385788611 小时前
一个适合新手的训练C题
c语言·开发语言
教练、我想打篮球11 小时前
12 pyflink 的一个基础使用, 以及环境相关
python·flink·pyflink
飞翔的佩奇12 小时前
【完整源码+数据集+部署教程】【天线&运输】直升机战机类型识别目标检测系统源码&数据集全套:改进yolo11-CSP-EDLAN
前端·python·yolo·计算机视觉·数据集·yolo11·直升机战机类型识别目标检测系统
C嘎嘎嵌入式开发12 小时前
(21)100天python从入门到拿捏《XML 数据解析》
xml·开发语言·python
晚风残12 小时前
【C++ Primer】第十七章:标准库特殊设施
开发语言·c++