藍芽協議棧分析
阿新 • • 發佈:2019-01-10
協議棧原始碼位置:external/bluetooth/bluedroid
藍芽協議棧架構:
描述了協議棧Bluedroid,HAL層藍芽適配庫以及上層應用類 模組及應用程式介面
Bluedroid 分為兩層: - BTE: Bluetooth Embedded System // 實現核心的藍芽功能
- BTA: Bluetooth Application Layer // 與框架的應用程式進行通訊 framework :與藍芽裝置互動原理是通過Binder IPC機制使用藍芽服務 ,程式碼位置framework/base/core/java/android/bluetooth Bluetooth Process:一個Android應用程式通過JNI與藍芽協議棧互動,在Android框架層實現了藍芽的服務和Profiles
JNI:通過回撥的方式,使用HAL層介面對藍芽裝置進行進行操作。
HAL:定義了各種Profiles下使用藍芽的標準介面,標頭檔案位於hardware/libhardware/include/hardware
bluetooth.h: Contains the HAL for the Bluetooth hardware on the device
bt_av.h: Contains the HAL for the advanced audio profile.
bt_hf.h: Contains the HAL for the handsfree profile.
bt_hh.h: Contains the HAL for the HID host profile
bt_hl.h: Contains the HAL for the health profile
bt_pan.h: Contains the HAL for the pan profile
bt_sock.h: Contains the HAL for the socket profile Bluetooth Stack: 藍芽協議棧,實現了通用的藍芽HAL及可配置的元件 Vendor extensions: 廠商通過libbt-vendor模組來自定義擴充套件介面和HCI來方便除錯 Bluetooth profile interface 能力獲取: (1)JNI層通過hw_get_module開啟藍芽協議庫; // com_android_bluetooth_btservice_AdapterService.cpp 呼叫hw_get_module (2)通過open方法得到一個hw_device_t物件; // open方法即external\bluetooth\bluedroid\btif\src\bluetooth.c 的open_bluetooth_stack (3)獲取到的hw_device_t物件也即協議棧物件bluetooth_module_t指標btStack (4)接著btStack->get_bluetooth_interface獲取到bt_interface_t指標sBluetoothInterface (5)得到bt_interface_t就可以對藍芽協議模組就行初始化,獲取profiles id (6)初始化,註冊回撥 sBluetoothInterface->init(&sBluetoothCallbacks); // 回撥 bt_callbacks_t sBluetoothCallbacks = { sizeof(sBluetoothCallbacks),
adapter_state_change_callback,
adapter_properties_callback,
remote_device_properties_callback,
device_found_callback,
discovery_state_changed_callback,
pin_request_callback,
ssp_request_callback,
bond_state_changed_callback,
acl_state_changed_callback,
callback_thread_event,
dut_mode_recv_callback,
le_test_mode_recv_callback
}; (7)獲取profiles sBluetoothInterface->get_profile_interface(BT_PROFILE_XXXXXX _ID);
// BT_PROFILE_XXXXXX_ID定義在external\bluetooth\bluedroid\btif\src\bluetooth.h
如下:
例如:音視訊播放時的按鍵控制:#define BT_PROFILE_AV_RC_ID "avrcp"
藍芽協議棧架構:
描述了協議棧Bluedroid,HAL層藍芽適配庫以及上層應用類 模組及應用程式介面
Bluedroid 分為兩層: - BTE: Bluetooth Embedded System // 實現核心的藍芽功能
- BTA: Bluetooth Application Layer // 與框架的應用程式進行通訊 framework :與藍芽裝置互動原理是通過Binder IPC機制使用藍芽服務 ,程式碼位置framework/base/core/java/android/bluetooth Bluetooth Process:一個Android應用程式通過JNI與藍芽協議棧互動,在Android框架層實現了藍芽的服務和Profiles
bt_hf.h: Contains the HAL for the handsfree profile.
bt_hh.h: Contains the HAL for the HID host profile
bt_hl.h: Contains the HAL for the health profile
bt_pan.h: Contains the HAL for the pan profile
bt_sock.h: Contains the HAL for the socket profile Bluetooth Stack: 藍芽協議棧,實現了通用的藍芽HAL及可配置的元件 Vendor extensions: 廠商通過libbt-vendor模組來自定義擴充套件介面和HCI來方便除錯 Bluetooth profile interface 能力獲取: (1)JNI層通過hw_get_module開啟藍芽協議庫; // com_android_bluetooth_btservice_AdapterService.cpp 呼叫hw_get_module (2)通過open方法得到一個hw_device_t物件; // open方法即external\bluetooth\bluedroid\btif\src\bluetooth.c 的open_bluetooth_stack (3)獲取到的hw_device_t物件也即協議棧物件bluetooth_module_t指標btStack (4)接著btStack->get_bluetooth_interface獲取到bt_interface_t指標sBluetoothInterface (5)得到bt_interface_t就可以對藍芽協議模組就行初始化,獲取profiles id (6)初始化,註冊回撥 sBluetoothInterface->init(&sBluetoothCallbacks); // 回撥 bt_callbacks_t sBluetoothCallbacks = { sizeof(sBluetoothCallbacks),
adapter_state_change_callback,
adapter_properties_callback,
remote_device_properties_callback,
device_found_callback,
discovery_state_changed_callback,
pin_request_callback,
ssp_request_callback,
bond_state_changed_callback,
acl_state_changed_callback,
callback_thread_event,
dut_mode_recv_callback,
le_test_mode_recv_callback
}; (7)獲取profiles sBluetoothInterface->get_profile_interface(BT_PROFILE_XXXXXX
#define BT_HARDWARE_MODULE_ID "bluetooth" #define BT_STACK_MODULE_ID "bluetooth" #define BT_STACK_TEST_MODULE_ID "bluetooth_test" /* Bluetooth profile interface IDs */ #define BT_PROFILE_HANDSFREE_ID "handsfree" #define BT_PROFILE_ADVANCED_AUDIO_ID "a2dp" #define BT_PROFILE_HEALTH_ID "health" #define BT_PROFILE_SOCKETS_ID "socket" #define BT_PROFILE_HIDHOST_ID "hidhost" #define BT_PROFILE_PAN_ID "pan" // proting from brcm #define BT_PROFILE_ADVANCED_AUDIO_SINK_ID "a2dpsink" #define BT_PROFILE_3D_SYNC_ID "3ds" #define BT_PROFILE_GATT_ID "gatt" #define BT_PROFILE_AV_RC_ID "avrcp" // proting from brcm #define BT_PROFILE_HIDDEVICE_ID "hiddevice"