峥嵘life头像
Android16 EDLA导入GMS后蓝牙打开失败未报错分析解决封面图

Android16 EDLA导入GMS后蓝牙打开失败未报错分析解决

Android16 EDLA导入GMS后蓝牙打开失败未报错分析解决

项目背景:Android16 EDLA 设备,原生 AOSP 蓝牙栈com.android.bluetooth,没有 Google 蓝牙 APEX (com.google.android.bluetooth);

未导入 GMS 包蓝牙功能正常,导入 GMS 全套包和一些patch之后,设置打开蓝牙失败,

实际蓝牙服务进程不启动,无 AndroidRuntime 崩溃堆栈,selinux 关闭现象依旧复现。

一、现象描述

  1. 未导入 GMS 版本:蓝牙开关正常,com.android.bluetooth进程正常拉起,蓝牙扫描、配对全部正常。
  2. 导入 GMS 和相关补丁之后:
    • UI 蓝牙开关可以点击,svc bluetooth enable 返回Success;
    • 但是 ps 查看不到com.android.bluetooth进程;
    • HAL 层android.hardware.bluetooth.service.default进程存在;
    • logcat 没有明显 crash 崩溃栈,没有直接蓝牙报错;
    • 关闭 SELinux(setenforce 0)问题依旧复现,排除单纯 avc 权限问题。
  3. 关键命令输出:
# 查看role角色信息
dumpsys role | grep -A30 SYSTEM_BLUETOOTH_STACK
# 输出结果:SYSTEM_BLUETOOTH_STACK角色holders为空,没有持有者

# 查看蓝牙manager状态
dumpsys bluetooth_manager | grep mSystemBluetoothStack
# 无输出,代表没有匹配到合法蓝牙栈包

# 查看蓝牙包信息
pm dump com.android.bluetooth | grep -E "flags|signature|path"
# 包存在,signature、privileged签名属性正常

重点:svc bluetooth enable返回 Success只代表命令入队成功,不等于蓝牙栈真正初始化成功,属于异步流程,上层返回成功,底层角色校验失败直接放弃启动蓝牙进程,不会打印崩溃日志,属于 Android16 新的 RoleService 校验机制带来的隐形坑。

二、原理说明 Android16 SYSTEM_BLUETOOTH_STACK Role 机制

Android16 引入android.app.role.SYSTEM_BLUETOOTH_STACK角色校验逻辑:

  1. SystemServer 启动 RoleService,优先读取config_systemBluetoothStack资源配置,获取期望蓝牙栈包名。
  2. 在 EDLA GMS 模板中,config_systemBluetoothStack默认写死指向com.google.android.bluetooth。
  3. 当前项目并没有导入 Google 蓝牙 APEX 包 com.google.android.bluetooth,该包不存在。
  4. RoleService 尝试分配SYSTEM_BLUETOOTH_STACK角色,找不到对应包,角色 holders 为空。
  5. BluetoothManagerService 检测无合法SYSTEM_BLUETOOTH_STACK持有者,直接放弃启动蓝牙 APK 进程,不会抛出 crash,不会打印明显错误日志。
  6. SELinux 关闭无法规避该问题,属于 Framework 业务逻辑校验,不是权限拒绝。

EDLA 认证说明:EDLA 允许不使用 Google 预编译蓝牙 com.google.android.bluetooth,直接使用 AOSP 原生com.android.bluetooth,但是必须同步修正资源配置,不能保留 GMS 模板遗留的config_systemBluetoothStack指向 google 包。

关键配置位置

源码路径:frameworks/base/core/res/res/values/config.xml

<!-- GMS模板遗留错误配置 -->

<?xml version="1.0" encoding="utf-8"?>
<resources>
...
    <string name="config_systemBluetoothStack" translatable="false">
        com.google.android.bluetooth
    </string>
</resources>

该配置告诉系统优先使用 google 蓝牙栈,但是本项目没有该包,校验失败。

补充:meta‑data android:name="android.app.role.SYSTEM_BLUETOOTH_STACK" android:value="true" 不需要加到com.android.bluetooth的 AndroidManifest,原生 AOSP 蓝牙模块已经内置该 meta‑data,手动重复添加反而会引入异常。

系统源码的framework默认是不定义这个的,但是最好也是全源码排查 config_systemBluetoothStack 关键字。

这里出现问题就是因为AML方案中定义了这个config,大概地址:

device/amlogic/XXX/frameworks/base/core/res/res/values/config.xml

三、完整排查步骤(adb 调试定位)

步骤 1:确认 hal 层是否正常

lshal | grep bluetooth
# 期望输出:android.hardware.bluetooth@1.0 HAL服务已经注册

HAL 存在说明 vendor 蓝牙固件、HCI、vendor hal 本身没有问题,问题出在上层 Framework/PackageManager/RoleService。

步骤 2:查看 SYSTEM_BLUETOOTH_STACK 角色持有者

dumpsys role | grep -A20 SYSTEM_BLUETOOTH_STACK
  • 如果holders=为空,就是本文章遇到的问题;
  • 如果 holders 存在包名,核对包名是否是实际运行的蓝牙包。

步骤 3:确认当前系统读取的 config_systemBluetoothStack 值

adb shell dumpsys platform_compat | grep config_systemBluetoothStack
# 或者查看framework资源
adb shell settings get secure bluetooth_stack_override

步骤 4:手动查看蓝牙包是否具备角色要求条件

pm dump com.android.bluetooth
# 确认:signature|privileged;manifest中包含SYSTEM_BLUETOOTH_STACK meta‑data

步骤 5:复现开启蓝牙,抓取完整 log 过滤关键字

logcat -b all | grep -E "RoleService|BluetoothManagerService|SYSTEM_BLUETOOTH_STACK"

重点观察日志:No valid stack for SYSTEM_BLUETOOTH_STACK,该日志容易被淹没,不会当做 error 级别打印。

正常或者异常打开蓝牙,-i 过滤bluetooth都是很多日志的,但是这种情况下是没啥日志的。

四、解决方案

方案一:修改 framework config.xml(推荐产品编译版本)

修改frameworks/base/core/res/res/values/config.xml,将配置指向 AOSP 原生蓝牙包:

<!-- 修改前(GMS模板遗留) -->
<!-- <string name="config_systemBluetoothStack" translatable="false">com.google.android.bluetooth</string> -->

<!-- 修改后,使用AOSP原生蓝牙 -->
<string name="config_systemBluetoothStack" translatable="false">com.android.bluetooth</string>
  1. 重新编译 framework-res 模块,整包烧录;
  2. 开机后执行验证命令:
dumpsys role | grep -A20 SYSTEM_BLUETOOTH_STACK
# holders应当出现 com.android.bluetooth
ps -A | grep com.android.bluetooth
# 打开蓝牙之后进程正常拉起

⚠️注意:部分项目会将 config_systemBluetoothStack 覆盖到vendor或者overlay目录,要检查 overlay 是否有重写该字段,如果 overlay 有高优先级覆盖,修改 framework 主文件不生效。

查找 overlay 覆盖命令:

find ./device ./vendor -name "*.xml" | xargs grep "config_systemBluetoothStack"

方案二:overlay 方式修改(推荐,不修改 framework 源码)

在设备 device overlay 中新增配置,不改动 AOSP 原始 framework 代码,EDLA 项目推荐 overlay 方式,方便维护: device/xxx/overlay/frameworks/base/core/res/res/values/config.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="config_systemBluetoothStack" translatable="false">com.android.bluetooth</string>
</resources>

将该 overlay 加入到设备device.mk的PRODUCT_PACKAGE_OVERLAYS。

前面方案都是可以解决,都感觉挺好的;

但是供应商建议EDLA项目还是使用 com.google.android.bluetooth 应用。

对比查看了Google手机:

Android13上使用的是com.android.bluetooth 应用;

Android16 使用的是com.google.android.bluetooth 应用.

估计后续mainline模块大部分会用上。

方案三:修改蓝牙应用为Google应用

GMS的mainline包默认有Google 的一些列应用,为啥默认不编译 Google的蓝牙应用。

wifi 和 Connectivity 默认默认是编译进去的。

为啥我也说不清楚。。。具体可以看:vendor/partner_modules/build/mainline_modules.mk

这里只讲修改就是在device某个mk文件加入:MAINLINE_INCLUDE_ALL_OPTIONAL_MODULES := true

如下修改:

+++ b/amlogic/akee/device.mk
@@ -363,6 +363,7 @@ ifeq ($(TARGET_BUILD_GMS), true)
 $(call inherit-product, vendor/partner_gms/products/gms_64bit_only.mk)
 #mainline
 $(call inherit-product, vendor/partner_modules/build/mainline_modules.mk)
+MAINLINE_INCLUDE_ALL_OPTIONAL_MODULES := true
 endif
 

后续 vendor/partner_modules 里面很多模块就会编译进去了。

这时候蓝牙就可以正常打开了。

但是有bug,蓝牙扫描到列表的时候会自动关开一次蓝牙,一直循环关开。。。

底层分析是蓝牙固件问题,需要找供应商更新替换vendor下的蓝牙固件;好像不是驱动固件。

如果没有供应商的支持,还是使用前面两个方案进行解决吧。

五、EDLA 关键注意点

  1. EDLA 认证不强制要求使用com.google.android.bluetooth,可以使用 AOSP 原生com.android.bluetooth,但是GMS 模板的 config 资源必须同步修正,很多移植人员直接照搬 GMS overlay,没有修改该字段,踩坑。
  2. WiFi 模块和蓝牙不一样:WiFi mainline 可以不使用 google 预编译 APEX,同样需要核对相关 config 资源。
  3. 不要手动在com.android.bluetooth AndroidManifest 重复添加Role,原生 AOSP 蓝牙已经自带,重复添加会引发角色分配异常。
  4. 该问题没有崩溃日志,很多人会优先怀疑 selinux、蓝牙固件、vendor hal,排查很长时间,优先检查SYSTEM_BLUETOOTH_STACK角色是 Android16 EDLA 移植 GMS 必查点。

六、验证检查清单(修复完成逐项确认)

表格

检查项预期结果
dumpsys role SYSTEM_BLUETOOTH_STACK holdersholders=com.android.bluetooth
dumpsys bluetooth_managermSystemBluetoothStack 包名正确
svc bluetooth enable 之后 pscom.android.bluetooth 进程正常存在
设置界面蓝牙开关开关正常,可扫描、配对设备
关闭打开蓝牙多次复现每次都正常拉起,不会偶发失效

七、其他

1、小结

Android16 EDLA 导入 GMS 之后蓝牙开关显示打开但是进程不启动,无 crash 日志,

优先排查SYSTEM_BLUETOOTH_STACK角色和config_systemBluetoothStack配置。

GMS 模板默认配置指向com.google.android.bluetooth,当项目没有引入 google 蓝牙 APEX,

RoleService 校验失败直接放弃启动蓝牙栈,关闭 selinux 无法解决。

使用 overlay 修改 config 指向 AOSP 原生com.android.bluetooth即可修复,该坑在 EDLA 设备移植 GMS 高频出现。

2、其他相似干扰排查点

  1. Apex 挂载时序:如果蓝牙以 apex 形式打包,开机早期 RoleService 执行时 apex 还未挂载,角色分配失败;需要调整 apex early‑mount 配置。
  2. 包签名:com.android.bluetooth 必须是 privileged/signature 签名,不能普通 app 签名。
  3. GMS 其他资源 overlay 残留:导入 GMS 后检查所有 overlay,不要遗留 google 蓝牙栈相关配置。

下面这个是以前写的记录。

3、Android13 安装谷歌GMS导致打开蓝牙失败解决方法

Android13 上安装谷歌GMS相关服务后,无法打开蓝牙,打开蓝牙马上异常导致自动关闭蓝牙。

这个问题国内估计大部分开发者不会遇到,但是如果是面向国外用户需要安装谷歌相关服务可能会遇到。

这种是有明显失败日志的。

原文链接:https://blog.csdn.net/wenzhi20102321/article/details/134220850

转载自 CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/wenzhi20102321/article/details/164628824

文章来源转载

评论

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--