Merge branch 'master' into easysetup
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / android / caedrutils.c
index e0cdfd5..5b682c7 100644 (file)
+/******************************************************************
+ *
+ * Copyright 2014 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
 #include <jni.h>
 #include <stdio.h>
 #include <android/log.h>
 #include "caedrutils.h"
 #include "logger.h"
 #include "oic_malloc.h"
-#include "uthreadpool.h"
+#include "oic_string.h"
+#include "cathreadpool.h"
 #include "uarraylist.h"
 
-#define TAG PCF("CA_EDR_UTILS")
+#define ERROR_CODE (-1)
+#define TAG PCF("OIC_CA_EDR_UTILS")
 
-static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
-static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
-static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
+static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
+static const char METHODID_STRINGNONPARAM[] = "()Ljava/lang/String;";
+static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
+static const char CLASSPATH_BT_DEVICE[] = "android/bluetooth/BluetoothDevice";
+static const char CLASSPATH_BT_SOCKET[] = "android/bluetooth/BluetoothSocket";
 
-static u_arraylist_t *gdeviceStateList = NULL;
-static u_arraylist_t *gdeviceObjectList = NULL;
+static u_arraylist_t *g_deviceStateList = NULL;
+static u_arraylist_t *g_deviceObjectList = NULL;
 
 // get address from bluetooth socket
 jstring CAEDRNativeGetAddressFromDeviceSocket(JNIEnv *env, jobject bluetoothSocketObj)
 {
-    if(!bluetoothSocketObj)
+    if (!bluetoothSocketObj)
     {
+        OIC_LOG(ERROR, TAG, "bluetoothSocketObj is null");
         return NULL;
     }
 
-    jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
-    if(!jni_cid_BTSocket)
+    jclass jni_cid_BTSocket = (*env)->FindClass(env, CLASSPATH_BT_SOCKET);
+    if (!jni_cid_BTSocket)
     {
-        OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_cid_BTSocket is null");
+        OIC_LOG(ERROR, TAG, "jni_cid_BTSocket is null");
         return NULL;
     }
 
-    jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTSocket, "getRemoteDevice",
-         "()Landroid/bluetooth/BluetoothDevice;");
-    if(!jni_mid_getRemoteDevice)
+    jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(
+            env, jni_cid_BTSocket, "getRemoteDevice", "()Landroid/bluetooth/BluetoothDevice;");
+    if (!jni_mid_getRemoteDevice)
     {
-        OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_mid_getRemoteDevice is null");
+        OIC_LOG(ERROR, TAG, "jni_mid_getRemoteDevice is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
         return NULL;
     }
 
-    jobject jni_obj_remoteBTDevice = (*env)->CallObjectMethod(env, bluetoothSocketObj, jni_mid_getRemoteDevice);
-    if(!jni_obj_remoteBTDevice)
+    jobject jni_obj_remoteBTDevice = (*env)->CallObjectMethod(env, bluetoothSocketObj,
+                                                              jni_mid_getRemoteDevice);
+    if (!jni_obj_remoteBTDevice)
     {
-        OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_obj_remoteBTDevice is null");
+        OIC_LOG(ERROR, TAG, "jni_obj_remoteBTDevice is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
         return NULL;
     }
 
-    jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
-    jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress", "()Ljava/lang/String;");
+    jclass jni_cid_BTDevice = (*env)->FindClass(env, CLASSPATH_BT_DEVICE);
+    if (!jni_cid_BTDevice)
+    {
+        OIC_LOG(ERROR, TAG, "jni_cid_BTDevice is null");
+        (*env)->DeleteLocalRef(env, jni_obj_remoteBTDevice);
+        (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
+        return NULL;
+    }
+    jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress",
+                                                     METHODID_STRINGNONPARAM);
+    if (!j_mid_getAddress)
+    {
+        OIC_LOG(ERROR, TAG, "j_mid_getAddress is null");
+        (*env)->DeleteLocalRef(env, jni_obj_remoteBTDevice);
+        (*env)->DeleteLocalRef(env, jni_cid_BTDevice);
+        (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
+        return NULL;
+    }
 
     jstring j_str_address = (*env)->CallObjectMethod(env, jni_obj_remoteBTDevice, j_mid_getAddress);
-    if(j_str_address)
+    if (!j_str_address)
     {
-        const char * address = (*env)->GetStringUTFChars(env, j_str_address, NULL);
-        OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: ~~ remote device address is %s", address);
-//      (*env)->ReleaseStringUTFChars(env, j_str_address, address);
-    } else {
+        OIC_LOG(ERROR, TAG, "j_str_address is null");
+        (*env)->DeleteLocalRef(env, jni_obj_remoteBTDevice);
+        (*env)->DeleteLocalRef(env, jni_cid_BTDevice);
+        (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
         return NULL;
     }
+
+    (*env)->DeleteLocalRef(env, jni_obj_remoteBTDevice);
+    (*env)->DeleteLocalRef(env, jni_cid_BTDevice);
+    (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
+
     return j_str_address;
 }
 
 jstring CAEDRNativeGetLocalDeviceAddress(JNIEnv* env)
 {
-    jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
-    if(!jni_cid_BTAdapter)
+    jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
+    if (!jni_cid_BTAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_cid_BTAdapter is null");
+        OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
         return NULL;
     }
 
-    jmethodID jni_mid_getDefaultAdapter =
-            (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
-    if(!jni_mid_getDefaultAdapter)
+    jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
+                                                                    "getDefaultAdapter",
+                                                                    METHODID_OBJECTNONPARAM);
+    if (!jni_mid_getDefaultAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_mid_getDefaultAdapter is null");
+        OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
         return NULL;
     }
 
-    jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getAddress", METHODID_STRINGNONPARAM);
-    if(!jni_mid_getAddress)
+    jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getAddress",
+                                                       METHODID_STRINGNONPARAM);
+    if (!jni_mid_getAddress)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_mid_getAddress is null");
+        OIC_LOG(ERROR, TAG, "jni_mid_getAddress is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
         return NULL;
     }
 
-    jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
-    if(!jni_obj_BTAdapter)
+    jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
+                                                               jni_mid_getDefaultAdapter);
+    if (!jni_obj_BTAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_obj_BTAdapter is null");
+        OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
         return NULL;
     }
 
-    jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getAddress);
-    if(!jni_str_address)
+    jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env, jni_obj_BTAdapter,
+                                                                jni_mid_getAddress);
+    if (!jni_str_address)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_str_address is null");
+        OIC_LOG(ERROR, TAG, "jni_str_address is null");
+        (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
         return NULL;
     }
 
+    (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+    (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+
     return jni_str_address;
 }
 
 jobjectArray CAEDRNativeGetBondedDevices(JNIEnv *env)
 {
-    jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
-    if(!jni_cid_BTAdapter)
+    jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
+    if (!jni_cid_BTAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_cid_BTAdapter is null");
+        OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
         return NULL;
     }
 
-    jmethodID jni_mid_getDefaultAdapter =
-            (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
+    jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
+                                                                    "getDefaultAdapter",
+                                                                    METHODID_OBJECTNONPARAM);
+    if (!jni_mid_getDefaultAdapter)
+    {
+        OIC_LOG(ERROR, TAG, "default adapter is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+        return NULL;
+    }
 
-    jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
-    if(!jni_obj_BTAdapter)
+    jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
+                                                               jni_mid_getDefaultAdapter);
+    if (!jni_obj_BTAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: bluetooth adapter is null");
+        OIC_LOG(ERROR, TAG, "bluetooth adapter is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
         return NULL;
     }
 
     // Get a list of currently paired devices
     jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
-            "getBondedDevices", "()Ljava/util/Set;");
-    if(!jni_mid_getBondedDevices)
+                                                             "getBondedDevices",
+                                                             "()Ljava/util/Set;");
+    if (!jni_mid_getBondedDevices)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
+        OIC_LOG(ERROR, TAG, "jni_mid_getBondedDevicesr is null");
+        (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
         return NULL;
     }
 
-    jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getBondedDevices);
-    if(!jni_obj_setPairedDevices)
+    jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
+                                                                jni_mid_getBondedDevices);
+    if (!jni_obj_setPairedDevices)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_obj_setPairedDevices is null");
+        OIC_LOG(ERROR, TAG, "ni_obj_setPairedDevices is null");
+        (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
         return NULL;
     }
 
     // Convert the set to an object array
     // object[] array = Set<BluetoothDevice>.toArray();
-    jclass jni_cid_Set = (*env)->FindClass(env,  "java/util/Set");
-    jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray", "()[Ljava/lang/Object;");
+    jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
+    if (!jni_cid_Set)
+    {
+        OIC_LOG(ERROR, TAG, "jni_cid_Set is null");
+        goto exit;
+    }
+    jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray",
+                                                    "()[Ljava/lang/Object;");
 
-    if(!jni_mid_toArray)
+    if (!jni_mid_toArray)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_mid_toArray is null");
-        return NULL;
+        OIC_LOG(ERROR, TAG, "jni_mid_toArray is null");
+        goto exit;
     }
 
-    jobjectArray jni_arrayPairedDevices = (jobjectArray)((*env)->CallObjectMethod(env,
-            jni_obj_setPairedDevices, jni_mid_toArray));
-    if(!jni_arrayPairedDevices)
+    jobjectArray jni_arrayPairedDevices = (jobjectArray)(
+            (*env)->CallObjectMethod(env, jni_obj_setPairedDevices, jni_mid_toArray));
+    if (!jni_arrayPairedDevices)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_arrayPairedDevices is null");
-        return NULL;
+        OIC_LOG(ERROR, TAG, "jni_arrayPairedDevices is null");
+        goto exit;
     }
 
+    (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+    (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+    (*env)->DeleteLocalRef(env, jni_obj_setPairedDevices);
+
     return jni_arrayPairedDevices;
+
+exit:
+    (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+    (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+    (*env)->DeleteLocalRef(env, jni_obj_setPairedDevices);
+    return NULL;
 }
 
 jint CAEDRNativeGetBTStateOnInfo(JNIEnv *env)
 {
-    jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
-    if(!jni_cid_BTAdapter)
+    jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
+    if (!jni_cid_BTAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
-        return -1;
+        OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
+        return ERROR_CODE;
     }
 
     jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
     if (jni_fid_stateon == 0)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] get_field_state is 0");
-        return -1;
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+
+        OIC_LOG(ERROR, TAG, "get_field_state is 0");
+        return ERROR_CODE;
     }
     jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
 
-    OIC_LOG_V(DEBUG, TAG, "[EDR][Native] bluetooth STATE_ON state integer value : %d", jni_int_val);
+    OIC_LOG_V(DEBUG, TAG, "bluetooth state integer value : %d", jni_int_val);
+
+    (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
 
     return jni_int_val;
 }
 
 jboolean CAEDRNativeIsEnableBTAdapter(JNIEnv *env)
 {
-    jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
-    if(!jni_cid_BTAdapter)
+    jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
+    if (!jni_cid_BTAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
-        return FALSE;
+        OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
+        return JNI_FALSE;
     }
 
-    jmethodID jni_mid_getDefaultAdapter =
-            (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
-    if(!jni_mid_getDefaultAdapter)
+    jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
+                                                                    "getDefaultAdapter",
+                                                                    METHODID_OBJECTNONPARAM);
+    if (!jni_mid_getDefaultAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_getDefaultAdapter is null");
-        return FALSE;
+        OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+        return JNI_FALSE;
     }
 
-    jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
-    if(!jni_obj_BTAdapter)
+    jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
+                                                               jni_mid_getDefaultAdapter);
+    if (!jni_obj_BTAdapter)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_obj_BTAdapter is null");
-        return FALSE;
+        OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+        return JNI_FALSE;
     }
 
     // isEnable()
-    jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled",
-            "()Z");
-    if(!jni_mid_isEnable)
+    jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled", "()Z");
+    if (!jni_mid_isEnable)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_isEnable is null");
-        return FALSE;
+        OIC_LOG(ERROR, TAG, "jni_mid_isEnable is null");
+        (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+        (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+        return JNI_FALSE;
     }
 
     jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
-    OIC_LOG_V(DEBUG, TAG, "[EDR][Native] adapter state is %d", jni_isEnable);
+
+    (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+    (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
 
     return jni_isEnable;
 }
 
 jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
 {
+    if (!bluetoothDevice)
+    {
+        OIC_LOG(ERROR, TAG, "bluetoothDevice is null");
+        return NULL;
+    }
     jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
-    if(!jni_cid_device_list)
+    if (!jni_cid_device_list)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_cid_device_list is null");
+        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;");
-    if(!jni_mid_getAddress)
+                                                       METHODID_STRINGNONPARAM);
+    if (!jni_mid_getAddress)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_getAddress is null");
+        OIC_LOG(ERROR, TAG, "jni_mid_getAddress is null");
         return NULL;
     }
 
-    jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice, jni_mid_getAddress);
-    if(!jni_address)
+    jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
+                                                            jni_mid_getAddress);
+    if (!jni_address)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_address is null");
+        OIC_LOG(ERROR, TAG, "jni_address is null");
         return NULL;
     }
     return jni_address;
 }
 
-
 /**
  * BT State List
  */
 void CAEDRNativeCreateDeviceStateList()
 {
-    OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceStateList");
+    OIC_LOG(DEBUG, TAG, "CAEDRNativeCreateDeviceStateList");
 
     // create new object array
-    if (gdeviceStateList == NULL)
+    if (NULL == g_deviceStateList)
     {
-        OIC_LOG_V(DEBUG, TAG, "Create device list");
+        OIC_LOG(DEBUG, TAG, "Create device list");
 
-        gdeviceStateList = u_arraylist_create();
+        g_deviceStateList = u_arraylist_create();
     }
 }
 
-void CAEDRUpdateDeviceState(uint32_t state, const char* address)
+void CAEDRUpdateDeviceState(CAConnectedState_t state, const char *address)
 {
-    state_t *newstate = (state_t*) OICMalloc( sizeof(state_t) );
-    memset(newstate->address, 0, CA_MACADDR_SIZE);
-    strcpy(newstate->address, address);
-    newstate->state = state;
+    if (!address)
+    {
+        OIC_LOG(ERROR, TAG, "address is null");
+        return;
+    }
+    CAConnectedDeviceInfo_t *deviceInfo =
+            (CAConnectedDeviceInfo_t *) OICCalloc(1, sizeof(CAConnectedDeviceInfo_t));
+    if (!deviceInfo)
+    {
+        OIC_LOG(ERROR, TAG, "deviceInfo is null");
+        return;
+    }
+    OICStrcpy((char*) deviceInfo->address, sizeof(deviceInfo->address), address);
+    deviceInfo->state = state;
 
-    CAEDRNativeAddDeviceStateToList(newstate);
+    CAEDRNativeAddDeviceStateToList(deviceInfo);
 }
 
-void CAEDRNativeAddDeviceStateToList(state_t* state)
+void CAEDRNativeAddDeviceStateToList(CAConnectedDeviceInfo_t *deviceInfo)
 {
-    if(!state)
+    if (!deviceInfo)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] device is null");
+        OIC_LOG(ERROR, TAG, "device is null");
         return;
     }
 
-    if(!gdeviceStateList)
+    if (!g_deviceStateList)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] gdevice_list is null");
+        OIC_LOG(ERROR, TAG, "gdevice_list is null");
         return;
     }
 
-    if(CAEDRNativeIsDeviceInList(state->address)) {
-        CAEDRNativeRemoveDevice(state->address); // delete previous state for update new state
+    if (CAEDRNativeIsDeviceInList((const char*) deviceInfo->address))
+    {
+        // delete previous state for update new state
+        CAEDRNativeRemoveDevice((const char*) deviceInfo->address);
     }
-    u_arraylist_add(gdeviceStateList, state);          // update new state
-    OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d", state->state);
+    u_arraylist_add(g_deviceStateList, deviceInfo); // update new state
+    OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d", deviceInfo->state);
 }
 
-jboolean CAEDRNativeIsDeviceInList(const char* remoteAddress){
+bool CAEDRNativeIsDeviceInList(const char* remoteAddress)
+{
+    if (!remoteAddress)
+    {
+        OIC_LOG(ERROR, TAG, "remoteAddress is null");
+        return false;
+    }
 
-    jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
     {
-        state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
-        if(!state)
+        CAConnectedDeviceInfo_t* deviceInfo =
+                (CAConnectedDeviceInfo_t*) u_arraylist_get(g_deviceStateList, index);
+        if (!deviceInfo)
         {
-            OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
-            return TRUE;
+            OIC_LOG(ERROR, TAG, "deviceInfo object is null");
+            return false;
         }
 
-        if(!strcmp(remoteAddress, state->address))
+        if (!strcmp(remoteAddress, (const char*) deviceInfo->address))
         {
-            OIC_LOG_V(DEBUG, TAG, "the device is already set");
-            return TRUE;
-        }
-        else
-        {
-            continue;
+            OIC_LOG(DEBUG, TAG, "the device is already set");
+            return true;
         }
     }
 
-    OIC_LOG_V(DEBUG, TAG, "there are no the device in list.");
-    return FALSE;
+    OIC_LOG(DEBUG, TAG, "there are no the device in list.");
+    return false;
 }
 
 void CAEDRNativeRemoveAllDeviceState()
 {
-    OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
+    OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
 
-    if(!gdeviceStateList)
+    if (!g_deviceStateList)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceStateList is null");
         return;
     }
 
     jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (index = 0; index < length; index++)
     {
-        state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
-        if(!state)
+        CAConnectedDeviceInfo_t* deviceInfo =
+                (CAConnectedDeviceInfo_t*) u_arraylist_get(g_deviceStateList, index);
+        if (!deviceInfo)
         {
-            OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
+            OIC_LOG(DEBUG, TAG, "jarrayObj is null");
             continue;
         }
-        OICFree(state);
+        OICFree(deviceInfo);
     }
 
-    OICFree(gdeviceStateList);
-    gdeviceStateList = NULL;
+    OICFree(g_deviceStateList);
+    g_deviceStateList = NULL;
     return;
 }
 
-void CAEDRNativeRemoveDevice(const charremoteAddress)
+void CAEDRNativeRemoveDevice(const char *remoteAddress)
 {
-    OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceforStateList");
+    OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceforStateList");
 
-    if(!gdeviceStateList)
+    if (!g_deviceStateList)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceStateList is null");
+        return;
+    }
+    if (!remoteAddress)
+    {
+        OIC_LOG(ERROR, TAG, "remoteAddress is null");
         return;
     }
 
-    jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
     {
-        state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
-        if(!state)
+        CAConnectedDeviceInfo_t* deviceInfo =
+                (CAConnectedDeviceInfo_t*) u_arraylist_get(g_deviceStateList, index);
+        if (!deviceInfo)
         {
-            OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
+            OIC_LOG(DEBUG, TAG, "deviceInfo object is null");
             continue;
         }
 
-        if(!strcmp(state->address, remoteAddress))
+        if (!strcmp((const char*) deviceInfo->address, remoteAddress))
         {
-            OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove state : %s", remoteAddress);
-            OICFree(state);
+            OIC_LOG_V(DEBUG, TAG, "remove state : %s", remoteAddress);
+            OICFree(deviceInfo);
 
-            CAEDRReorderingDeviceList(index);
+            u_arraylist_remove(g_deviceStateList, index);
             break;
         }
     }
     return;
 }
 
-jboolean CAEDRIsConnectedDevice(const char* remoteAddress)
+CAConnectedState_t CAEDRIsConnectedDevice(const char *remoteAddress)
 {
-    OIC_LOG_V(DEBUG, TAG, "CAEDRIsConnectedDevice");
+    OIC_LOG(DEBUG, TAG, "CAEDRIsConnectedDevice");
 
-    if(!gdeviceStateList)
+    if (!remoteAddress)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
-        return FALSE;
+        OIC_LOG(ERROR, TAG, "remoteAddress is null");
+        return STATE_DISCONNECTED;
     }
 
-    jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
+    if (!g_deviceStateList)
+    {
+        OIC_LOG(ERROR, TAG, "gdeviceStateList is null");
+        return STATE_DISCONNECTED;
+    }
+
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
     {
-        state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
-        if(!state)
+        CAConnectedDeviceInfo_t* deviceInfo =
+                (CAConnectedDeviceInfo_t*) u_arraylist_get(g_deviceStateList, index);
+        if (!deviceInfo)
         {
-            OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
+            OIC_LOG(DEBUG, TAG, "deviceInfo object is null");
             continue;
         }
 
-        if(!strcmp(state->address, remoteAddress))
+        if (!strcmp((const char*) deviceInfo->address, remoteAddress))
         {
-            OIC_LOG_V(DEBUG, TAG, "[EDR][Native] check whether it is connected or not");
-
-            return state->state;
+            return deviceInfo->state;
         }
     }
-    return FALSE;
-}
-
-void CAEDRReorderingDeviceList(uint32_t index)
-{
-    if (index >= gdeviceStateList->length)
-    {
-        return;
-    }
-
-    if (index < gdeviceStateList->length - 1)
-    {
-        memmove(&gdeviceStateList->data[index], &gdeviceStateList->data[index + 1],
-                (gdeviceStateList->length - index - 1) * sizeof(void *));
-    }
-
-    gdeviceStateList->size--;
-    gdeviceStateList->length--;
+    return STATE_DISCONNECTED;
 }
 
 /**
@@ -432,128 +551,143 @@ void CAEDRReorderingDeviceList(uint32_t index)
  */
 void CAEDRNativeCreateDeviceSocketList()
 {
-    OIC_LOG(DEBUG, TAG, "[BLE][Native] CAEDRNativeCreateDeviceSocketList");
+    OIC_LOG(DEBUG, TAG, "CAEDRNativeCreateDeviceSocketList");
 
     // create new object array
-    if (gdeviceObjectList == NULL)
+    if (NULL == g_deviceObjectList)
     {
-        OIC_LOG_V(DEBUG, TAG, "Create Device object list");
+        OIC_LOG(DEBUG, TAG, "Create Device object list");
 
-        gdeviceObjectList = u_arraylist_create();
+        g_deviceObjectList = u_arraylist_create();
     }
 }
 
 void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
 {
-    OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeAddDeviceobjToList");
+    OIC_LOG(DEBUG, TAG, "CANativeAddDeviceobjToList");
 
-    if(!deviceSocket)
+    if (!deviceSocket)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] Device is null");
+        OIC_LOG(ERROR, TAG, "Device is null");
         return;
     }
 
-    if(!gdeviceObjectList)
+    if (!g_deviceObjectList)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
         return;
     }
 
     jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
-    if(!jni_remoteAddress)
+    if (!jni_remoteAddress)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
+        OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
         return;
     }
 
     const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
 
-    if(!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
+    if (!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
     {
         jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
-        u_arraylist_add(gdeviceObjectList, gDeviceSocker);
-        OIC_LOG_V(DEBUG, TAG, "Set Socket Object to Array");
+        u_arraylist_add(g_deviceObjectList, gDeviceSocker);
+        OIC_LOG(DEBUG, TAG, "Set Socket Object to Array");
     }
+    (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
+    (*env)->DeleteLocalRef(env, jni_remoteAddress);
 }
 
-jboolean CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
+bool CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
 {
-    OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsDeviceObjInList");
+    OIC_LOG(DEBUG, TAG, "CANativeIsDeviceObjInList");
 
-    jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
+    if (!remoteAddress)
+    {
+        OIC_LOG(ERROR, TAG, "remoteAddress is null");
+        return false;
+    }
+
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
     {
 
-        jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
-        if(!jarrayObj)
+        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
+        if (!jarrayObj)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
-            return TRUE;
+            OIC_LOG(DEBUG, TAG, "jarrayObj is null");
+            return false;
         }
 
         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
-        if(!jni_setAddress)
+        if (!jni_setAddress)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
-            return TRUE;
+            OIC_LOG(DEBUG, TAG, "jni_setAddress is null");
+            return false;
         }
 
         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
+        if (!setAddress)
+        {
+            OIC_LOG(DEBUG, TAG, "setAddress is null");
+            return false;
+        }
 
-        if(!strcmp(remoteAddress, setAddress))
+        if (!strcmp(remoteAddress, setAddress))
         {
-            OIC_LOG_V(DEBUG, TAG, "the device is already set");
-            return TRUE;
+            OIC_LOG(DEBUG, TAG, "the device is already set");
+            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+            return true;
         }
         else
         {
+            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
             continue;
         }
     }
 
-    OIC_LOG_V(DEBUG, TAG, "there are no the Device obejct in list. we can add");
-    return FALSE;
+    OIC_LOG(DEBUG, TAG, "there are no the Device obejct in list. we can add");
+    return false;
 }
 
 void CAEDRNativeSocketCloseToAll(JNIEnv *env)
 {
-    OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeSocketCloseToAll");
+    OIC_LOG(DEBUG, TAG, "CAEDRNativeSocketCloseToAll");
 
-    if(!gdeviceObjectList)
+    if (!g_deviceObjectList)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
         return;
     }
 
-    jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
-    if(!jni_cid_BTSocket)
+    jclass jni_cid_BTSocket = (*env)->FindClass(env, CLASSPATH_BT_SOCKET);
+    if (!jni_cid_BTSocket)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
+        OIC_LOG(ERROR, TAG, "jni_cid_BTSocket is null");
         return;
     }
 
     jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
-    if(!jni_mid_close)
+    if (!jni_mid_close)
     {
-        OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_mid_close is null");
+        OIC_LOG(ERROR, TAG, "jni_mid_close is null");
         return;
     }
 
-    jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
     {
-        jobject jni_obj_socket = (jobject) u_arraylist_get(gdeviceObjectList, index);
-        if(!jni_obj_socket)
+        jobject jni_obj_socket = (jobject) u_arraylist_get(g_deviceObjectList, index);
+        if (!jni_obj_socket)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] socket obj is null");
+            OIC_LOG(ERROR, TAG, "socket obj is null");
             return;
         }
 
         (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
 
-        if((*env)->ExceptionCheck(env))
+        if ((*env)->ExceptionCheck(env))
         {
-            OIC_LOG(DEBUG, TAG, "[EDR][Native] close: close is Failed!!!");
+            OIC_LOG(ERROR, TAG, "close is Failed!!!");
             (*env)->ExceptionDescribe(env);
             (*env)->ExceptionClear(env);
             return;
@@ -563,169 +697,235 @@ void CAEDRNativeSocketCloseToAll(JNIEnv *env)
 
 void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
 {
-    OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
+    OIC_LOG(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
 
-    if(!gdeviceObjectList)
+    if (!g_deviceObjectList)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
         return;
     }
 
-    jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
     {
-        jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
-        if(!jarrayObj)
+        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
+        if (!jarrayObj)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
+            OIC_LOG(ERROR, TAG, "jarrayObj is null");
             return;
         }
         (*env)->DeleteGlobalRef(env, jarrayObj);
     }
 
-    OICFree(gdeviceObjectList);
-    gdeviceObjectList = NULL;
+    OICFree(g_deviceObjectList);
+    g_deviceObjectList = NULL;
     return;
 }
 
 void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
 {
-    OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
+    OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
 
-    if(!gdeviceObjectList)
+    if (!g_deviceObjectList)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
         return;
     }
 
-    jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
     {
-        jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
-        if(!jarrayObj)
+        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
+        if (!jarrayObj)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
+            OIC_LOG(DEBUG, TAG, "jarrayObj is null");
             continue;
         }
 
         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
-        if(!jni_setAddress)
+        if (!jni_setAddress)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
+            OIC_LOG(DEBUG, TAG, "jni_setAddress is null");
             continue;
         }
-        const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
 
         jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
-        if(!jni_remoteAddress)
+        if (!jni_remoteAddress)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
+            OIC_LOG(DEBUG, TAG, "jni_remoteAddress is null");
             continue;
         }
+
+        const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
         const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
 
-        if(!strcmp(setAddress, remoteAddress))
+        if (!strcmp(setAddress, remoteAddress))
         {
-            OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
+            OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
             (*env)->DeleteGlobalRef(env, jarrayObj);
+            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+            (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
 
-            CAEDRReorderingDeviceSocketList(index);
+            u_arraylist_remove(g_deviceObjectList, index);
             break;
         }
+        (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+        (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
     }
 
-    OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
+    OIC_LOG(DEBUG, TAG, "there are no target object");
     return;
 }
 
 void CAEDRNativeRemoveDeviceSocketBaseAddr(JNIEnv *env, jstring address)
 {
-    OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
+    OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
 
-    if(!gdeviceObjectList)
+    if (!g_deviceObjectList)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
         return;
     }
 
-    jint index;
-    for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
     {
-        jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
-        if(!jarrayObj)
+        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
+        if (!jarrayObj)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
+            OIC_LOG(DEBUG, TAG, "jarrayObj is null");
             continue;
         }
 
         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
-        if(!jni_setAddress)
+        if (!jni_setAddress)
         {
-            OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
+            OIC_LOG(ERROR, TAG, "jni_setAddress is null");
             continue;
         }
         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
         const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
 
-        if(!strcmp(setAddress, remoteAddress))
+        if (!strcmp(setAddress, remoteAddress))
         {
-            OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
+            OIC_LOG_V(ERROR, TAG, "remove object : %s", remoteAddress);
             (*env)->DeleteGlobalRef(env, jarrayObj);
+            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+            (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
 
-            CAEDRReorderingDeviceSocketList(index);
+            u_arraylist_remove(g_deviceObjectList, index);
             break;
         }
+        (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+        (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
     }
 
-    OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
+    OIC_LOG(DEBUG, TAG, "there are no target object");
     return;
 }
 
 jobject CAEDRNativeGetDeviceSocket(uint32_t idx)
 {
-    OIC_LOG_V(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
-
-    if(!gdeviceObjectList)
+    if (!g_deviceObjectList)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
         return NULL;
     }
 
-    jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, idx);
-    if(!jarrayObj)
+    jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, idx);
+    if (!jarrayObj)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is not available");
+        OIC_LOG(ERROR, TAG, "jarrayObj is not available");
         return NULL;
     }
     return jarrayObj;
 }
 
+jobject CAEDRNativeGetDeviceSocketBaseAddr(JNIEnv *env, const char* remoteAddress)
+{
+    OIC_LOG(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
+
+    if (!g_deviceObjectList)
+    {
+        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
+        return NULL;
+    }
+
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
+    {
+        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
+        if (!jarrayObj)
+        {
+            OIC_LOG(ERROR, TAG, "jarrayObj is null");
+            continue;
+        }
+
+        jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
+        if (!jni_setAddress)
+        {
+            OIC_LOG(ERROR, TAG, "jni_setAddress is null");
+            continue;
+        }
+        const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
+
+        if (!strcmp(setAddress, remoteAddress))
+        {
+            OIC_LOG_V(ERROR, TAG, "remove object : %s", remoteAddress);
+            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+            (*env)->DeleteLocalRef(env, jni_setAddress);
+            return jarrayObj;
+        }
+        (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+        (*env)->DeleteLocalRef(env, jni_setAddress);
+    }
+
+    return NULL;
+}
+
 uint32_t CAEDRGetSocketListLength()
 {
-    if(!gdeviceObjectList)
+    if (!g_deviceObjectList)
     {
-        OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
+        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
         return 0;
     }
 
-    uint32_t length = u_arraylist_length(gdeviceObjectList);
+    uint32_t length = u_arraylist_length(g_deviceObjectList);
 
     return length;
 }
 
-void CAEDRReorderingDeviceSocketList(uint32_t index)
+CAConnectedDeviceInfo_t *CAEDRGetDeviceInfoFromAddress(const char *remoteAddress)
 {
-    if (index >= gdeviceObjectList->length)
+    OIC_LOG(DEBUG, TAG, "CAEDRGetDeviceInfoFromAddress");
+
+    if (!g_deviceStateList)
     {
-        return;
+        OIC_LOG(ERROR, TAG, "gdeviceStateList is null");
+        return NULL;
     }
-
-    if (index < gdeviceObjectList->length - 1)
+    if (!remoteAddress)
     {
-        memmove(&gdeviceObjectList->data[index], &gdeviceObjectList->data[index + 1],
-                (gdeviceObjectList->length - index - 1) * sizeof(void *));
+        OIC_LOG(ERROR, TAG, "remoteAddress is null");
+        return NULL;
     }
 
-    gdeviceObjectList->size--;
-    gdeviceObjectList->length--;
+    jint length = u_arraylist_length(g_deviceStateList);
+    for (jint index = 0; index < length; index++)
+    {
+        CAConnectedDeviceInfo_t* deviceInfo =
+                (CAConnectedDeviceInfo_t*) u_arraylist_get(g_deviceStateList, index);
+        if (!deviceInfo)
+        {
+            OIC_LOG(DEBUG, TAG, "deviceInfo object is null");
+            continue;
+        }
+
+        if (!strcmp((const char*) deviceInfo->address, remoteAddress))
+        {
+            return deviceInfo;
+        }
+    }
+    return NULL;
 }