android 蓝牙driver的启动流程
16lz
2021-12-04
android 蓝牙driver的启动流程
- Bluetooth.apk
- JNI
Bluetooth.apk
packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java
- 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 installd分析
- Android(安卓)MediaScannerJNI源码详解
- android时区的初始化
- android adb配置环境变量
- Android(安卓)jni系统变量、函数、接口定义汇总
- android stuio eclipse映射下的快捷键
- Android(安卓)AVD启动失败
- 基于Android(安卓)Volley的网络请求工具
- [Android(安卓)Studio] Android(安卓)studio 多渠道打包(超简洁