首先我们都知道蓝牙第一步是上电,但是android4.4蓝牙上电部分的代码实际已经和android4.3不一样了。
android4.3蓝牙os是走system/bluetooth,但是android4.4走的是hardware/libhardware和external/bluetooth/,具体请看下面:
Bluetooth.h(hardware/libhardware/include/hardware/)----这是结构体,主要是看enable函数。
[cpp] view plain copy
typedefstruct{
/** set to sizeof(bt_interface_t) */
size_tsize;
/**
* Opens the interface and provides the callback routines
* to the implemenation of this interface.
*/
int(*init)(bt_callbacks_t* callbacks );
/** Enable Bluetooth. */
int(*"color:#ff0000;">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)(constbt_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,
constbt_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)(constbt_bdaddr_t *bd_addr);
/** Remove Bond */
int(*remove_bond)(constbt_bdaddr_t *bd_addr);
/** Cancel Bond */
int(*cancel_bond)(constbt_bdaddr_t *bd_addr);
/** BT Legacy PinKey Reply */
/** If accept==FALSE, then pin_len and pin_code shall be 0x0 */
int(*pin_reply)(constbt_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)(constbt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
uint8_t accept, uint32_t passkey);
/** Get Bluetooth profile interface */
constvoid* (*get_profile_interface) (constchar*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);
} bt_interface_t;
有结构体就会要找结构体对照的函数实现:
Bluetooth.c (external\bluetooth\bluedroid\btif\src)
[cpp] view plain copy
constbt_interface_t* bluetooth__get_bluetooth_interface ()
{
/* fixme -- add property to disable bt interface ? */
return&bluetoothInterface;
}
[cpp] view plain copy
staticconstbt_interface_t bluetoothInterface = {
sizeof(bluetoothInterface),
init,
enable,
disable,
cleanup,
get_adapter_properties,
get_adapter_property,
set_adapter_property,
get_remote_device_properties,
get_remote_device_property,
set_remote_device_property,
get_remote_service_record,
get_remote_services,
start_discovery,
cancel_discovery,
create_bond,
remove_bond,
cancel_bond,
pin_reply,
ssp_reply,
get_profile_interface,
dut_mode_configure,
dut_mode_send,
#if BLE_INCLUDED == TRUE
le_test_mode,
#else
NULL,
#endif
config_hci_snoop_log
};
找到enable函数了,那就要看看他的实现:
[cpp] view plain copy
staticintenable(void)
{
ALOGI("enable");
/* sanity check */
if(interface_ready() == FALSE)
returnBT_STATUS_NOT_READY;
returnbtif_enable_bluetooth();
}
接下来就是按照函数实现一步一步走了,但是btif_enable_bluetooth()是有声明函数的,一定要注意,在btif_api.h,这里就不贴代码了。继续:
Btif_core.c (\external\bluetooth\bluedroid\btif\src)
[cpp] view plain copy
bt_status_t btif_enable_bluetooth(void)
{
BTIF_TRACE_DEBUG0("BTIF ENABLE BLUETOOTH");
if(btif_core_state != BTIF_CORE_STATE_DISABLED)
{
ALOGD("not disabled\n");
returnBT_STATUS_DONE;
}
btif_core_state = BTIF_CORE_STATE_ENABLING;
/* Create the GKI tasks and run them */
bte_main_enable();
returnBT_STATUS_SUCCESS;
}
接下来我们要去找bte_main_enable函数的实现了:
[cpp] view plain copy
voidbte_main_enable()
{
APPL_TRACE_DEBUG1("%s", __FUNCTION__);
/* Init