added to call unregisterReceiver for Android BT/BLE.
authorjihwan.seo <jihwan.seo@samsung.com>
Thu, 5 Nov 2015 11:54:54 +0000 (20:54 +0900)
committerJon A. Cruz <jonc@osg.samsung.com>
Mon, 23 Nov 2015 20:11:32 +0000 (20:11 +0000)
there was no method to call unregisterReceiver.

Change-Id: I7ceb5bc73d4a25a1e93cbbf6e3e167c0d9f5c98f
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4047
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
android/android_api/base/src/main/java/org/iotivity/ca/CaEdrInterface.java
android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java
resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaEdrInterface.java
resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaLeClientInterface.java
resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.c
resource/csdk/connectivity/src/bt_edr_adapter/android/caedrclient.h
resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c
resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h

index 1e4d47c5b313273265e0e533e52cf3628251e736..7a07b246bbd8f4955aa787dc9c376ab98f38eb6f 100644 (file)
@@ -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);
 
index ef20573bb98a57dad88ee8b869e4f510a03bbea2..2ec4471b065ecf19416a17ca49dad33d4e1509b0 100644 (file)
@@ -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);
index 1e4d47c5b313273265e0e533e52cf3628251e736..7a07b246bbd8f4955aa787dc9c376ab98f38eb6f 100755 (executable)
@@ -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);
 
index 5a21786c6523987fa3aa7060d05224df083e87fa..019438a7a889b69161365543abeaaa05edc57373 100755 (executable)
@@ -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);
index b41dfb28403a22609b1950276e98852f637a21f7..99c4523349d32958796bb27b5895e074c2210a4e 100644 (file)
@@ -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()
index 98e2cc885c31a9f5c50caa4106bdfd25ecbca418..22fb6e65e3ec0341ef87fd977a1fb1a1b6e0a91b 100644 (file)
@@ -59,6 +59,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.
  */
index 01de046e1aff752d493b7a5bfb3990941102b388..17fd2d69f85b85d91e99319ccd89cf80a15c02f8 100644 (file)
@@ -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");
index df53c3d23b0fb6ca25cebde4de021802b7d6c88c..613138554cddd8131446f463ae793439254ce66c 100644 (file)
@@ -90,6 +90,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.