蓝牙打开-bluedroid中的流程(基于android O代码)

蓝牙打开从app层到协议栈的跨度太大,仅仅看代码很容易迷失方向,先从log看起。

01-18 18:25:31.570 D/BluetoothAdapterService(21885): onCreate()  // adapterservice启动
01-18 18:25:31.570 I/BluetoothVendorJni(21885): classInitNative: succeeds
01-18 18:25:31.570 D/BluetoothAdapterState(21885): make() - Creating AdapterState
01-18 18:25:31.580 I/BluetoothAdapterState(21885): Entering OffState
01-18 18:25:31.580 I/bt_btif (21885): init  然后调用到init方法
01-18 18:25:31.580 D/bt_osi_allocation_tracker(21885): canary initialized
01-18 18:25:31.590 I/bt_osi_thread(21885): run_thread: thread id 21915, thread name stack_manager started

在AdapterService文件里面会先调用classInitNative方法,这个方法的定义在JNI文件中,定义如下:

static void classInitNative(JNIEnv* env, jclass clazz) {
    int err;
    hw_module_t* module;

    jclass jniCallbackClass =
        env->FindClass("com/android/bluetooth/btservice/JniCallbacks");
    sJniCallbacksField = env->GetFieldID(clazz, "mJniCallbacks",
        "Lcom/android/bluetooth/btservice/JniCallbacks;");

    method_stateChangeCallback = env->GetMethodID(jniCallbackClass, "stateChangeCallback", "(I)V");

    method_adapterPropertyChangedCallback = env->GetMethodID(jniCallbackClass,
                                                             "adapterPropertyChangedCallback",
                                                             "([I[[B)V");
    method_discoveryStateChangeCallback = env->GetMethodID(jniCallbackClass,
                                                           "discoveryStateChangeCallback", "(I)V");

    method_devicePropertyChangedCallback = env->GetMethodID(jniCallbackClass,
                                                            "devicePropertyChangedCallback",
                                                            "([B[I[[B)V");
    method_deviceFoundCallback = env->GetMethodID(jniCallbackClass, "deviceFoundCallback", "([B)V");
    method_pinRequestCallback = env->GetMethodID(jniCallbackClass, "pinRequestCallback",
                                                 "([B[BIZ)V");
    method_sspRequestCallback = env->GetMethodID(jniCallbackClass, "sspRequestCallback",
                                                 "([B[BIII)V");

    method_bondStateChangeCallback = env->GetMethodID(jniCallbackClass,
                                                     "bondStateChangeCallback", "(I[BI)V");

    method_aclStateChangeCallback = env->GetMethodID(jniCallbackClass,
                                                    "aclStateChangeCallback", "(I[BI)V");

    method_setWakeAlarm = env->GetMethodID(clazz, "setWakeAlarm", "(JZ)Z");
    method_acquireWakeLock = env->GetMethodID(clazz, "acquireWakeLock", "(Ljava/lang/String;)Z");
    method_releaseWakeLock = env->GetMethodID(clazz, "releaseWakeLock", "(Ljava/lang/String;)Z");
    method_energyInfo = env->GetMethodID(clazz, "energyInfoCallback", "(IIJJJJ)V");

    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");
    }

该方法中会定义一些回调函数,并且初始化sBluetoothInterface接口, get_bluetooth_interface方法返回协议栈里面定义的方法的接口,协议栈中的方法如下所示:

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
};

将会调用到协议里面的init方法。

static int init(bt_callbacks_t *callbacks) {
  LOG_INFO("%s", __func__);

  if (interface_ready())
    return BT_STATUS_DONE;

#ifdef BLUEDROID_DEBUG
  allocation_tracker_init();
#endif

  bt_hal_cbacks = callbacks;
  stack_manager_get_interface()->init_stack();
  btif_debug_init();
  return BT_STATUS_SUCCESS;
}

在init里面会创建stack_manager线程,然后使用thread_post方法给stack_manager线程传递处理函数event_init_stack,这个函数主要用来初始化协议栈,在函数里面会调用btif_init_bluetooth, btif_init_bluetooth函数里面创建JNI线程,给这个线程传递的处理函数是run_message_loop。

至此,蓝牙协议栈初始化完成,下面进入enable过程。

01-18 18:25:31.760 I/bt_btif (21885): enable: start restricted = 0
01-18 18:25:31.760 I/bt_stack_manager(21885): event_start_up_stack is bringing up the stack

enable方法中会给stack_manager线程传递处理函数event_start_up_stack,在event_start_up_stack函数中会创建 bt_workqueue线程,该线程中的处理方法是:btu_task_start_up

01-18 18:25:31.870 I/bt_osi_thread(21885): run_thread: thread id 21934, thread name bt_workqueue started

在btu_task_start_up方法中,会初始化BTU,BTM,L2CAP,SDP. 创建btu message_loop线程,给该线程传入的处理函数是

btu_message_loop_run函数, 这个函数向JNI线程发送初始化完成的消息。

你可能感兴趣的:(蓝牙打开-bluedroid中的流程(基于android O代码))