android 蓝牙driver的启动流程

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

你可能感兴趣的:(android代码分析)