From 39cd9a6c0c16b33fc55b7af604d44362f0499cda Mon Sep 17 00:00:00 2001 From: "jihwan.seo" Date: Fri, 25 Mar 2016 16:34:37 +0900 Subject: [PATCH] clean up getting methodID from jni Change-Id: Ic6852d4f6195356cd35c1c4cabe3d3438ecbcd26 Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/6321 Tested-by: jenkins-iotivity Reviewed-by: Jon A. Cruz --- .../src/bt_le_adapter/android/caleclient.c | 225 ++++++-------------- .../src/bt_le_adapter/android/caleserver.c | 228 ++++++--------------- .../src/bt_le_adapter/android/caleutils.c | 61 +++--- .../src/bt_le_adapter/android/caleutils.h | 13 ++ 4 files changed, 172 insertions(+), 355 deletions(-) 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 0594fc1..92f78f1 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -1146,15 +1146,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); - jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_gattdevice_list) - { - OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null"); - return NULL; - } - - jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice", - "()Landroid/bluetooth/BluetoothDevice;"); + jmethodID jni_mid_getDevice = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, "getDevice", + "()Landroid/bluetooth/BluetoothDevice;"); if (!jni_mid_getDevice) { OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null"); @@ -1188,16 +1181,9 @@ CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt) VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null"); VERIFY_NON_NULL(env, TAG, "env is null"); - // get BluetoothGatt class - OIC_LOG(DEBUG, TAG, "get BluetoothGatt class"); - jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_BluetoothGatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null"); - return CA_STATUS_FAILED; - } - - jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V"); + // get BluetoothGatt method + OIC_LOG(DEBUG, TAG, "get BluetoothGatt method"); + jmethodID jni_mid_closeGatt = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, "close", "()V"); if (!jni_mid_closeGatt) { OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null"); @@ -1727,21 +1713,13 @@ jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean aut return NULL; } - // get BluetoothDevice class - OIC_LOG(DEBUG, TAG, "get BluetoothDevice class"); - jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice"); - if (!jni_cid_BluetoothDevice) - { - OIC_LOG(ERROR, TAG, "bleConnect: jni_cid_BluetoothDevice is null"); - return NULL; - } - - // get connectGatt method - OIC_LOG(DEBUG, TAG, "get connectGatt method"); - jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice, "connectGatt", - "(Landroid/content/Context;ZLandroid/" - "bluetooth/BluetoothGattCallback;)" - "Landroid/bluetooth/BluetoothGatt;"); + // 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;"); if (!jni_mid_connectGatt) { OIC_LOG(ERROR, TAG, "bleConnect: jni_mid_connectGatt is null"); @@ -1850,16 +1828,9 @@ CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt) VERIFY_NON_NULL(env, TAG, "env is null"); VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null"); - // get BluetoothGatt class - jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_BluetoothGatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null"); - return CA_STATUS_FAILED; - } - + // get BluetoothGatt method OIC_LOG(DEBUG, TAG, "get gatt disconnect method"); - jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, + jmethodID jni_mid_disconnectGatt = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, "disconnect", "()V"); if (!jni_mid_disconnectGatt) { @@ -1994,23 +1965,16 @@ CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt) return CA_ADAPTER_NOT_ENABLED; } - // get BluetoothGatt class - OIC_LOG(DEBUG, TAG, "get BluetoothGatt class"); - jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_BluetoothGatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null"); - return CA_STATUS_FAILED; - } - - OIC_LOG(DEBUG, TAG, "discovery gatt services method"); - jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, - "discoverServices", "()Z"); + // get BluetoothGatt.discoverServices method + OIC_LOG(DEBUG, TAG, "get BluetoothGatt.discoverServices method"); + jmethodID jni_mid_discoverServices = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, + "discoverServices", "()Z"); if (!jni_mid_discoverServices) { OIC_LOG(ERROR, TAG, "jni_mid_discoverServices is null"); return CA_STATUS_FAILED; } + // call disconnect gatt method OIC_LOG(DEBUG, TAG, "CALL API - request discovery gatt services"); jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices); @@ -2132,20 +2096,12 @@ CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt, return CA_STATUS_FAILED; } - // get BluetoothGatt class - OIC_LOG(DEBUG, TAG, "get BluetoothGatt class"); - jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_BluetoothGatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null"); - return CA_STATUS_FAILED; - } - + // get BluetoothGatt.write characteristic method OIC_LOG(DEBUG, TAG, "write characteristic method"); - jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, - "writeCharacteristic", - "(Landroid/bluetooth/" - "BluetoothGattCharacteristic;)Z"); + jmethodID jni_mid_writeCharacteristic = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, + "writeCharacteristic", + "(Landroid/bluetooth/" + "BluetoothGattCharacteristic;)Z"); if (!jni_mid_writeCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_writeCharacteristic is null"); @@ -2181,13 +2137,6 @@ CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt) return CA_STATUS_FAILED; } - jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_BluetoothGatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null"); - return CA_STATUS_FAILED; - } - jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID); if (!jni_uuid) { @@ -2203,10 +2152,10 @@ CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt) } OIC_LOG(DEBUG, TAG, "read characteristic method"); - jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, - "readCharacteristic", - "(Landroid/bluetooth/" - "BluetoothGattCharacteristic;)Z"); + jmethodID jni_mid_readCharacteristic = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, + "readCharacteristic", + "(Landroid/bluetooth/" + "BluetoothGattCharacteristic;)Z"); if (!jni_mid_readCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_readCharacteristic is null"); @@ -2243,20 +2192,12 @@ CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoot return CA_ADAPTER_NOT_ENABLED; } - // get BluetoothGatt class + // get BluetoothGatt.setCharacteristicNotification method OIC_LOG(DEBUG, TAG, "CALEClientSetCharacteristicNotification"); - jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_BluetoothGatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null"); - return CA_STATUS_FAILED; - } - - // set Characteristic Notification - jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, - "setCharacteristicNotification", - "(Landroid/bluetooth/" - "BluetoothGattCharacteristic;Z)Z"); + jmethodID jni_mid_setNotification = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, + "setCharacteristicNotification", + "(Landroid/bluetooth/" + "BluetoothGattCharacteristic;Z)Z"); if (!jni_mid_setNotification) { OIC_LOG(ERROR, TAG, "jni_mid_getService is null"); @@ -2290,18 +2231,12 @@ jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring cha return NULL; } - // get BluetoothGatt class - OIC_LOG(DEBUG, TAG, "CALEClientGetGattService"); - jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_BluetoothGatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null"); - return NULL; - } - - jmethodID jni_mid_getService = (*env)->GetMethodID( - env, jni_cid_BluetoothGatt, "getService", - "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;"); + // 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;"); if (!jni_mid_getService) { OIC_LOG(ERROR, TAG, "jni_mid_getService is null"); @@ -2325,21 +2260,13 @@ jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring cha return NULL; } - // get bluetooth gatt service class - jclass jni_cid_BluetoothGattService = (*env)->FindClass( - env, "android/bluetooth/BluetoothGattService"); - if (!jni_cid_BluetoothGattService) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGattService is null"); - return NULL; - } - - OIC_LOG(DEBUG, TAG, "get gatt getCharacteristic method"); - jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService, - "getCharacteristic", - "(Ljava/util/UUID;)" - "Landroid/bluetooth/" - "BluetoothGattCharacteristic;"); + // get bluetooth gatt service method + jmethodID jni_mid_getCharacteristic = CALEGetJNIMethodID(env, "android/bluetooth/" + "BluetoothGattService", + "getCharacteristic", + "(Ljava/util/UUID;)" + "Landroid/bluetooth/" + "BluetoothGattCharacteristic;"); if (!jni_mid_getCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null"); @@ -2462,17 +2389,9 @@ jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteris return NULL; } - jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/" - "BluetoothGattCharacteristic"); - if (!jni_cid_BTGattCharacteristic) - { - OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null"); - return NULL; - } - - OIC_LOG(DEBUG, TAG, "get value in Characteristic"); - jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue", - "()[B"); + jmethodID jni_mid_getValue = CALEGetJNIMethodID(env, "android/bluetooth/" + "BluetoothGattCharacteristic", + "getValue", "()[B"); if (!jni_mid_getValue) { OIC_LOG(ERROR, TAG, "jni_mid_getValue is null"); @@ -2566,16 +2485,8 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, } OIC_LOG(DEBUG, TAG, "CALEClientSetUUIDToDescriptor"); - jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/" - "BluetoothGattCharacteristic"); - if (!jni_cid_BTGattCharacteristic) - { - OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null"); - return CA_STATUS_FAILED; - } - - OIC_LOG(DEBUG, TAG, "set value in Characteristic"); - jmethodID jni_mid_getDescriptor = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, + jmethodID jni_mid_getDescriptor = CALEGetJNIMethodID(env, "android/bluetooth/" + "BluetoothGattCharacteristic", "getDescriptor", "(Ljava/util/UUID;)Landroid/bluetooth/" "BluetoothGattDescriptor;"); @@ -2640,15 +2551,8 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, return CA_STATUS_FAILED; } - jclass jni_cid_gatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt"); - if (!jni_cid_gatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_gatt is null"); - return CA_STATUS_FAILED; - } - - OIC_LOG(DEBUG, TAG, "write Descriptor in gatt object"); - jmethodID jni_mid_writeDescriptor = (*env)->GetMethodID(env, jni_cid_gatt, "writeDescriptor", + jmethodID jni_mid_writeDescriptor = CALEGetJNIMethodID(env, "android/bluetooth/BluetoothGatt", + "writeDescriptor", "(Landroid/bluetooth/" "BluetoothGattDescriptor;)Z"); if (!jni_mid_writeDescriptor) @@ -3255,16 +3159,8 @@ jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice) } // get method ID of getDevice() - jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_gattdevice_list) - { - OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null"); - (*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress); - return NULL; - } - - jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice", - METHODID_BT_DEVICE); + jmethodID jni_mid_getDevice = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, + "getDevice", METHODID_BT_DEVICE); if (!jni_mid_getDevice) { OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null"); @@ -4103,15 +3999,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); - jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT); - if (!jni_cid_gattdevice_list) - { - OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null"); - return NULL; - } - - jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice", - METHODID_BT_DEVICE); + jmethodID jni_mid_getDevice = CALEGetJNIMethodID(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/bt_le_adapter/android/caleserver.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c index f3053b0..5699eb0 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c @@ -68,6 +68,7 @@ static ca_cond g_threadSendNotifyCond = NULL; static bool g_isSignalSetFlag = false; static const char CLASSPATH_BT_ADVERTISE_CB[] = "android/bluetooth/le/AdvertiseCallback"; +static const char CLASSPATH_BT_GATTSERVER[] = "android/bluetooth/BluetoothGattServer"; void CALEServerJNISetContext() { @@ -161,17 +162,10 @@ static jint CALEServerGetConnectionState(JNIEnv *env, jobject device) VERIFY_NON_NULL_RET(env, TAG, "env", -1); VERIFY_NON_NULL_RET(device, TAG, "device", -1); - jclass jni_cid_bluetoothManager = (*env)->FindClass(env, "android/bluetooth/BluetoothManager"); - if (!jni_cid_bluetoothManager) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothManager is null"); - return -1; - } - - jmethodID jni_mid_getConnectionState = (*env)->GetMethodID(env, jni_cid_bluetoothManager, - "getConnectionState", - "(Landroid/bluetooth/BluetoothDevice" - ";I)I"); + jmethodID jni_mid_getConnectionState = CALEGetJNIMethodID(env, "android/bluetooth/BluetoothManager", + "getConnectionState", + "(Landroid/bluetooth/BluetoothDevice" + ";I)I"); if (!jni_mid_getConnectionState) { OIC_LOG(ERROR, TAG, "jni_mid_getConnectionState is null"); @@ -210,35 +204,10 @@ jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData) } OIC_LOG(DEBUG, TAG, "CALEServerSetResponseData"); - - jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env, - "android/bluetooth/BluetoothGattServer"); - if (!jni_cid_bluetoothGattServer) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null"); - return NULL; - } - - jclass jni_cid_bluetoothGattService = (*env)->FindClass(env, "android/bluetooth/" - "BluetoothGattService"); - if (!jni_cid_bluetoothGattService) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattService is null"); - return NULL; - } - - jclass jni_cid_bluetoothGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/" - "BluetoothGattCharacteristic"); - if (!jni_cid_bluetoothGattCharacteristic) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattCharacteristic is null"); - return NULL; - } - - jmethodID jni_mid_getService = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer, - "getService", - "(Ljava/util/UUID;)Landroid/bluetooth/" - "BluetoothGattService;"); + jmethodID jni_mid_getService = CALEGetJNIMethodID(env, CLASSPATH_BT_GATTSERVER, + "getService", + "(Ljava/util/UUID;)Landroid/bluetooth/" + "BluetoothGattService;"); if (!jni_mid_getService) { OIC_LOG(ERROR, TAG, "jni_mid_getService is null"); @@ -261,11 +230,12 @@ jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData) return NULL; } - jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_bluetoothGattService, - "getCharacteristic", - "(Ljava/util/UUID;)" - "Landroid/bluetooth/" - "BluetoothGattCharacteristic;"); + jmethodID jni_mid_getCharacteristic = CALEGetJNIMethodID(env, "android/bluetooth/" + "BluetoothGattService", + "getCharacteristic", + "(Ljava/util/UUID;)" + "Landroid/bluetooth/" + "BluetoothGattCharacteristic;"); if (!jni_mid_getCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null"); @@ -288,8 +258,9 @@ jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData) return NULL; } - jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_bluetoothGattCharacteristic, - "setValue", "([B)Z"); + jmethodID jni_mid_setValue = CALEGetJNIMethodID(env, "android/bluetooth/" + "BluetoothGattCharacteristic", + "setValue", "([B)Z"); if (!jni_mid_setValue) { OIC_LOG(ERROR, TAG, "jni_mid_setValue is null"); @@ -327,18 +298,12 @@ CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject respo return CA_STATUS_FAILED; } - jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env, - "android/bluetooth/BluetoothGattServer"); - if (!jni_cid_bluetoothGattServer) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null"); - return CA_STATUS_FAILED; - } - - jmethodID jni_mid_notifyCharacteristicChanged = (*env)->GetMethodID( - env, jni_cid_bluetoothGattServer, "notifyCharacteristicChanged", - "(Landroid/bluetooth/BluetoothDevice;" - "Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z"); + jmethodID jni_mid_notifyCharacteristicChanged = CALEGetJNIMethodID(env, + CLASSPATH_BT_GATTSERVER, + "notifyCharacteristicChanged", + "(Landroid/bluetooth/BluetoothDevice;" + "Landroid/bluetooth/" + "BluetoothGattCharacteristic;Z)Z"); if (!jni_mid_notifyCharacteristicChanged) { OIC_LOG(ERROR, TAG, "jni_mid_notifyCharacteristicChanged is null"); @@ -390,18 +355,10 @@ CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, j return CA_ADAPTER_NOT_ENABLED; } - jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env, - "android/bluetooth/BluetoothGattServer"); - if (!jni_cid_bluetoothGattServer) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null"); - return CA_STATUS_FAILED; - } - - jmethodID jni_mid_sendResponse = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer, - "sendResponse", - "(Landroid/bluetooth/BluetoothDevice;" - "III[B)Z"); + jmethodID jni_mid_sendResponse = CALEGetJNIMethodID(env, CLASSPATH_BT_GATTSERVER, + "sendResponse", + "(Landroid/bluetooth/BluetoothDevice;" + "III[B)Z"); if (!jni_mid_sendResponse) { OIC_LOG(ERROR, TAG, "jni_mid_sendResponse is null"); @@ -712,24 +669,17 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) return CA_STATUS_FAILED; } - jclass jni_cid_leAdvertiser = (*env)->FindClass(env, - "android/bluetooth/le/BluetoothLeAdvertiser"); - if (!jni_cid_leAdvertiser) - { - OIC_LOG(ERROR, TAG, "jni_cid_leAdvertiser is null"); - return CA_STATUS_FAILED; - } - - jmethodID jni_mid_startAdvertising = (*env)->GetMethodID(env, jni_cid_leAdvertiser, - "startAdvertising", - "(Landroid/bluetooth/le/" - "AdvertiseSettings;Landroid/bluetooth/" - "le/AdvertiseData;Landroid/bluetooth/" - "le/AdvertiseCallback;)V"); + jmethodID jni_mid_startAdvertising = CALEGetJNIMethodID(env, "android/bluetooth/le/" + "BluetoothLeAdvertiser", + "startAdvertising", + "(Landroid/bluetooth/le/" + "AdvertiseSettings;Landroid/bluetooth/" + "le/AdvertiseData;Landroid/bluetooth/" + "le/AdvertiseCallback;)V"); if (!jni_mid_startAdvertising) { - OIC_LOG(ERROR, TAG, "jni_mid_startAdvertising is null"); - return CA_STATUS_FAILED; + OIC_LOG(ERROR, TAG, "jni_mid_startAdvertising is null"); + return CA_STATUS_FAILED; } (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser, jni_mid_startAdvertising, @@ -769,14 +719,6 @@ CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) return CA_STATUS_FAILED; } - jclass jni_cid_leAdvertiser = (*env)->FindClass(env, - "android/bluetooth/le/BluetoothLeAdvertiser"); - if (!jni_cid_leAdvertiser) - { - OIC_LOG(ERROR, TAG, "jni_cid_leAdvertiser is null"); - return CA_STATUS_FAILED; - } - jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", "()Landroid/bluetooth/" @@ -797,10 +739,11 @@ CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) return CA_STATUS_FAILED; } - jmethodID jni_mid_stopAdvertising = (*env)->GetMethodID(env, jni_cid_leAdvertiser, - "stopAdvertising", - "(Landroid/bluetooth/le/" - "AdvertiseCallback;)V"); + jmethodID jni_mid_stopAdvertising = CALEGetJNIMethodID(env, "android/bluetooth/le/" + "BluetoothLeAdvertiser", + "stopAdvertising", + "(Landroid/bluetooth/le/" + "AdvertiseCallback;)V"); if (!jni_mid_stopAdvertising) { OIC_LOG(ERROR, TAG, "jni_mid_stopAdvertising is null"); @@ -907,13 +850,6 @@ jobject CALEServerOpenGattServer(JNIEnv *env) return NULL; } - jclass jni_cid_bluetoothManager = (*env)->FindClass(env, "android/bluetooth/BluetoothManager"); - if (!jni_cid_bluetoothManager) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothManager is null"); - return NULL; - } - jfieldID jni_fid_bluetoothService = (*env)->GetStaticFieldID(env, jni_cid_context, "BLUETOOTH_SERVICE", "Ljava/lang/String;"); @@ -933,13 +869,14 @@ jobject CALEServerOpenGattServer(JNIEnv *env) return NULL; } - jmethodID jni_mid_openGattServer = (*env)->GetMethodID(env, jni_cid_bluetoothManager, - "openGattServer", - "(Landroid/content/Context;" - "Landroid/bluetooth/" - "BluetoothGattServerCallback;)" - "Landroid/bluetooth/" - "BluetoothGattServer;"); + jmethodID jni_mid_openGattServer = CALEGetJNIMethodID(env, "android/bluetooth/" + "BluetoothManager", + "openGattServer", + "(Landroid/content/Context;" + "Landroid/bluetooth/" + "BluetoothGattServerCallback;)" + "Landroid/bluetooth/" + "BluetoothGattServer;"); if (!jni_mid_openGattServer) { OIC_LOG(ERROR, TAG, "jni_mid_openGattServer is null"); @@ -1270,23 +1207,15 @@ CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer, return CA_ADAPTER_NOT_ENABLED; } - jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env, - "android/bluetooth/BluetoothGattServer"); - if (!jni_cid_bluetoothGattServer) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null"); - return CA_STATUS_FAILED; - } - - jmethodID jni_mid_addService = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer, - "addService", - "(Landroid/bluetooth/BluetoothGattService;)" - "Z"); - if (!jni_mid_addService) - { - OIC_LOG(ERROR, TAG, "jni_mid_addService is null"); - return CA_STATUS_FAILED; - } + jmethodID jni_mid_addService = CALEGetJNIMethodID(env, CLASSPATH_BT_GATTSERVER, + "addService", + "(Landroid/bluetooth/BluetoothGattService;)" + "Z"); + if (!jni_mid_addService) + { + OIC_LOG(ERROR, TAG, "jni_mid_addService is null"); + return CA_STATUS_FAILED; + } jboolean jni_boolean_addService = (*env)->CallBooleanMethod(env, bluetoothGattServer, jni_mid_addService, @@ -1314,16 +1243,9 @@ CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice) return CA_ADAPTER_NOT_ENABLED; } - jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env, - "android/bluetooth/BluetoothGattServer"); - if (!jni_cid_bluetoothGattServer) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null"); - return CA_STATUS_FAILED; - } - - jmethodID jni_mid_connect = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer, "connect", - "(Landroid/bluetooth/BluetoothDevice;Z)Z"); + jmethodID jni_mid_connect = CALEGetJNIMethodID(env, CLASSPATH_BT_GATTSERVER, + "connect", + "(Landroid/bluetooth/BluetoothDevice;Z)Z"); if (!jni_mid_connect) { OIC_LOG(ERROR, TAG, "jni_mid_connect is null"); @@ -1392,18 +1314,10 @@ CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice) return CA_ADAPTER_NOT_ENABLED; } - jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env, - "android/bluetooth/BluetoothGattServer"); - if (!jni_cid_bluetoothGattServer) - { - OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattServer is null"); - return CA_STATUS_FAILED; - } - - jmethodID jni_mid_cancelConnection = (*env)->GetMethodID(env, jni_cid_bluetoothGattServer, - "cancelConnection", - "(Landroid/bluetooth/BluetoothDevice;)" - "V"); + jmethodID jni_mid_cancelConnection = CALEGetJNIMethodID(env, CLASSPATH_BT_GATTSERVER, + "cancelConnection", + "(Landroid/bluetooth/BluetoothDevice;)" + "V"); if (!jni_mid_cancelConnection) { OIC_LOG(ERROR, TAG, "jni_mid_cancelConnection is null"); @@ -1433,14 +1347,8 @@ CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer) // get BluetoothGatt class OIC_LOG(DEBUG, TAG, "get BluetoothGatt class"); - jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGattServer"); - if (!jni_cid_BluetoothGatt) - { - OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null"); - return CA_STATUS_FAILED; - } - - jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V"); + jmethodID jni_mid_closeGatt = CALEGetJNIMethodID(env, CLASSPATH_BT_GATTSERVER, + "close", "()V"); if (!jni_mid_closeGatt) { OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null"); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c index cd4a425..2905742 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c @@ -30,6 +30,32 @@ #define TAG PCF("OIC_CA_LE_UTILS") +jmethodID CALEGetJNIMethodID(JNIEnv *env, const char* className, + const char* methodName, + const char* methodFormat) +{ + VERIFY_NON_NULL_RET(env, TAG, "env", NULL); + VERIFY_NON_NULL_RET(className, TAG, "className", NULL); + VERIFY_NON_NULL_RET(methodName, TAG, "methodName", NULL); + VERIFY_NON_NULL_RET(methodFormat, TAG, "methodFormat", NULL); + + jclass jni_cid = (*env)->FindClass(env, className); + if (!jni_cid) + { + OIC_LOG_V(ERROR, 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, TAG, "jni_midID [%s] is null", methodName); + return NULL; + } + + return jni_midID; +} + jobject CALEGetUuidFromString(JNIEnv *env, const char* uuid) { VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL); @@ -104,15 +130,9 @@ bool CALEIsBondedDevice(JNIEnv *env, jobject bluetoothDevice) VERIFY_NON_NULL_RET(env, TAG, "env is null", false); VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", false); - jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice"); - if (!jni_cid_device_list) - { - OIC_LOG(ERROR, TAG, "jni_cid_device_list is null"); - return false; - } - - jmethodID jni_mid_getBondState = (*env)->GetMethodID(env, jni_cid_device_list, "getBondState", - "()I"); + jmethodID jni_mid_getBondState = CALEGetJNIMethodID(env, "android/bluetooth/BluetoothDevice", + "getBondState", + "()I"); if (!jni_mid_getBondState) { OIC_LOG(ERROR, TAG, "jni_mid_getBondState is null"); @@ -178,15 +198,8 @@ jobjectArray CALEGetBondedDevices(JNIEnv *env) return NULL; } - jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set"); - if (!jni_cid_Set) - { - OIC_LOG(ERROR, TAG, "getBondedDevices : jni_cid_Set is null"); - return NULL; - } - - jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray", - "()[Ljava/lang/Object;"); + jmethodID jni_mid_toArray = CALEGetJNIMethodID(env, "java/util/Set", "toArray", + "()[Ljava/lang/Object;"); if (!jni_mid_toArray) { OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_toArray is null"); @@ -338,15 +351,9 @@ jstring CALEGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice) VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL); VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", NULL); - jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice"); - if (!jni_cid_device_list) - { - OIC_LOG(ERROR, TAG, "jni_cid_device_list is null"); - return NULL; - } - - jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress", - "()Ljava/lang/String;"); + jmethodID jni_mid_getAddress = CALEGetJNIMethodID(env, "android/bluetooth/BluetoothDevice", + "getAddress", + "()Ljava/lang/String;"); if (!jni_mid_getAddress) { OIC_LOG(ERROR, TAG, "jni_mid_getAddress is null"); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h index c1fe2ce..dcb1b04 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h @@ -62,6 +62,19 @@ static const jint BOND_NONE = 10; static const jint STATE_CONNECTED = 2; static const jint STATE_DISCONNECTED = 0; + +/** + * 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 of the method. + */ +jmethodID CALEGetJNIMethodID(JNIEnv *env, const char* className, + const char* methodName, + const char* methodFormat); + /** * get uuid(jni object) from uuid(character). * @param[in] env JNI interface pointer. -- 2.7.4