android 蓝牙driver的启动流程

    • Bluetooth.apk
    • JNI

Bluetooth.apk

packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java

  1. AdapterService有一static修饰的代码块:
    static {        System.load("/system/lib/libbluetooth_jni.so");        classInitNative();    }

这里面做了一些初始化操作,例如给sBluetoothInterface赋值。
2. AdapterService的onCreate()方法:

    @Override    public void onCreate() {        super.onCreate();        debugLog("onCreate()");        mBinder = new AdapterServiceBinder(this);        mAdapterProperties = new AdapterProperties(this);        mVendor = new Vendor(this);        mAdapterStateMachine =  AdapterState.make(this, mAdapterProperties, mVendor);        mJniCallbacks =  new JniCallbacks(mAdapterStateMachine, mAdapterProperties);        initNative();        mNativeAvailable=true;        mCallbacks = new RemoteCallbackList<IBluetoothCallback>();        //Load the name and address        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDADDR);        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME);        mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);        mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(                BatteryStats.SERVICE_NAME));        mSdpManager = SdpManager.init(this);        registerReceiver(mAlarmBroadcastReceiver, new IntentFilter(ACTION_ALARM_WAKEUP));        mProfileObserver = new ProfileObserver(getApplicationContext(), this, new Handler());        mProfileObserver.start();        mVendor.init();        setAdapterService(this);    }

这里面大概就是初始化一些类等等,我们要跟的是initNative(),可以看到,这里就该到了JNI。

private native static void initNative();

JNI

static bool initNative(JNIEnv* env, jobject obj) {    ALOGV("%s:",__FUNCTION__);    android_bluetooth_UidTraffic.clazz = (jclass) env->NewGlobalRef(            env->FindClass("android/bluetooth/UidTraffic"));    sJniAdapterServiceObj = env->NewGlobalRef(obj);    if (sJniCallbacksField) {        sJniCallbacksObj = env->NewGlobalRef(env->GetObjectField(obj, sJniCallbacksField));    } else {        ALOGE("Error: sJniCallbacksField is null\n");    }    if (sBluetoothInterface) {        int ret = sBluetoothInterface->init(&sBluetoothCallbacks);        if (ret != BT_STATUS_SUCCESS && ret != BT_STATUS_DONE) {            ALOGE("Error while setting the callbacks: %d\n", ret);            sBluetoothInterface = NULL;            return JNI_FALSE;        }        /*disable these os_callout settings, so that native wake_lock will be enabled*/#if 0        ret = sBluetoothInterface->set_os_callouts(&sBluetoothOsCallouts);        if (ret != BT_STATUS_SUCCESS) {            ALOGE("Error while setting Bluetooth callouts: %d\n", ret);            sBluetoothInterface->cleanup();            sBluetoothInterface = NULL;            return JNI_FALSE;        }#endif        if ( (sBluetoothSocketInterface = (btsock_interface_t *)                  sBluetoothInterface->get_profile_interface(BT_PROFILE_SOCKETS_ID)) == NULL) {                ALOGE("Error getting socket interface");        }        return JNI_TRUE;    }    return JNI_FALSE;}

这里有个重要变量:sBluetoothInterface,该变量的赋值在第一步中。
//To Do

更多相关文章

  1. android installd分析
  2. Android(安卓)MediaScannerJNI源码详解
  3. android时区的初始化
  4. android adb配置环境变量
  5. Android(安卓)jni系统变量、函数、接口定义汇总
  6. android stuio eclipse映射下的快捷键
  7. Android(安卓)AVD启动失败
  8. 基于Android(安卓)Volley的网络请求工具
  9. [Android(安卓)Studio] Android(安卓)studio 多渠道打包(超简洁

随机推荐

  1. Android(安卓)ListView
  2. Android中SQLite数据库的使用(总结)
  3. Android中的SharedPreference源码整理总
  4. android 触摸事件和点击事件执行顺序
  5. 如何在你的App中应用Material Design设计
  6. cocos2dx android 程序启动黑屏相关
  7. android 源码和兼容包
  8. Useful related java API for Android
  9. Android(安卓)去除标题栏
  10. Android里面从Dialog中获取返回值并写入