From f685d8953b291c60f7f04f03912d8b57a442584b Mon Sep 17 00:00:00 2001 From: Jaehong Jo Date: Tue, 5 Apr 2016 20:15:59 +0900 Subject: [PATCH] Fix issuse related unregisterReceiver In EDR, BLE, NFC Adapter Change-Id: I6102c96c1cad58e8124b75c848a498621d250e96 Signed-off-by: Jaehong Jo Reviewed-on: https://gerrit.iotivity.org/gerrit/7615 Tested-by: jenkins-iotivity Reviewed-by: Jon A. Cruz --- resource/csdk/connectivity/inc/caadapterutils.h | 12 +++ .../connectivity/src/adapter_util/caadapterutils.c | 26 +++++ .../src/bt_edr_adapter/android/caedrclient.c | 21 ++-- .../src/bt_le_adapter/android/caleclient.c | 120 ++++++++++++--------- .../src/ip_adapter/android/caipnwmonitor.c | 12 +-- .../src/nfc_adapter/android/canfcserver.c | 20 +++- 6 files changed, 140 insertions(+), 71 deletions(-) diff --git a/resource/csdk/connectivity/inc/caadapterutils.h b/resource/csdk/connectivity/inc/caadapterutils.h index aec4ea7..cd3f102 100644 --- a/resource/csdk/connectivity/inc/caadapterutils.h +++ b/resource/csdk/connectivity/inc/caadapterutils.h @@ -255,6 +255,18 @@ void CANativeSetActivity(JNIEnv *env, jobject activity); jobject *CANativeGetActivity(); /** + * get method ID for method Name and class + * @param[in] env JNI interface pointer. + * @param[in] className android class. + * @param[in] methodName android method name. + * @param[in] methodFormat method type of methodName. + * @return jmethodID iD of the method. + */ +jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className, + const char* methodName, + const char* methodFormat); + +/** * To Delete other Global References * Called during CATerminate to remove global references */ diff --git a/resource/csdk/connectivity/src/adapter_util/caadapterutils.c b/resource/csdk/connectivity/src/adapter_util/caadapterutils.c index bb79375..a26fc4a 100644 --- a/resource/csdk/connectivity/src/adapter_util/caadapterutils.c +++ b/resource/csdk/connectivity/src/adapter_util/caadapterutils.c @@ -255,6 +255,32 @@ jobject *CANativeGetActivity() return g_Activity; } +jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className, + const char* methodName, + const char* methodFormat) +{ + VERIFY_NON_NULL_RET(env, CA_ADAPTER_UTILS_TAG, "env", NULL); + VERIFY_NON_NULL_RET(className, CA_ADAPTER_UTILS_TAG, "className", NULL); + VERIFY_NON_NULL_RET(methodName, CA_ADAPTER_UTILS_TAG, "methodName", NULL); + VERIFY_NON_NULL_RET(methodFormat, CA_ADAPTER_UTILS_TAG, "methodFormat", NULL); + + jclass jni_cid = (*env)->FindClass(env, className); + if (!jni_cid) + { + OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_cid [%s] is null", className); + return NULL; + } + + jmethodID jni_midID = (*env)->GetMethodID(env, jni_cid, methodName, methodFormat); + if (!jni_midID) + { + OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_midID [%s] is null", methodName); + return NULL; + } + + return jni_midID; +} + void CADeleteGlobalReferences(JNIEnv *env) { if (g_Context) diff --git a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.c b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.c index adc3e79..c741f0a 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.c +++ b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.c @@ -258,20 +258,21 @@ CAResult_t CAEDRCreateJNIInterfaceObject(jobject context) return CA_STATUS_FAILED; } - //getApplicationContext - jclass contextClass = (*env)->FindClass(env, CLASSPATH_CONTEXT); - if (!contextClass) + + jmethodID mid_getApplicationContext = CAGetJNIMethodID(env, CLASSPATH_CONTEXT, + "getApplicationContext", + METHODID_CONTEXTNONPARAM); + if (!mid_getApplicationContext) { - OIC_LOG(ERROR, TAG, "Could not get context object class"); + OIC_LOG(ERROR, TAG, "Could not get getApplicationContext method"); return CA_STATUS_FAILED; } - jmethodID getApplicationContextMethod = (*env)->GetMethodID(env, contextClass, - "getApplicationContext", - METHODID_CONTEXTNONPARAM); - if (!getApplicationContextMethod) + jobject jApplicationContext = (*env)->CallObjectMethod(env, context, + mid_getApplicationContext); + if (!jApplicationContext) { - OIC_LOG(ERROR, TAG, "Could not get getApplicationContext method"); + OIC_LOG(ERROR, TAG, "Could not get application context"); return CA_STATUS_FAILED; } @@ -291,7 +292,7 @@ CAResult_t CAEDRCreateJNIInterfaceObject(jobject context) return CA_STATUS_FAILED; } - (*env)->NewObject(env, EDRJniInterface, EDRInterfaceConstructorMethod, context); + (*env)->NewObject(env, EDRJniInterface, EDRInterfaceConstructorMethod, jApplicationContext); OIC_LOG(DEBUG, TAG, "NewObject Success"); return CA_STATUS_OK; diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c index 9623793..f463d38 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -169,6 +169,24 @@ CAResult_t CALECreateJniInterfaceObject() isAttached = true; } + jmethodID mid_getApplicationContext = CAGetJNIMethodID(env, "android/content/Context", + "getApplicationContext", + "()Landroid/content/Context;"); + + if (!mid_getApplicationContext) + { + OIC_LOG(ERROR, TAG, "Could not get getApplicationContext method"); + return CA_STATUS_FAILED; + } + + jobject jApplicationContext = (*env)->CallObjectMethod(env, g_context, + mid_getApplicationContext); + if (!jApplicationContext) + { + OIC_LOG(ERROR, TAG, "Could not get application context"); + return CA_STATUS_FAILED; + } + jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface"); if (!jni_LEInterface) { @@ -184,7 +202,7 @@ CAResult_t CALECreateJniInterfaceObject() goto error_exit; } - (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, g_context); + (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, jApplicationContext); OIC_LOG(DEBUG, TAG, "Create instance for CaLeClientInterface"); if (isAttached) @@ -1101,8 +1119,8 @@ jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt) VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL); VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL); - jmethodID jni_mid_getDevice = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, "getDevice", - "()Landroid/bluetooth/BluetoothDevice;"); + jmethodID jni_mid_getDevice = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, "getDevice", + "()Landroid/bluetooth/BluetoothDevice;"); if (!jni_mid_getDevice) { OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null"); @@ -1138,7 +1156,7 @@ CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt) // get BluetoothGatt method OIC_LOG(DEBUG, TAG, "get BluetoothGatt method"); - jmethodID jni_mid_closeGatt = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, "close", "()V"); + jmethodID jni_mid_closeGatt = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, "close", "()V"); if (!jni_mid_closeGatt) { OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null"); @@ -1668,11 +1686,11 @@ jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean aut // get BluetoothDevice method OIC_LOG(DEBUG, TAG, "get BluetoothDevice method"); - jmethodID jni_mid_connectGatt = CALEGetJNIMethodID(env, "android/bluetooth/BluetoothDevice", - "connectGatt", - "(Landroid/content/Context;ZLandroid/" - "bluetooth/BluetoothGattCallback;)" - "Landroid/bluetooth/BluetoothGatt;"); + jmethodID jni_mid_connectGatt = CAGetJNIMethodID(env, "android/bluetooth/BluetoothDevice", + "connectGatt", + "(Landroid/content/Context;ZLandroid/" + "bluetooth/BluetoothGattCallback;)" + "Landroid/bluetooth/BluetoothGatt;"); if (!jni_mid_connectGatt) { OIC_LOG(ERROR, TAG, "bleConnect: jni_mid_connectGatt is null"); @@ -1783,8 +1801,8 @@ CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt) // get BluetoothGatt method OIC_LOG(DEBUG, TAG, "get gatt disconnect method"); - jmethodID jni_mid_disconnectGatt = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, - "disconnect", "()V"); + jmethodID jni_mid_disconnectGatt = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, + "disconnect", "()V"); if (!jni_mid_disconnectGatt) { OIC_LOG(ERROR, TAG, "jni_mid_disconnectGatt is null"); @@ -1920,8 +1938,8 @@ CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt) // get BluetoothGatt.discoverServices method OIC_LOG(DEBUG, TAG, "get BluetoothGatt.discoverServices method"); - jmethodID jni_mid_discoverServices = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, - "discoverServices", "()Z"); + jmethodID jni_mid_discoverServices = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, + "discoverServices", "()Z"); if (!jni_mid_discoverServices) { OIC_LOG(ERROR, TAG, "jni_mid_discoverServices is null"); @@ -2051,10 +2069,10 @@ CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt, // get BluetoothGatt.write characteristic method OIC_LOG(DEBUG, TAG, "write characteristic method"); - jmethodID jni_mid_writeCharacteristic = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, - "writeCharacteristic", - "(Landroid/bluetooth/" - "BluetoothGattCharacteristic;)Z"); + jmethodID jni_mid_writeCharacteristic = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, + "writeCharacteristic", + "(Landroid/bluetooth/" + "BluetoothGattCharacteristic;)Z"); if (!jni_mid_writeCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_writeCharacteristic is null"); @@ -2105,10 +2123,10 @@ CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt) } OIC_LOG(DEBUG, TAG, "read characteristic method"); - jmethodID jni_mid_readCharacteristic = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, - "readCharacteristic", - "(Landroid/bluetooth/" - "BluetoothGattCharacteristic;)Z"); + jmethodID jni_mid_readCharacteristic = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, + "readCharacteristic", + "(Landroid/bluetooth/" + "BluetoothGattCharacteristic;)Z"); if (!jni_mid_readCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_readCharacteristic is null"); @@ -2147,10 +2165,10 @@ CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoot // get BluetoothGatt.setCharacteristicNotification method OIC_LOG(DEBUG, TAG, "CALEClientSetCharacteristicNotification"); - jmethodID jni_mid_setNotification = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, - "setCharacteristicNotification", - "(Landroid/bluetooth/" - "BluetoothGattCharacteristic;Z)Z"); + jmethodID jni_mid_setNotification = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, + "setCharacteristicNotification", + "(Landroid/bluetooth/" + "BluetoothGattCharacteristic;Z)Z"); if (!jni_mid_setNotification) { OIC_LOG(ERROR, TAG, "jni_mid_getService is null"); @@ -2186,10 +2204,10 @@ jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring cha // get BluetoothGatt.getService method OIC_LOG(DEBUG, TAG, "BluetoothGatt.getService"); - jmethodID jni_mid_getService = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, - "getService", - "(Ljava/util/UUID;)Landroid/bluetooth/" - "BluetoothGattService;"); + jmethodID jni_mid_getService = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, + "getService", + "(Ljava/util/UUID;)Landroid/bluetooth/" + "BluetoothGattService;"); if (!jni_mid_getService) { OIC_LOG(ERROR, TAG, "jni_mid_getService is null"); @@ -2214,12 +2232,12 @@ jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring cha } // get bluetooth gatt service method - jmethodID jni_mid_getCharacteristic = CALEGetJNIMethodID(env, "android/bluetooth/" - "BluetoothGattService", - "getCharacteristic", - "(Ljava/util/UUID;)" - "Landroid/bluetooth/" - "BluetoothGattCharacteristic;"); + jmethodID jni_mid_getCharacteristic = CAGetJNIMethodID(env, "android/bluetooth/" + "BluetoothGattService", + "getCharacteristic", + "(Ljava/util/UUID;)" + "Landroid/bluetooth/" + "BluetoothGattCharacteristic;"); if (!jni_mid_getCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null"); @@ -2342,9 +2360,9 @@ jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteris return NULL; } - jmethodID jni_mid_getValue = CALEGetJNIMethodID(env, "android/bluetooth/" - "BluetoothGattCharacteristic", - "getValue", "()[B"); + jmethodID jni_mid_getValue = CAGetJNIMethodID(env, "android/bluetooth/" + "BluetoothGattCharacteristic", + "getValue", "()[B"); if (!jni_mid_getValue) { OIC_LOG(ERROR, TAG, "jni_mid_getValue is null"); @@ -2438,11 +2456,11 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, } OIC_LOG(DEBUG, TAG, "CALEClientSetUUIDToDescriptor"); - jmethodID jni_mid_getDescriptor = CALEGetJNIMethodID(env, "android/bluetooth/" - "BluetoothGattCharacteristic", - "getDescriptor", - "(Ljava/util/UUID;)Landroid/bluetooth/" - "BluetoothGattDescriptor;"); + jmethodID jni_mid_getDescriptor = CAGetJNIMethodID(env, "android/bluetooth/" + "BluetoothGattCharacteristic", + "getDescriptor", + "(Ljava/util/UUID;)Landroid/bluetooth/" + "BluetoothGattDescriptor;"); if (!jni_mid_getDescriptor) { OIC_LOG(ERROR, TAG, "jni_mid_getDescriptor is null"); @@ -2504,10 +2522,10 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, return CA_STATUS_FAILED; } - jmethodID jni_mid_writeDescriptor = CALEGetJNIMethodID(env, "android/bluetooth/BluetoothGatt", - "writeDescriptor", - "(Landroid/bluetooth/" - "BluetoothGattDescriptor;)Z"); + jmethodID jni_mid_writeDescriptor = CAGetJNIMethodID(env, "android/bluetooth/BluetoothGatt", + "writeDescriptor", + "(Landroid/bluetooth/" + "BluetoothGattDescriptor;)Z"); if (!jni_mid_writeDescriptor) { OIC_LOG(ERROR, TAG, "jni_mid_writeDescriptor is null"); @@ -3119,8 +3137,8 @@ jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice) } // get method ID of getDevice() - jmethodID jni_mid_getDevice = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, - "getDevice", METHODID_BT_DEVICE); + jmethodID jni_mid_getDevice = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, + "getDevice", METHODID_BT_DEVICE); if (!jni_mid_getDevice) { OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null"); @@ -3952,8 +3970,8 @@ static jstring CALEClientGetAddressFromGatt(JNIEnv *env, jobject gatt) VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL); VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL); - jmethodID jni_mid_getDevice = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, - "getDevice", METHODID_BT_DEVICE); + jmethodID jni_mid_getDevice = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, + "getDevice", METHODID_BT_DEVICE); if (!jni_mid_getDevice) { OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null"); diff --git a/resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c b/resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c index 3598110..2685880 100644 --- a/resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c +++ b/resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c @@ -321,16 +321,10 @@ CAResult_t CAIPJniInit() return CA_STATUS_FAILED; } - jclass cls_Context = (*env)->FindClass(env, "android/content/Context"); - if (!cls_Context) - { - OIC_LOG(ERROR, TAG, "Could not get context object class"); - return CA_STATUS_FAILED; - } + jmethodID mid_getApplicationContext = CAGetJNIMethodID(env, "android/content/Context", + "getApplicationContext", + "()Landroid/content/Context;"); - jmethodID mid_getApplicationContext = (*env)->GetMethodID(env, cls_Context, - "getApplicationContext", - "()Landroid/content/Context;"); if (!mid_getApplicationContext) { OIC_LOG(ERROR, TAG, "Could not get getApplicationContext method"); diff --git a/resource/csdk/connectivity/src/nfc_adapter/android/canfcserver.c b/resource/csdk/connectivity/src/nfc_adapter/android/canfcserver.c index aa23348..4718c5c 100644 --- a/resource/csdk/connectivity/src/nfc_adapter/android/canfcserver.c +++ b/resource/csdk/connectivity/src/nfc_adapter/android/canfcserver.c @@ -148,6 +148,24 @@ CAResult_t CANfcCreateJniInterfaceObject() isAttached = true; } + jmethodID mid_getApplicationContext = CAGetJNIMethodID(env, "android/content/Context", + "getApplicationContext", + "()Landroid/content/Context;"); + + if (!mid_getApplicationContext) + { + OIC_LOG(ERROR, TAG, "Could not get getApplicationContext method"); + return CA_STATUS_FAILED; + } + + jobject jApplicationContext = (*env)->CallObjectMethod(env, g_context, + mid_getApplicationContext); + if (!jApplicationContext) + { + OIC_LOG(ERROR, TAG, "Could not get application context"); + return CA_STATUS_FAILED; + } + jclass jni_NfcInterface = (*env)->FindClass(env, "org/iotivity/ca/CaNfcInterface"); if (!jni_NfcInterface) { @@ -164,7 +182,7 @@ CAResult_t CANfcCreateJniInterfaceObject() } jobject jni_nfcInstance = (*env)->NewObject(env, jni_NfcInterface, - NfcInterfaceConstructorMethod, g_context, + NfcInterfaceConstructorMethod, jApplicationContext, g_activity); if (!jni_nfcInstance) { -- 2.7.4