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");
        }
相关推荐
张小潇3 小时前
AOSP15 Input专题InputDispatcher源码分析
android
TT_Close3 小时前
【Flutter×鸿蒙】debug 包也要签名,这点和 Android 差远了
android·flutter·harmonyos
Kapaseker5 小时前
2026年,我们还该不该学编程?
android·kotlin
树獭非懒16 小时前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习19 小时前
就算没有服务器,我照样能够同步数据
后端·python·程序员
雨白21 小时前
Android 快捷方式实战指南:静态、动态与固定快捷方式详解
android
hqk21 小时前
鸿蒙项目实战:手把手带你实现 WanAndroid 布局与交互
android·前端·harmonyos
曲幽21 小时前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly21 小时前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent
LING21 小时前
RN容器启动优化实践
android·react native