DeviceProperties
、AdapterProperties
、StorageModule
、以及 bt_config.conf
是 AOSP Bluetooth 棧中 設備屬性管理與持久化系統 的核心組成部分,它們之間關系緊密,但職責各有不同。
下面我將依次講解它們的區別與聯系.
注意:
- 在代碼里面 還有
BluetoothProperties
: 他是管理 藍牙相關的系統屬性的, 和本文討論的DeviceProperties
、AdapterProperties
不是同一個話題。 - 有興趣可以參看: 【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 |
掃描行為是否寫入文件? | ? 不會,只有綁定/配對設備才寫入 |
接下來我會出單獨的文章來總結 DeviceProperties
、AdapterProperties
.
敬請期待!!!