【APP】
应用程序通过BluetoothManager
和BluetoothAdapter
来操作蓝牙相关接口
【JNI】AdaptorService 中打开蓝牙协议栈的代码片段
static void classInitNative(JNIEnv* env, jclass clazz) {
int err;
hw_module_t* module;
...
char value[PROPERTY_VALUE_MAX];
property_get("bluetooth.mock_stack", value, "");
const char *id = (strcmp(value, "1")? BT_STACK_MODULE_ID : BT_STACK_TEST_MODULE_ID);
err = hw_get_module(id, (hw_module_t const**)&module);
if (err == 0) {
hw_device_t* abstraction;
err = module->methods->open(module, id, &abstraction);
if (err == 0) {
bluetooth_module_t* btStack = (bluetooth_module_t *)abstraction;
sBluetoothInterface = btStack->get_bluetooth_interface();
} else {
ALOGE("Error while opening Bluetooth library");
}
} else {
ALOGE("No Bluetooth Library found");
}
}
【HAL】
/system/bt/btif/src/bluetooth.c
HAL层注册了蓝牙协议栈,它作为一个HAL层的“设备”,为上层服务。
tatic struct hw_module_methods_t bt_stack_module_methods = {
.open = open_bluetooth_stack,
};
struct hw_module_t HAL_MODULE_INFO_SYM = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = BT_HARDWARE_MODULE_ID,
.name = "Bluetooth Stack",
.author = "The Android Open Source Project",
.methods = &bt_stack_module_methods
};
【Interface】Blutooth接口函数
/system/bt/btif/src/bluetooth.c
static const bt_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,
get_connection_state,
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,
set_os_callouts,
read_energy_info,
dump,
config_clear
};
【Profile】HF client侧接口
/system/bt/btif/src/btif_hf_client.c
static const bthf_client_interface_t bthfClientInterface = {
sizeof(bthf_client_interface_t),
.init = init,
.connect = connect,
.disconnect = disconnect,
.connect_audio = connect_audio,
.disconnect_audio = disconnect_audio,
.start_voice_recognition = start_voice_recognition,
.stop_voice_recognition = stop_voice_recognition,
.volume_control = volume_control,
.dial = dial,
.dial_memory = dial_memory,
.handle_call_action = handle_call_action,
.query_current_calls = query_current_calls,
.query_current_operator_name = query_current_operator_name,
.retrieve_subscriber_info = retrieve_subscriber_info,
.send_dtmf = send_dtmf,
.request_last_voice_tag_number = request_last_voice_tag_number,
.cleanup = cleanup,
.send_at_cmd = send_at_cmd,
};