From: jihwan.seo Date: Thu, 5 Nov 2015 11:54:54 +0000 (+0900) Subject: added to call unregisterReceiver for Android BT/BLE. X-Git-Tag: 1.2.0+RC1~823 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b8bdfc0eec0c7797792203e357cdc73eb84c76fc;hp=5ac1f0a4857758955cc48b847722b3d244ea2fc7;p=platform%2Fupstream%2Fiotivity.git added to call unregisterReceiver for Android BT/BLE. there was no method to call unregisterReceiver. Change-Id: I7ceb5bc73d4a25a1e93cbbf6e3e167c0d9f5c98f Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/4047 Tested-by: jenkins-iotivity Reviewed-by: Jaehong Jo Reviewed-by: Jon A. Cruz --- diff --git a/android/android_api/base/src/main/java/org/iotivity/ca/CaEdrInterface.java b/android/android_api/base/src/main/java/org/iotivity/ca/CaEdrInterface.java index 1e4d47c..7a07b24 100644 --- a/android/android_api/base/src/main/java/org/iotivity/ca/CaEdrInterface.java +++ b/android/android_api/base/src/main/java/org/iotivity/ca/CaEdrInterface.java @@ -28,20 +28,25 @@ import android.content.Intent; import android.content.IntentFilter; public class CaEdrInterface { + private static Context mContext; private CaEdrInterface(Context context) { - - registerIntentFilter(context); + mContext = context; + registerIntentFilter(); } - private static IntentFilter registerIntentFilter(Context context) { + private static IntentFilter registerIntentFilter() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); - context.registerReceiver(mReceiver, filter); + mContext.registerReceiver(mReceiver, filter); return filter; } + public static void destroyEdrInterface() { + mContext.unregisterReceiver(mReceiver); + } + // Network Monitor private native static void caEdrStateChangedCallback(int state); diff --git a/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java b/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java index ef20573..2ec4471 100644 --- a/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java +++ b/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java @@ -42,13 +42,13 @@ public class CaLeClientInterface { private static String SERVICE_UUID = "ADE3D529-C784-4F63-A987-EB69F70EE816"; private static String TAG = "Sample_Service : CaLeClientInterface"; + private static Context mContext; private CaLeClientInterface(Context context) { - caLeRegisterLeScanCallback(mLeScanCallback); caLeRegisterGattCallback(mGattCallback); - - registerIntentFilter(context); + mContext = context; + registerIntentFilter(); } public static void getLeScanCallback() { @@ -59,14 +59,18 @@ public class CaLeClientInterface { caLeRegisterGattCallback(mGattCallback); } - private static IntentFilter registerIntentFilter(Context context) { + private static IntentFilter registerIntentFilter() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); - context.registerReceiver(mReceiver, filter); + mContext.registerReceiver(mReceiver, filter); return filter; } + public static void destroyLeInterface() { + mContext.unregisterReceiver(mReceiver); + } + private native static void caLeRegisterLeScanCallback(BluetoothAdapter.LeScanCallback callback); private native static void caLeRegisterGattCallback(BluetoothGattCallback callback); diff --git a/resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaEdrInterface.java b/resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaEdrInterface.java index 1e4d47c..7a07b24 100755 --- a/resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaEdrInterface.java +++ b/resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaEdrInterface.java @@ -28,20 +28,25 @@ import android.content.Intent; import android.content.IntentFilter; public class CaEdrInterface { + private static Context mContext; private CaEdrInterface(Context context) { - - registerIntentFilter(context); + mContext = context; + registerIntentFilter(); } - private static IntentFilter registerIntentFilter(Context context) { + private static IntentFilter registerIntentFilter() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); - context.registerReceiver(mReceiver, filter); + mContext.registerReceiver(mReceiver, filter); return filter; } + public static void destroyEdrInterface() { + mContext.unregisterReceiver(mReceiver); + } + // Network Monitor private native static void caEdrStateChangedCallback(int state); diff --git a/resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaLeClientInterface.java b/resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaLeClientInterface.java index 5a21786..019438a 100755 --- a/resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaLeClientInterface.java +++ b/resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaLeClientInterface.java @@ -42,13 +42,13 @@ public class CaLeClientInterface { private static String SERVICE_UUID = "ADE3D529-C784-4F63-A987-EB69F70EE816"; private static String TAG = "Sample_Service : CaLeClientInterface"; + private static Context mContext; private CaLeClientInterface(Context context) { - caLeRegisterLeScanCallback(mLeScanCallback); caLeRegisterGattCallback(mGattCallback); - - registerIntentFilter(context); + mContext = context; + registerIntentFilter(); } public static void getLeScanCallback() { @@ -59,14 +59,18 @@ public class CaLeClientInterface { caLeRegisterGattCallback(mGattCallback); } - private static IntentFilter registerIntentFilter(Context context) { + private static IntentFilter registerIntentFilter() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); - context.registerReceiver(mReceiver, filter); + mContext.registerReceiver(mReceiver, filter); return filter; } + public static void destroyLeInterface() { + mContext.unregisterReceiver(mReceiver); + } + private native static void caLeRegisterLeScanCallback(BluetoothAdapter.LeScanCallback callback); private native static void caLeRegisterGattCallback(BluetoothGattCallback callback); 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 b41dfb2..99c4523 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.c +++ b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.c @@ -507,6 +507,78 @@ void CAEDRTerminate() CAEDRNativeRemoveAllDeviceState(); CAEDRNativeRemoveAllDeviceSocket(env); + CAEDRDestroyJniInterface(); +} + +CAResult_t CAEDRDestroyJniInterface() +{ + OIC_LOG(DEBUG, TAG, "CAEDRDestroyJniInterface"); + + if (!g_jvm) + { + OIC_LOG(ERROR, TAG, "g_jvm is null"); + return CA_STATUS_FAILED; + } + + bool isAttached = false; + JNIEnv* env; + jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); + if (JNI_OK != res) + { + OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); + res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); + + if (JNI_OK != res) + { + OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); + return CA_STATUS_FAILED; + } + isAttached = true; + } + + jclass jni_EDRJniInterface = (*env)->FindClass(env, CLASSPATH_BT_INTERFACE); + if (!jni_EDRJniInterface) + { + OIC_LOG(ERROR, TAG, "Could not get CaEdrInterface class"); + goto error_exit; + } + + jmethodID jni_EDRInterfaceDestroyMethod = (*env)->GetStaticMethodID(env, jni_EDRJniInterface, + "destroyEdrInterface", + "()V"); + if (!jni_EDRInterfaceDestroyMethod) + { + OIC_LOG(ERROR, TAG, "Could not get CaEdrInterface destroy method"); + goto error_exit; + } + + (*env)->CallStaticVoidMethod(env, jni_EDRJniInterface, jni_EDRInterfaceDestroyMethod); + + if ((*env)->ExceptionCheck(env)) + { + OIC_LOG(ERROR, TAG, "destroyEdrInterface has failed"); + (*env)->ExceptionDescribe(env); + (*env)->ExceptionClear(env); + goto error_exit; + } + + OIC_LOG(DEBUG, TAG, "Destroy instance for CaEdrInterface"); + + if (isAttached) + { + (*g_jvm)->DetachCurrentThread(g_jvm); + } + + return CA_STATUS_OK; + +error_exit: + + if (isAttached) + { + (*g_jvm)->DetachCurrentThread(g_jvm); + } + + return CA_STATUS_FAILED; } void CAEDRCoreJniInit() diff --git a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.h b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.h index 98e2cc8..22fb6e6 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.h +++ b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.h @@ -60,6 +60,12 @@ void CAEDRInitialize(ca_thread_pool_t handle); void CAEDRTerminate(); /** + * destroy interface object and terminate the interface. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + */ +CAResult_t CAEDRDestroyJniInterface(); + +/** * Initialize JNI object. */ void CAEDRCoreJniInit(); 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 01de046..17fd2d6 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -335,6 +335,7 @@ void CALEClientTerminate() CALEClientSetSendFinishFlag(false); CALEClientTerminateGattMutexVariables(); + CALEClientDestroyJniInterface(); ca_cond_free(g_deviceDescCond); ca_cond_free(g_threadCond); @@ -348,6 +349,77 @@ void CALEClientTerminate() } } +CAResult_t CALEClientDestroyJniInterface() +{ + OIC_LOG(DEBUG, TAG, "CALEClientDestroyJniInterface"); + + if (!g_jvm) + { + OIC_LOG(ERROR, TAG, "g_jvm is null"); + return CA_STATUS_FAILED; + } + + bool isAttached = false; + JNIEnv* env; + jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); + if (JNI_OK != res) + { + OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); + res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); + + if (JNI_OK != res) + { + OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); + return CA_STATUS_FAILED; + } + isAttached = true; + } + + jclass jni_LeInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface"); + if (!jni_LeInterface) + { + OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface class"); + goto error_exit; + } + + jmethodID jni_InterfaceDestroyMethod = (*env)->GetStaticMethodID(env, jni_LeInterface, + "destroyLeInterface", + "()V"); + if (!jni_InterfaceDestroyMethod) + { + OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface destroy method"); + goto error_exit; + } + + (*env)->CallStaticVoidMethod(env, jni_LeInterface, jni_InterfaceDestroyMethod); + + if ((*env)->ExceptionCheck(env)) + { + OIC_LOG(ERROR, TAG, "destroyLeInterface has failed"); + (*env)->ExceptionDescribe(env); + (*env)->ExceptionClear(env); + goto error_exit; + } + + OIC_LOG(DEBUG, TAG, "Destroy instance for CaLeClientInterface"); + + if (isAttached) + { + (*g_jvm)->DetachCurrentThread(g_jvm); + } + + return CA_STATUS_OK; + +error_exit: + + if (isAttached) + { + (*g_jvm)->DetachCurrentThread(g_jvm); + } + + return CA_STATUS_FAILED; +} + void CALEClientSendFinish(JNIEnv *env, jobject gatt) { OIC_LOG(DEBUG, TAG, "CALEClientSendFinish"); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h index df53c3d..6131385 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h @@ -91,6 +91,12 @@ CAResult_t CALEClientInitialize(ca_thread_pool_t handle); void CALEClientTerminate(); /** + * destroy interface object and terminate the interface. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + */ +CAResult_t CALEClientDestroyJniInterface(); + +/** * for destroy sending routine. * @param[in] env JNI interface pointer. * @param[in] gatt Gatt profile object.