Android Bluedroid 框架图
蓝牙组件
Bluetooth core stack library
HCI library
Vendor Specific HCI library
UART, RFKILL,TUN/TAP and UHID device drivers
相关模块
bluetooth.apk :   packagesappsBluetooth目录下
libbluetooth_jni:  packagesappsBluetoothjni目录下
libhardware: hardwarelibhardware目录下  加载 module.{vendor}.so ,蓝牙模块就是bluetooth.{vendor}.so
bluetooth.default.so: externalbluetoothbluedroidmain , externalbluetoothbluedroid 打开各个厂商的动态库libbt-vendor_{vendor}.so, eg:libbt-vendor_rtk.so
依赖libbt-hci.so、libbt-utils.so、libbt-vendor_xxx.so
libbt-vendor_xxx.so:  bluetooth.default.so 或者bluetooth.{vendor}.so会打开该库
audio.a2dp.default.so:高级音频传输库
主体调用流程
(1) BluetoothManagerService == mBluetooth(AdapterService)                                                       ==>
(2) AdapterService  == classInitNative, initNative, enableNative                                                     ==> 
(3) com_android_bluetooth_btservice_AdapterService == hw_get_module // libhardware.so ==> 
classInitNative:
- ro.hardware.bluetooth=vendor 
- load(../../bluetooth.vendor.so)
- module->methods->open()
 initNative:
- sBluetoothInterface->init()   // bluetooth.default.so (externalbluetoothbluedroidbtifsrcbluetooth.c)
 bt_utils_init ,
 btif_init_bluetooth() //btif_core.c BTIF main task prepares BT scheduler for startup
 --->btif_config_init
 --->bte_main_boot_entry
 --->GKI_init()
 --->bte_main_in_hw_init() //Internal helper function for chip hardware init
 --->bt_hc_get_interface() // libbt-hci.so
 --->bte_load_conf()
 --->bte_load_ble_conf()
 --->....
 --->btif_fetch_local_bdaddr
 --->GKI_create_task(btif_task,...,..)
 
- sBluetoothInterface->set_os_callouts()
- sBluetoothInterface->get_profile_interface(BT_PROFILE_SOCKETS_ID)
- sBluetoothInterface->get_profile_interface(BT_PROFILE_MAP_CLIENT_ID)
- sBluetoothMceInterface->init()
 enableNative:
- enable() // bluetooth.default.so externalbluetoothbluedroidbtifsrcbluetooth.c
- btif_enable_bluetooth()
- bte_main_enable()
 --->GKI_create_task(btu_task,...,..)
 --->bte_hci_enable()
 ---->bt_hc_if->init() // bt_hc_if为bt_hc_interface_t类型,也bt_hci_bdroid.c中init
 --->vendor_open() // 打开libbt-vendor.so 可以配置成各个厂商的库,eg:bcm的libbt-vendor_bcm.so
 --->utils_init()
 --->userial_init()
 --->....
主干流程: Bluetooth.apk  --> libhardware.so  --> bluetooth.{vendor}.so --> libbt-hci.so   --> libbt-vendor_xxx.so
hardware层移植任务
(1)对应厂商的bluetooth.{vendor}.so库的实现  // 参考bluetooth.default.so
完成hw_module_t  结构体HAL_MODULE_INFO_SYM的填充,相关结构体hw_module_methods_t,bt_interface_t方法的实现
typedef struct {
    /** set to sizeof(bt_interface_t) */
    size_t size;
    /**
     * Opens the interface and provides the callback routines
     * to the implemenation of this interface.
     */
    int (*init)(bt_callbacks_t* callbacks );
    /** Enable Bluetooth. */
    int (*enable)(void);
    /** Disable Bluetooth. */
    int (*disable)(void);
    /** Closes the interface. */
    void (*cleanup)(void);
    /** Get all Bluetooth Adapter properties at init */
    int (*get_adapter_properties)(void);
    /** Get Bluetooth Adapter property of 'type' */
    int (*get_adapter_property)(bt_property_type_t type);
    /** Set Bluetooth Adapter property of 'type' */
    /* Based on the type, val shall be one of
     * bt_bdaddr_t or bt_bdname_t or bt_scanmode_t etc
     */
    int (*set_adapter_property)(const bt_property_t *property);
    /** Get all Remote Device properties */
    int (*get_remote_device_properties)(bt_bdaddr_t *remote_addr);
    /** Get Remote Device property of 'type' */
    int (*get_remote_device_property)(bt_bdaddr_t *remote_addr,
                                      bt_property_type_t type);
    /** Set Remote Device property of 'type' */
    int (*set_remote_device_property)(bt_bdaddr_t *remote_addr,
                                      const bt_property_t *property);
    /** Get Remote Device's service record  for the given UUID */
    int (*get_remote_service_record)(bt_bdaddr_t *remote_addr,
                                     bt_uuid_t *uuid);
    /** Start SDP to get remote services */
    int (*get_remote_services)(bt_bdaddr_t *remote_addr);
    /** Start Discovery */
    int (*start_discovery)(void);
    /** Cancel Discovery */
    int (*cancel_discovery)(void);
    /** Create Bluetooth Bonding */
    int (*create_bond)(const bt_bdaddr_t *bd_addr, int transport);
    /** Remove Bond */
    int (*remove_bond)(const bt_bdaddr_t *bd_addr);
    /** Cancel Bond */
    int (*cancel_bond)(const bt_bdaddr_t *bd_addr);
    /**
     * Get the connection status for a given remote device.
     * return value of 0 means the device is not connected,
     * non-zero return status indicates an active connection.
     */
    int (*get_connection_state)(const bt_bdaddr_t *bd_addr);
    /** BT Legacy PinKey Reply */
    /** If accept==FALSE, then pin_len and pin_code shall be 0x0 */
    int (*pin_reply)(const bt_bdaddr_t *bd_addr, uint8_t accept,
                     uint8_t pin_len, bt_pin_code_t *pin_code);
    /** BT SSP Reply - Just Works, Numeric Comparison and Passkey
     * passkey shall be zero for BT_SSP_VARIANT_PASSKEY_COMPARISON &
     * BT_SSP_VARIANT_CONSENT
     * For BT_SSP_VARIANT_PASSKEY_ENTRY, if accept==FALSE, then passkey
     * shall be zero */
    int (*ssp_reply)(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
                     uint8_t accept, uint32_t passkey);
    /** Get Bluetooth profile interface */
    const void* (*get_profile_interface) (const char *profile_id);
    /** Bluetooth Test Mode APIs - Bluetooth must be enabled for these APIs */
    /* Configure DUT Mode - Use this mode to enter/exit DUT mode */
    int (*dut_mode_configure)(uint8_t enable);
    /* Send any test HCI (vendor-specific) command to the controller. Must be in DUT Mode */
    int (*dut_mode_send)(uint16_t opcode, uint8_t *buf, uint8_t len);
    /** BLE Test Mode APIs */
    /* opcode MUST be one of: LE_Receiver_Test, LE_Transmitter_Test, LE_Test_End */
    int (*le_test_mode)(uint16_t opcode, uint8_t *buf, uint8_t len);
    /* enable or disable bluetooth HCI snoop log */
    int (*config_hci_snoop_log)(uint8_t enable);
    /** Sets the OS call-out functions that bluedroid needs for alarms and wake locks.
      * This should be called immediately after a successful |init|.
      */
    int (*set_os_callouts)(bt_os_callouts_t *callouts);
    /** Read Energy info details - return value indicates BT_STATUS_SUCCESS or BT_STATUS_NOT_READY
      * Success indicates that the VSC command was sent to controller
      */
    int (*read_energy_info)();
    // MStar Android Patch Begin
    /** This ensures the chip is Powered ON  to support other radios in the combo chip.
     * If the chip is OFF it set the chip to ON, if it is already ON it just increases the radio ref count
     * to keep track when to Power OFF */
    int (*enableRadio)(void);
    /** This decreases radio ref count  and ensures that chip is Powered OFF
     * when the radio ref count becomes zero. */
    int (*disableRadio)(void);
     /* Send any test HCI (vendor-specific) command */
     int (*send_vsc)(uint16_t opcode, uint8_t *buf, uint8_t len);
     /**Set secure mode configurations*/
     int (*secure_mode_configure)(uint8_t secure_mode, uint8_t scans_disabled,
                                 uint8_t insec_pair_disabled);
     /**Force disable bluetooth*/
     void (*force_disable) ();
     /** Add/remove SDP record */
     int (*set_sdp_record)(uint8_t enable, uint16_t uuid);
    // MStar Android Patch End
} bt_interface_t;详参externalbluetoothbluedroidbtifsrcbluetooth.c
(2)完成libbt-vendor_{vendor}.so模块的实现 主要bt_vendor_interface_t结构体中init,op,cleanup方法的实现
typedef struct {
    /** Set to sizeof(bt_vndor_interface_t) */
    size_t          size;
    /*
     * Functions need to be implemented in Vendor libray (libbt-vendor.so).
     */
    /**
     * Caller will open the interface and pass in the callback routines
     * to the implemenation of this interface.
     */
    int   (*init)(const bt_vendor_callbacks_t* p_cb, unsigned char *local_bdaddr);
    /**  Vendor specific operations */
    int (*op)(bt_vendor_opcode_t opcode, void *param);
    /** Closes the interface */
    void  (*cleanup)(void);
} bt_vendor_interface_t;动态加载节点 (1) hardware层 bluetooth.{vendor}.so 加载 hardware.c文件中可以看到, load 加载的库在/system/lib64/hw,/system/lib/hw,/vendor/lib64/hw,/vendor/lib/hw之中,但是加载的库名称根据 定义的ro.hardware.bluetooth的值来确定
(2)bluetooth.{vendor}.so加载 libbt-hci.so
(3)libbt-hci.so加载libbt-vendor_xxx.so externalbluetoothbluedroidhcisrcvendor.c 文件会根据VENDOR_LIBRARY_NAME定义的值来加载动态库,默认是libbt-vendor.so
相关链接: 移植:http://blog.csdn.net/xiaojsj111/article/details/12647923#t2 蓝牙简介:http://blog.chinaunix.net/uid-24227137-id-3359410.html Hiconfig:http://blog.csdn.net/xubin341719/article/details/38640533