step.1:
btif_dm.cc
static void btif_dm_upstreams_evt(uint16_t event, char* p_param) {
tBTA_DM_SEC* p_data = (tBTA_DM_SEC*)p_param;
tBTA_SERVICE_MASK service_mask;
uint32_t i;
RawAddress bd_addr;
BTIF_TRACE_EVENT("%s: ev: %s", __func__, dump_dm_event(event));
//android::CallStack stack;
//stack.update();
//stack.log("callstack");
//libutilscallstack
//debug_stamp
LOG_INFO(LOG_TAG, "%s: ev: %s", __func__, dump_dm_event(event));
HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
&bd_addr, BT_ACL_STATE_CONNECTED);
step.2:
packages/apps/bluetooth/jni
com_android_bluetooth_btservice_AdapterService.cpp
static void acl_state_changed_callback(bt_status_t status, RawAddress* bd_addr,
bt_acl_state_t state) {
if (!bd_addr) {
ALOGE("Address is null in %s", __func__);
return;
}
ALOGE( "in %s", __func__ );
CallbackEnv sCallbackEnv(__func__);
if (!sCallbackEnv.valid()) return;
ScopedLocalRef<jbyteArray> addr(
sCallbackEnv.get(), sCallbackEnv->NewByteArray(sizeof(RawAddress)));
if (!addr.get()) {
ALOGE("Address allocation failed in %s", __func__);
return;
}
sCallbackEnv->SetByteArrayRegion(addr.get(), 0, sizeof(RawAddress),
(jbyte*)bd_addr);
sCallbackEnv->CallVoidMethod(sJniCallbacksObj, method_aclStateChangeCallback,
(jint)status, addr.get(), (jint)state);
}
step.3:
static void classInitNative(JNIEnv* env, jclass clazz) {
jclass jniUidTrafficClass = env->FindClass("android/bluetooth/UidTraffic");
android_bluetooth_UidTraffic.constructor =
env->GetMethodID(jniUidTrafficClass, "<init>", "(IJJ)V");
jclass jniCallbackClass = env->FindClass("com/android/bluetooth/btservice/JniCallbacks");
sJniCallbacksField = env->GetFieldID(
clazz, "mJniCallbacks", "Lcom/android/bluetooth/btservice/JniCallbacks;");
method_stateChangeCallback =
env->GetMethodID(jniCallbackClass, "stateChangeCallback", "(I)V");
method_adapterPropertyChangedCallback = env->GetMethodID(
jniCallbackClass, "adapterPropertyChangedCallback", "([I[[B)V");
method_discoveryStateChangeCallback = env->GetMethodID(
jniCallbackClass, "discoveryStateChangeCallback", "(I)V");
method_devicePropertyChangedCallback = env->GetMethodID(
jniCallbackClass, "devicePropertyChangedCallback", "([B[I[[B)V");
method_deviceFoundCallback =
env->GetMethodID(jniCallbackClass, "deviceFoundCallback", "([B)V");
method_pinRequestCallback =
env->GetMethodID(jniCallbackClass, "pinRequestCallback", "([B[BIZ)V");
method_sspRequestCallback =
env->GetMethodID(jniCallbackClass, "sspRequestCallback", "([B[BIII)V");
//void sspRequestCallback(byte[] address, byte[] name, int cod, int pairingVariant, int passkey) {
method_bondStateChangeCallback =
env->GetMethodID(jniCallbackClass, "bondStateChangeCallback", "(I[BI)V");
method_aclStateChangeCallback =
env->GetMethodID(jniCallbackClass, "aclStateChangeCallback", "(I[BI)V");
method_setWakeAlarm = env->GetMethodID(clazz, "setWakeAlarm", "(JZ)Z");
method_acquireWakeLock =
env->GetMethodID(clazz, "acquireWakeLock", "(Ljava/lang/String;)Z");
method_releaseWakeLock =
env->GetMethodID(clazz, "releaseWakeLock", "(Ljava/lang/String;)Z");
method_energyInfo = env->GetMethodID(
clazz, "energyInfoCallback", "(IIJJJJ[Landroid/bluetooth/UidTraffic;)V");
if (hal_util_load_bt_library((bt_interface_t const**)&sBluetoothInterface)) {
ALOGE("No Bluetooth Library found");
}
}
step.4:
JniCallbacks.java
void aclStateChangeCallback(int status, byte[] address, int newState) {
mRemoteDevices.aclStateChangeCallback(status, address, newState);
}
step.5:
RemoteDevices.java
void aclStateChangeCallback(int status, byte[] address, int newState) {
BluetoothDevice device = getDevice(address);
if (device == null) {
errorLog("aclStateChangeCallback: device is NULL, address="
+ Utils.getAddressStringFromByte(address) + ", newState=" + newState + "\n\n\n");
return;
}
int state = sAdapterService.getState();
一些疑点捋一捋:
acl_state_changed_cb 怎么到下一步的?
static const bt_interface_t* sBluetoothInterface = NULL;
if (hal_util_load_bt_library((bt_interface_t const**)&sBluetoothInterface)) {
ALOGE("No Bluetooth Library found");
}
int hal_util_load_bt_library(const bt_interface_t** interface) {
const char* sym = BLUETOOTH_INTERFACE_STRING;
#define BLUETOOTH_INTERFACE_STRING "bluetoothInterface"
bt_interface_t* itf = nullptr;
// The library name is not set by default, so the preset library name is used.
char path[PROPERTY_VALUE_MAX] = "";
property_get(PROPERTY_BT_LIBRARY_NAME, path, DEFAULT_BT_LIBRARY_NAME);
void* handle = dlopen(path, RTLD_NOW);
#define DEFAULT_BT_LIBRARY_NAME "libbluetooth.so"
//打开蓝牙库
if (!handle) {
const char* err_str = dlerror();
LOG(ERROR) << __func__ << ": failed to load Bluetooth library, error="
<< (err_str ? err_str : "error unknown");
goto error;
}
// Get the address of the bt_interface_t.
itf = (bt_interface_t*)dlsym(handle, sym);
//加载其中的 bluetoothInterface
if (!itf) {
LOG(ERROR) << __func__ << ": failed to load symbol from Bluetooth library "
<< sym;
goto error;
}
// Success.
LOG(INFO) << __func__ << " loaded HAL: btinterface=" << itf
<< ", handle=" << handle;
*interface = itf;
return 0;
error:
*interface = NULL;
if (handle) dlclose(handle);
return -EINVAL;
}
bt/btif/src/Bluetooth.cc
EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
sizeof(bluetoothInterface),
init,
enable,
disable,
cleanup,
get_adapter_properties,
get_adapter_property,
set_adapter_property,
get_remote_device_properties,
get_remote_device_property,
set_remote_device_property,
get_remote_service_record,
get_remote_services,
start_discovery,
cancel_discovery,
create_bond,
create_bond_out_of_band,
remove_bond,
cancel_bond,
get_connection_state,
pin_reply,
ssp_reply,
get_profile_interface,
dut_mode_configure,
dut_mode_send,
le_test_mode,
set_os_callouts,
read_energy_info,
dump,
dumpMetrics,
config_clear,
interop_database_clear,
interop_database_add,
get_avrcp_service,
};
static bt_callbacks_t sBluetoothCallbacks = {
sizeof(sBluetoothCallbacks), adapter_state_change_callback,
adapter_properties_callback, remote_device_properties_callback,
device_found_callback, discovery_state_changed_callback,
pin_request_callback, ssp_request_callback,
bond_state_changed_callback, acl_state_changed_callback,
callback_thread_event, dut_mode_recv_callback,
le_test_mode_recv_callback, energy_info_recv_callback};
static bool initNative(JNIEnv* env, jobject obj) {
ALOGV("%s", __func__);
android_bluetooth_UidTraffic.clazz =
(jclass)env->NewGlobalRef(env->FindClass("android/bluetooth/UidTraffic"));
sJniAdapterServiceObj = env->NewGlobalRef(obj);
sJniCallbacksObj =
env->NewGlobalRef(env->GetObjectField(obj, sJniCallbacksField));
if (!sBluetoothInterface) {
return JNI_FALSE;
}
int ret = sBluetoothInterface->init(&sBluetoothCallbacks);
bt/btif/src/Bluetooth.cc
static int init(bt_callbacks_t* callbacks) {
// LOG_INFO(LOG_TAG, "%s", __func__);
if (interface_ready()) return BT_STATUS_DONE;
#ifdef BLUEDROID_DEBUG
allocation_tracker_init();
#endif
bt_hal_cbacks = callbacks;
stack_manager_get_interface()->init_stack();
btif_debug_init();
return BT_STATUS_SUCCESS;
}
HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
&bd_addr, BT_ACL_STATE_CONNECTED);
终于将上下联系起来了!