协议栈源码位置: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_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"
例如:音视频播放时的按键控制:#define BT_PROFILE_AV_RC_ID "avrcp"