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时区的初始化
  2. android adb配置环境变量
  3. Android jni系统变量、函数、接口定义汇总
  4. (2) Android中Binder调用流程 --- Binder环境的初始化
  5. OpenGL,Android注意事项初始化顺序 NullPointer
  6. Android P WMS初始化过程

随机推荐

  1. Android根据当前时间获取前面的时间日期,
  2. Android(安卓)相关知识
  3. 这些年我踩过的坑——Android
  4. Fragment报错记录
  5. android -------- GifView 显示gif图片
  6. BlobCache算法详解
  7. Android序列化 Serializable使用和Parcel
  8. Android通过手机GPS获取经纬度方法
  9. android studio-toolbar的使用
  10. 树莓派---MAC下安装AndroidThings