【android bluetooth 框架分析 04】【bt-framework 层详解 6】【Properties介绍】

DevicePropertiesAdapterPropertiesStorageModule、以及 bt_config.conf 是 AOSP Bluetooth 栈中 设备属性管理与持久化系统 的核心组成部分,它们之间关系紧密,但职责各有不同。

下面我将依次讲解它们的区别与联系.

注意:

  • 在代码里面 还有 BluetoothProperties : 他是管理 蓝牙相关的系统属性的, 和本文讨论的 DevicePropertiesAdapterProperties 不是同一个话题。
  • 有兴趣可以参看: 【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. 核心组件职责概览

模块 职责 管理对象 是否与存储直接交互
AdapterProperties 管理本地蓝牙适配器的属性(如:名称、可发现性、IO能力) 本地适配器 ✅ 是,会调用 StorageModule
DeviceProperties 管理单个远程设备的属性(如名称、RSSI、UUID、版本信息等) 每个远程设备一套 ✅ 是,会调用 StorageModule
StorageModule 抽象了属性的持久化与加载逻辑,负责读写 bt_config.conf 存储本地和远程设备属性 ✅ 是,底层对 bt_config.conf 读写
bt_config.conf 配置文件,持久化存储蓝牙设备属性(ini 格式) 适配器/已配对的远程设备 ✅ 是,由 StorageModule 管理

2. 各模块职责与交互细节

1. AdapterProperties

  • 对应的是本地蓝牙适配器的属性,如:

    • 本地蓝牙名 (BT_PROPERTY_BDNAME)

    • 适配器地址 (BT_PROPERTY_BDADDR)

    • 可发现性 (BT_PROPERTY_ADAPTER_SCAN_MODE)

    • LE 特性等

  • 存储方式:

    • 初始化时从 StorageModule 读取对应项

    • 修改时(如用户改蓝牙名)通过 StorageModule 写入 bt_config.conf[Adapter]


2. DeviceProperties

  • 对应每一个远程设备(配对或曾连接)的属性,如:

    • 名称、RSSI、UUID、版本、是否支持某功能等

    • 这些属性通过扫描、配对、连接等过程获得

  • 管理方式:

    • 每个远程设备维护一个 DeviceProperties 实例(以地址为 key)

    • 当发现设备、连接、配对或服务发现后更新属性

  • 写入存储:

    • 只有绑定/配对成功的设备才会写入

      • 如果一个设备只是在扫描时被发现,会创建一个 临时的 DeviceProperties,保存在内存中。 掉电或者开关蓝牙时将丢失。
    • 这些属性会保存到 bt_config.conf[RemoteDevice] 节,例如:

[RemoteDevice00:11:22:33:44:55]
Name=MyHeadphones
DevType=1
Service=180A 112D ...

3. StorageModule

  • 提供统一接口负责蓝牙配置的持久化与读取。

  • 核心功能:

    • 加载/保存本地适配器属性

    • 加载/保存配对设备属性

    • 支持迁移、同步、回写等操作

  • 底层调用 config.cc 进行 ini 格式文件操作。

我之前写过一篇 关于 StorageModule 模块的文章,需要 可以查阅:

  • 【android bluetooth 框架分析 02】【Module详解 6】【StorageModule 模块介绍】

4. bt_config.conf 文件

  • 位置:/data/misc/bluedroid/bt_config.conf

  • 权限:系统组件访问,普通 APP 不可读

  • 结构:

[Adapter]
Address=00:11:22:33:AA:BB
Name=CarBluetooth
ScanMode=1
DiscoverableTimeout=120

[RemoteDevice11:22:33:44:55:66]
Name=Phone
DevType=1
Service=110A 110B

3. 关键问题:扫描到的未配对设备会写入 bt_config.conf 吗?

扫描到的未配对设备会写入 bt_config.conf 吗?

不会!

  • 当仅仅是扫描(inquiry/discovery)到一个设备时,系统可能会临时创建该设备的 DeviceProperties 实例,但不会写入 bt_config.conf

  • 只有以下情形会触发写入:

    • 配对成功

    • 某些属性需要持久化(如用户手动设置了设备名等)

    • 有实际连接历史 + 存储条件满足(具体由 DeviceManager::Add 判断)

1. 临时设备属性的生命周期:

  • 临时创建的 DeviceProperties 保存在内存中

  • 断电或重启后不保留

  • 若用户未配对该设备,这些属性不会持久化


4. 模块关系图示意

+-----------------+
| AdapterProperties (本地适配器属性)
+-----------------+
         |
         v
+-----------------+
| StorageModule   | <-------> bt_config.conf (持久化存储)
+-----------------+
         ^
         |
+------------------+
| DeviceProperties (远程设备属性)
| -- 每个设备一套 --
+------------------+

5. 我们 源码里面都定义了那些属性呢?

1. java 侧的属性表

  • android/app/src/com/android/bluetooth/btservice/AbstractionLayer.java
    static final int BT_PROPERTY_BDNAME = 0x01;
    static final int BT_PROPERTY_BDADDR = 0x02;
    static final int BT_PROPERTY_UUIDS = 0x03;
    static final int BT_PROPERTY_CLASS_OF_DEVICE = 0x04;
    static final int BT_PROPERTY_TYPE_OF_DEVICE = 0x05;
    static final int BT_PROPERTY_SERVICE_RECORD = 0x06;
    static final int BT_PROPERTY_ADAPTER_SCAN_MODE = 0x07;
    static final int BT_PROPERTY_ADAPTER_BONDED_DEVICES = 0x08;
    static final int BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT = 0x09;

    static final int BT_PROPERTY_REMOTE_FRIENDLY_NAME = 0x0A;
    static final int BT_PROPERTY_REMOTE_RSSI = 0x0B;

    static final int BT_PROPERTY_REMOTE_VERSION_INFO = 0x0C;
    static final int BT_PROPERTY_LOCAL_LE_FEATURES = 0x0D;

    static final int BT_PROPERTY_LOCAL_IO_CAPS = 0x0e;
    static final int BT_PROPERTY_LOCAL_IO_CAPS_BLE = 0x0f;

    static final int BT_PROPERTY_DYNAMIC_AUDIO_BUFFER = 0x10;
    static final int BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER = 0x11;

2. native 属性表

bt_property_type_t

  • system/include/hardware/bluetooth.h
/* Bluetooth Adapter and Remote Device property types */
typedef enum {
  /* Properties common to both adapter and remote device */
  /**
   * Description - Bluetooth Device Name
   * Access mode - Adapter name can be GET/SET. Remote device can be GET
   * Data type   - bt_bdname_t
   */
  BT_PROPERTY_BDNAME = 0x1,
  /**
   * Description - Bluetooth Device Address
   * Access mode - Only GET.
   * Data type   - RawAddress
   */
  BT_PROPERTY_BDADDR,
  /**
   * Description - Bluetooth Service 128-bit UUIDs
   * Access mode - Only GET.
   * Data type   - Array of bluetooth::Uuid (Array size inferred from property
   *               length).
   */
  BT_PROPERTY_UUIDS,
  /**
   * Description - Bluetooth Class of Device as found in Assigned Numbers
   * Access mode - Only GET.
   * Data type   - uint32_t.
   */
  BT_PROPERTY_CLASS_OF_DEVICE,
  /**
   * Description - Device Type - BREDR, BLE or DUAL Mode
   * Access mode - Only GET.
   * Data type   - bt_device_type_t
   */
  BT_PROPERTY_TYPE_OF_DEVICE,
  /**
   * Description - Bluetooth Service Record
   * Access mode - Only GET.
   * Data type   - bt_service_record_t
   */
  BT_PROPERTY_SERVICE_RECORD,

  /* Properties unique to adapter */
  /**
   * Description - Bluetooth Adapter scan mode
   * Access mode - GET and SET
   * Data type   - bt_scan_mode_t.
   */
  BT_PROPERTY_ADAPTER_SCAN_MODE,
  /**
   * Description - List of bonded devices
   * Access mode - Only GET.
   * Data type   - Array of RawAddress of the bonded remote devices
   *               (Array size inferred from property length).
   */
  BT_PROPERTY_ADAPTER_BONDED_DEVICES,
  /**
   * Description - Bluetooth Adapter Discoverable timeout (in seconds)
   * Access mode - GET and SET
   * Data type   - uint32_t
   */
  BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT,

  /* Properties unique to remote device */
  /**
   * Description - User defined friendly name of the remote device
   * Access mode - GET and SET
   * Data type   - bt_bdname_t.
   */
  BT_PROPERTY_REMOTE_FRIENDLY_NAME,
  /**
   * Description - RSSI value of the inquired remote device
   * Access mode - Only GET.
   * Data type   - int8_t.
   */
  BT_PROPERTY_REMOTE_RSSI,
  /**
   * Description - Remote version info
   * Access mode - SET/GET.
   * Data type   - bt_remote_version_t.
   */

  BT_PROPERTY_REMOTE_VERSION_INFO,

  /**
   * Description - Local LE features
   * Access mode - GET.
   * Data type   - bt_local_le_features_t.
   */
  BT_PROPERTY_LOCAL_LE_FEATURES,

  /**
   * Description - Local Input/Output Capabilities for classic Bluetooth
   * Access mode - GET and SET
   * Data Type - bt_io_cap_t.
   */
  BT_PROPERTY_LOCAL_IO_CAPS,

  /**
   * Description - Local Input/Output Capabilities for BLE
   * Access mode - GET and SET
   * Data Type - bt_io_cap_t.
   */
  BT_PROPERTY_LOCAL_IO_CAPS_BLE,

  BT_PROPERTY_DYNAMIC_AUDIO_BUFFER,

  /**
   * Description - True if Remote is a Member of a Coordinated Set.
   * Access mode - GET.
   * Data Type - bool.
   */
  BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER,

  BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP = 0xFF,
} bt_property_type_t;

3. 属性使用场景介绍

1. AdapterProperties 和 DeviceProperties 共同使用

枚举常量 说明 使用范围 数据类型 访问权限
适用于 Adapter 和 Remote Device
BT_PROPERTY_BDNAME 设备名称 Adapter: 读/写Remote Device: 只读 bt_bdname_t GET / SET(Adapter)GET(Remote)
BT_PROPERTY_BDADDR 设备地址 Adapter & Remote Device RawAddress GET
BT_PROPERTY_UUIDS 支持的服务 UUID 列表 Remote Device bluetooth::Uuid[] GET
BT_PROPERTY_CLASS_OF_DEVICE 类别码 Remote Device uint32_t GET
BT_PROPERTY_TYPE_OF_DEVICE 设备类型(BR/EDR/LE) Remote Device bt_device_type_t GET
BT_PROPERTY_SERVICE_RECORD 服务记录 Remote Device bt_service_record_t GET

2. 仅 AdapterProperties 使用

枚举常量 说明 使用范围 数据类型 访问权限
仅适用于 Adapter(本地适配器)
BT_PROPERTY_ADAPTER_SCAN_MODE 扫描模式(可发现性) Adapter bt_scan_mode_t GET / SET
BT_PROPERTY_ADAPTER_BONDED_DEVICES 已绑定设备地址列表 Adapter RawAddress[] GET
BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT 可发现超时时间 Adapter uint32_t GET / SET
BT_PROPERTY_LOCAL_LE_FEATURES 本地 LE 特性 Adapter bt_local_le_features_t GET
BT_PROPERTY_LOCAL_IO_CAPS 本地 IO 能力(经典蓝牙) Adapter bt_io_cap_t GET / SET
BT_PROPERTY_LOCAL_IO_CAPS_BLE 本地 IO 能力(BLE) Adapter bt_io_cap_t GET / SET
BT_PROPERTY_DYNAMIC_AUDIO_BUFFER 音频缓冲设置(动态) Adapter 自定义类型 (未明确)

3. 仅 DeviceProperties 使用

枚举常量 说明 使用范围 数据类型 访问权限
仅适用于 Remote Device(远程设备)
BT_PROPERTY_REMOTE_FRIENDLY_NAME 远程设备名称(用户设定) Remote Device bt_bdname_t GET / SET
BT_PROPERTY_REMOTE_RSSI 远程设备 RSSI Remote Device int8_t GET
BT_PROPERTY_REMOTE_VERSION_INFO 远程设备协议版本信息 Remote Device bt_remote_version_t GET / SET
BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER 是否是协同设备成员 Remote Device bool GET
BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 属性刷新时间戳 Remote Device int64_t(或自定义) GET

4. 小结

分类 枚举项
Adapter 专属属性 ADAPTER_SCAN_MODE, ADAPTER_BONDED_DEVICES, ADAPTER_DISCOVERABLE_TIMEOUT, LOCAL_LE_FEATURES, LOCAL_IO_CAPS, LOCAL_IO_CAPS_BLE, DYNAMIC_AUDIO_BUFFER
Remote Device 专属属性 REMOTE_FRIENDLY_NAME, REMOTE_RSSI, REMOTE_VERSION_INFO, REMOTE_IS_COORDINATED_SET_MEMBER, REMOTE_DEVICE_TIMESTAMP, CLASS_OF_DEVICE, TYPE_OF_DEVICE, SERVICE_RECORD, UUIDS
Adapter 与 Remote 共用属性 BDNAME, BDADDR

6. 总结重点

关键点 说明
AdapterProperties 管理本地适配器的属性,初始化时加载并可写入 bt_config.conf
DeviceProperties 管理远程设备属性,仅在配对后写入 bt_config.conf
StorageModule 所有属性存储的中间桥梁
bt_config.conf 存储持久化蓝牙信息的文件,位于 /data/misc/bluedroid
扫描行为是否写入文件? ❌ 不会,只有绑定/配对设备才写入

接下来我会出单独的文章来总结 DevicePropertiesAdapterProperties.

敬请期待!!!

你可能感兴趣的:(android,15,蓝牙协议栈分析,android,aosp13,bt,bluetooth,framework,bt.server)