您的位置:首页 > 产品设计 > UI/UE

Bluetooth的子协议学习系列-HL

2015-09-06 15:50 453 查看
分析蓝牙很重要的一部分就是要搞懂JNI到bluedroid的调用关系.

代码路径为:

Z:\8939_21391\packages\apps\Bluetooth\jni\com_android_bluetooth_hdp.cpp

#define LOG_TAG "BluetoothHealthServiceJni"

#define LOG_NDEBUG 0

#define CHECK_CALLBACK_ENV \

if (!checkCallbackThread()) { \

ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__);\

return; \

}

#include "com_android_bluetooth.h"

#include "hardware/bt_hl.h"

#include "utils/Log.h"

#include "android_runtime/AndroidRuntime.h"

#include <string.h>

namespace android {

static jmethodID method_onAppRegistrationState;

static jmethodID method_onChannelStateChanged;

static const bthl_interface_t
*sBluetoothHdpInterface = NULL;//这里的bthl_interface_t 就是bluedroid里面定义的类型.

static jobject mCallbacksObj = NULL;

static void initializeNative(JNIEnv *env, jobject object) {
    const bt_interface_t* btInf;
    bt_status_t status;

    if ( (btInf = getBluetoothInterface()) == NULL) {
        ALOGE("Bluetooth module is not loaded");
        return;
    }

    if (sBluetoothHdpInterface !=NULL) {
         ALOGW("Cleaning up Bluetooth Health Interface before initializing...");
         sBluetoothHdpInterface->cleanup();
         sBluetoothHdpInterface = NULL;
    }

    if (mCallbacksObj != NULL) {
         ALOGW("Cleaning up Bluetooth Health callback object");
         env->DeleteGlobalRef(mCallbacksObj);
         mCallbacksObj = NULL;
    }

    if ( (sBluetoothHdpInterface = (bthl_interface_t *)
          btInf->get_profile_interface(BT_PROFILE_HEALTH_ID)) == NULL) {

// get_profile_interface 实现在bluedroid里面的.bluetooth.c 里面.根据//BT_PROFILE_HEALTH_ID 获取对应的interface
//.Z:\8939_21391\external\bluetooth\bluedroid\btif\src\bluetooth.c
ALOGE("Failed to get Bluetooth Health Interface"); return; }
// 通过hal层代码来获取bluedroid的interface来进行init 初始化操作

if ( (status = sBluetoothHdpInterface->init(&sBluetoothHdpCallbacks)) != BT_STATUS_SUCCESS) {

ALOGE("Failed to initialize Bluetooth HDP, status: %d", status);

sBluetoothHdpInterface = NULL;

return;

}

mCallbacksObj = env->NewGlobalRef(object);

}


static jint registerHealthAppNative(JNIEnv *env, jobject object, jint data_type,

jint role, jstring name, jint channel_type) {

bt_status_t status;

bthl_mdep_cfg_t mdep_cfg;

bthl_reg_param_t reg_param;

int app_id;

if (!sBluetoothHdpInterface) return NULL;

mdep_cfg.mdep_role = (bthl_mdep_role_t) role;

mdep_cfg.data_type = data_type;

mdep_cfg.channel_type = (bthl_channel_type_t) channel_type;

// TODO(BT) pass all the followings in from java instead of reuse name

mdep_cfg.mdep_description = env->GetStringUTFChars(name, NULL);

reg_param.application_name = env->GetStringUTFChars(name, NULL);

reg_param.provider_name = NULL;

reg_param.srv_name = NULL;

reg_param.srv_desp = NULL;

reg_param.number_of_mdeps = 1;

reg_param.mdep_cfg = &mdep_cfg;

if ( (status = sBluetoothHdpInterface->register_application(®_param, &app_id)) !=

BT_STATUS_SUCCESS) {

ALOGE("Failed register health app, status: %d", status);

return -1;

}

env->ReleaseStringUTFChars(name, mdep_cfg.mdep_description);

env->ReleaseStringUTFChars(name, reg_param.application_name);

return app_id;

}

sBluetoothHdpInterface->register_application 这部分实现在

Z:\8939_21391\external\bluetooth\bluedroid\btif\src\btif_hl.c

static const bthl_interface_t bthlInterface = {

sizeof(bthl_interface_t),

init,

register_application,//这里就是上面调用的register_application 接口实现.

unregister_application,

connect_channel,

destroy_channel,

cleanup,

};

从这个流程可以看到HL协议在运行时需要走的流程. 其他的service 和bta 部分的分析后续会写.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: