1. GATT Service Class Initialization
packages/apps/Bluetooth/src/com/android/bluetooth/gatt/GattService.java
public class GattService extends ProfileService {
static {
classInitNative();
}
private static native void classInitNative();
packages/apps/Bluetooth/jni/com_android_bluetooth_gatt.cpp
static void classInitNative(JNIEnv* env, jclass clazz) {
// Client callbacks
method_onClientRegistered =
env->GetMethodID(clazz, "onClientRegistered", "(IIJJ)V");
......
// Server callbacks
method_onServerRegistered =
env->GetMethodID(clazz, "onServerRegistered", "(IIJJ)V");
......
}
从以上代码流程可知,classInitNative 用于建立 JNI层函数 和 Java层方法 之间的映射关系,从而达到从 JNI 层调用函数在 Java 层执行方法的目的。
2. GATT Service Super Initialization
3. GATT Service Object Initialization
packages/apps/Bluetooth/src/com/android/bluetooth/gatt/GattService.java
public class GattService extends ProfileService {
@Override
protected boolean start() {
mExposureNotificationPackage = getString(R.string.exposure_notification_package);
Settings.Global.putInt(
getContentResolver(), "bluetooth_sanitized_exposure_notification_supported", 1);
// 1 GATT 初始化
initializeNative();
mAdapter = BluetoothAdapter.getDefaultAdapter();
//
mCompanionManager = ICompanionDeviceManager.Stub.asInterface(
ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE));
mAppOps = getSystemService(AppOpsManager.class);
// 2
mAdvertiseManager = new AdvertiseManager(this, AdapterService.getAdapterService());
mAdvertiseManager.start();
// 3
mScanManager = new ScanManager(this);
mScanManager.start();
// 4
mPeriodicScanManager = new PeriodicScanManager(AdapterService.getAdapterService());
mPeriodicScanManager.start();
// 将本类对象赋值到静态变量上
setGattService(this);
return true;
}
}
3.1 GATT Service initializeNative
packages/apps/Bluetooth/src/com/android/bluetooth/gatt/GattService.java
private native void initializeNative();
packages/apps/Bluetooth/jni/com_android_bluetooth_gatt.cpp
static const btgatt_interface_t* sGattIf = NULL;
static void initializeNative(JNIEnv* env, jobject object) {
btIf = getBluetoothInterface();
sGattIf =
(btgatt_interface_t*)btIf->get_profile_interface(BT_PROFILE_GATT_ID);
bt_status_t status = sGattIf->init(&sGattCallbacks);
mCallbacksObj = env->NewGlobalRef(object);
}
get_profile_interface() 函数初始化系列前文已经分析过,用于获取指定的 profile 的入口。此处不再详述其过程,通过入口调用初始化函数代码如下文所示。
system/bt/btif/src/btif_gatt.cc
static bt_status_t btif_gatt_init(const btgatt_callbacks_t* callbacks) {
bt_gatt_callbacks = callbacks;
return BT_STATUS_SUCCESS;
}
static btgatt_interface_t btgattInterface = {
sizeof(btgattInterface),
btif_gatt_init,
btif_gatt_cleanup,
&btgattClientInterface,
&btgattServerInterface,
nullptr, // filled in btif_gatt_get_interface
nullptr // filled in btif_gatt_get_interface
};
const btgatt_interface_t* btif_gatt_get_interface() {
btgattInterface.scanner = get_ble_scanner_instance();
btgattInterface.advertiser = get_ble_advertiser_instance();
return &btgattInterface;
}
GATT 初始化完成,已经可以获取到 scanner 和 advertiser 的入口,并且将回调函数注入到 GATT btif 层。
3.2 AdvertiseManager
packages/apps/Bluetooth/src/com/android/bluetooth/gatt/AdvertiseManager.java
class AdvertiseManager {
static {
classInitNative();
}
AdvertiseManager(GattService service, AdapterService adapterService) {
mService = service;
mAdapterService = adapterService;
}
void start() {
initializeNative();
HandlerThread thread = new HandlerThread("BluetoothAdvertiseManager");
thread.start();
mHandler = new Handler(thread.getLooper());
}
private static native void classInitNative();
private native void initializeNative();
packages/apps/Bluetooth/jni/com_android_bluetooth_gatt.cpp
static JNINativeMethod sAdvertiseMethods[] = {
{"classInitNative", "()V", (void*)advertiseClassInitNative},
{"initializeNative", "()V", (void*)advertiseInitializeNative},
......
}
static void advertiseClassInitNative(JNIEnv* env, jclass clazz) {
method_onAdvertisingSetStarted =
env->GetMethodID(clazz, "onAdvertisingSetStarted", "(IIII)V");
......
}
static void advertiseInitializeNative(JNIEnv* env, jobject object) {
......
mAdvertiseCallbacksObj = env->NewGlobalRef(object);
}
3.3 ScanManager
packages/apps/Bluetooth/src/com/android/bluetooth/gatt/ScanManager.java
public class ScanManager {
ScanManager(GattService service) {
mRegularScanClients =
Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
mBatchClients = Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
mSuspendedScanClients =
Collections.newSetFromMap(new ConcurrentHashMap<ScanClient, Boolean>());
mService = service;
mScanNative = new ScanNative();
mCurUsedTrackableAdvertisements = 0;
mDm = (DisplayManager) mService.getSystemService(Context.DISPLAY_SERVICE);
mActivityManager = (ActivityManager) mService.getSystemService(Context.ACTIVITY_SERVICE);
mLocationManager = (LocationManager) mService.getSystemService(Context.LOCATION_SERVICE);
}
void start() {
HandlerThread thread = new HandlerThread("BluetoothScanManager");
thread.start();
mHandler = new ClientHandler(thread.getLooper());
if (mDm != null) {
mDm.registerDisplayListener(mDisplayListener, null);
}
if (mActivityManager != null) {
mActivityManager.addOnUidImportanceListener(mUidImportanceListener,
FOREGROUND_IMPORTANCE_CUTOFF);
}
IntentFilter locationIntentFilter = new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
mService.registerReceiver(mLocationReceiver, locationIntentFilter);
}
}
常规的初始化流程,暂无特别需要介绍的内容。
3.4 PeriodicScanManager
packages/apps/Bluetooth/src/com/android/bluetooth/gatt/PeriodicScanManager.java
class PeriodicScanManager {
static {
classInitNative();
}
PeriodicScanManager(AdapterService adapterService) {
mAdapterService = adapterService;
}
void start() {
initializeNative();
}
private static native void classInitNative();
private native void initializeNative();
}
packages/apps/Bluetooth/jni/com_android_bluetooth_gatt.cpp
static JNINativeMethod sPeriodicScanMethods[] = {
{"classInitNative", "()V", (void*)periodicScanClassInitNative},
{"initializeNative", "()V", (void*)periodicScanInitializeNative},
......
}
static void periodicScanClassInitNative(JNIEnv* env, jclass clazz) {
method_onSyncStarted =
env->GetMethodID(clazz, "onSyncStarted", "(IIIILjava/lang/String;III)V");
......
}
static void periodicScanInitializeNative(JNIEnv* env, jobject object) {
......
mPeriodicScanCallbacksObj = env->NewGlobalRef(object);
}
常规的初始化流程,暂无特别需要介绍的内容。