Fixed klockwork memory leaks and modified the logs
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / android / caedrclient.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <jni.h>
4
5 #include "caedrinterface.h"
6 #include "caedrutils.h"
7 #include "caedrclient.h"
8 #include "caleserver.h"
9 #include "logger.h"
10 #include "oic_malloc.h"
11 #include "uthreadpool.h" /* for thread pool */
12 #include "umutex.h"
13 #include "uarraylist.h"
14 #include "caadapterutils.h"
15 #include "com_iotivity_jar_CALeInterface.h"
16
17 //#define DEBUG_MODE
18 #define TAG PCF("CA_EDR_CLIENT")
19
20 static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
21 static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
22 static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
23 static const char *CLASSPATH_BT_UUID = "java/util/UUID";
24
25 static const uint32_t STATE_CONNECTED = 1;
26 static const uint32_t STATE_DISCONNECTED = 0;
27
28 static const uint32_t MAX_PDU_BUFFER = 1024;
29
30 static u_arraylist_t *gdeviceStateList = NULL;
31 static u_arraylist_t *gdeviceObjectList = NULL;
32
33 static u_thread_pool_t gThreadPoolHandle = NULL;
34
35 static JavaVM *g_jvm;
36 static jobject gContext;
37
38 static jbyteArray gSendBuffer;
39
40 /**
41  * @var gMutexSocketListManager
42  * @brief Mutex to synchronize socket list update
43  */
44 static u_mutex gMutexSocketListManager;
45
46 // server socket instance
47 static jobject gServerSocketObject = NULL;
48
49 /**
50  * @var gMutexUnicastServer
51  * @brief Mutex to synchronize unicast server
52  */
53 static u_mutex gMutexUnicastServer = NULL;
54
55 /**
56  * @var gStopUnicast
57  * @brief Flag to control the Receive Unicast Data Thread
58  */
59 static bool gStopUnicast = FALSE;
60
61 /**
62  * @var gMutexMulticastServer
63  * @brief Mutex to synchronize secure multicast server
64  */
65 static u_mutex gMutexMulticastServer = NULL;
66
67 /**
68  * @var gStopMulticast
69  * @brief Flag to control the Receive Multicast Data Thread
70  */
71 static bool gStopMulticast = FALSE;
72
73 /**
74  * @var gStopAccept
75  * @brief Flag to control the Accept Thread
76  */
77 static bool gStopAccept = FALSE;
78
79 typedef struct send_data {
80     char* address;
81     char* data;
82     uint32_t id;
83 } data_t;
84
85 /**
86  @brief Thread context information for unicast, multicast and secured unicast server
87  */
88 typedef struct
89 {
90     bool *stopFlag;
91     CAAdapterServerType_t type;
92 } CAAdapterReceiveThreadContext_t;
93
94 typedef struct
95 {
96     bool *stopFlag;
97 } CAAdapterAcceptThreadContext_t;
98
99 // temp method
100
101 CAResult_t CAEDRGetInterfaceInformation(CALocalConnectivity_t **info)
102 {
103     OIC_LOG_V(DEBUG, TAG, "IN");
104
105     OIC_LOG_V(DEBUG, TAG, "OUT");
106     return CA_STATUS_OK;
107 }
108
109 void CAEDRTerminateClient()
110 {
111     OIC_LOG(DEBUG, TAG, "IN");
112     CAEDRTerminate();
113     OIC_LOG(DEBUG, TAG, "OUT");
114 }
115
116 CAResult_t CAEDRManagerReadData(void)
117 {
118     OIC_LOG_V(DEBUG, TAG, "IN");
119     OIC_LOG_V(DEBUG, TAG, "OUT");
120     return CA_NOT_SUPPORTED;
121 }
122
123 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress, const char *serviceUUID,
124                                       void *data, uint32_t dataLength, uint32_t *sentLength)
125 {
126     OIC_LOG_V(DEBUG, TAG, "IN");
127     CAEDRSendUnicastMessage(remoteAddress, (const char*) data, dataLength);
128     OIC_LOG_V(DEBUG, TAG, "OUT");
129     return CA_STATUS_OK;
130 }
131
132
133 CAResult_t CAEDRClientSendMulticastData(const char *serviceUUID, void *data,
134                                         uint32_t dataLength, uint32_t *sentLength)
135 {
136     OIC_LOG_V(DEBUG, TAG, "IN");
137     CAEDRSendMulticastMessage((const char*) data, dataLength);
138     OIC_LOG_V(DEBUG, TAG, "OUT");
139     return CA_STATUS_OK;
140 }
141
142 void CAEDRClientUnsetCallbacks(void)
143 {
144     OIC_LOG_V(DEBUG, TAG, "IN");
145
146     OIC_LOG_V(DEBUG, TAG, "OUT");
147 }
148
149 void CAEDRClientDisconnectAll(void)
150 {
151     OIC_LOG_V(DEBUG, TAG, "IN");
152
153     OIC_LOG_V(DEBUG, TAG, "OUT");
154 }
155
156 CAResult_t CAEDRGetAdapterEnableState(CABool_t *state)
157 {
158     OIC_LOG_V(DEBUG, TAG, "IN");
159
160     jboolean isAttached = FALSE;
161     JNIEnv* env;
162     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
163     if(res != JNI_OK)
164     {
165         OIC_LOG_V(DEBUG, TAG, "CAEDRGetAdapterEnableState - Could not get JNIEnv pointer");
166         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
167
168         if(res != JNI_OK)
169         {
170             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
171             return;
172         }
173         isAttached = TRUE;
174     }
175     jboolean ret = CAEDRNativeIsEnableBTAdapter(env);
176     if(ret)
177     {
178         *state = CA_TRUE;
179     }
180     else
181     {
182         *state = CA_FALSE;
183     }
184
185     if(isAttached)
186         (*g_jvm)->DetachCurrentThread(g_jvm);
187
188     OIC_LOG_V(DEBUG, TAG, "OUT");
189     return CA_STATUS_OK;
190 }
191
192 ////////////////////////////////////////////////////////////////////////////////////////////////////
193 //FIXME getting context
194
195 void CAEDRJniSetContext(jobject context)
196 {
197     OIC_LOG_V(DEBUG, TAG, "caedrSetObject");
198
199     jboolean isAttached = FALSE;
200     JNIEnv* env;
201     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
202     if(res != JNI_OK)
203     {
204         OIC_LOG_V(DEBUG, TAG, "CAEDRInitialize - Could not get JNIEnv pointer");
205         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
206
207         if(res != JNI_OK)
208         {
209             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
210             return;
211         }
212         isAttached = TRUE;
213     }
214
215     gContext = (*env)->NewGlobalRef(env, context);
216
217     if(isAttached)
218         (*g_jvm)->DetachCurrentThread(g_jvm);
219 }
220
221 void CAEDRCreateJNIInterfaceObject(jobject context)
222 {
223     JNIEnv* env;
224     OIC_LOG_V(DEBUG, TAG, "[EDRCore] CAEDRCreateJNIInterfaceObject");
225
226     if ((*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6) != JNI_OK)
227     {
228         OIC_LOG_V(DEBUG, TAG, "[EDRCore] Could not get JNIEnv pointer");
229         return;
230     }
231
232     //getApplicationContext
233     jclass contextClass = (*env)->FindClass(env, "android/content/Context");
234     if (contextClass == 0)
235     {
236         OIC_LOG_V(DEBUG, TAG, "[EDRCore] Could not get context object class");
237         return;
238     }
239
240     jmethodID getApplicationContextMethod = (*env)->GetMethodID(env, contextClass,
241             "getApplicationContext", "()Landroid/content/Context;");
242     if (getApplicationContextMethod == 0)
243     {
244         OIC_LOG_V(DEBUG, TAG, "[EDRCore] Could not get getApplicationContext method");
245         return;
246     }
247
248     jobject gApplicationContext = (*env)->CallObjectMethod(env, context, getApplicationContextMethod);
249     OIC_LOG_V(DEBUG, TAG, "[WIFICore] Saving Android application context object %p", gApplicationContext);
250
251    //Create WiFiInterface instance
252     jclass WiFiJniInterface = (*env)->FindClass(env, "com/iotivity/jar/CAEDRInterface");
253     if (!WiFiJniInterface)
254     {
255         OIC_LOG_V(DEBUG, TAG, "[EDRCore] Could not get CAWiFiInterface class");
256         return;
257     }
258
259     jmethodID WiFiInterfaceConstructorMethod = (*env)->GetMethodID(env,
260             WiFiJniInterface, "<init>", "(Landroid/content/Context;)V");
261     if (!WiFiInterfaceConstructorMethod)
262     {
263         OIC_LOG_V(DEBUG, TAG, "[EDRCore] Could not get CAWiFiInterface constructor method");
264         return;
265     }
266
267     (*env)->NewObject(env, WiFiJniInterface, WiFiInterfaceConstructorMethod, gApplicationContext);
268     OIC_LOG_V(DEBUG, TAG, "[EDRCore] Create CAWiFiInterface instance");
269     OIC_LOG_V(DEBUG, TAG, "[EDRCore] NewObject Success");
270
271 }
272
273 static void CAEDRDestroyMutex()
274 {
275     OIC_LOG(DEBUG, TAG, "IN");
276
277     if (gMutexUnicastServer)
278     {
279         u_mutex_free(gMutexUnicastServer);
280         gMutexUnicastServer = NULL;
281     }
282
283 #ifdef __WITH_DTLS__
284
285 #endif
286
287     if (gMutexMulticastServer)
288     {
289         u_mutex_free(gMutexMulticastServer);
290         gMutexMulticastServer = NULL;
291     }
292
293     if(gMutexSocketListManager)
294     {
295         u_mutex_free(gMutexSocketListManager);
296         gMutexSocketListManager = NULL;
297     }
298
299     OIC_LOG(DEBUG, TAG, "OUT");
300 }
301
302 static CAResult_t CAEDRCreateMutex()
303 {
304     OIC_LOG(DEBUG, TAG, "IN");
305
306     gMutexUnicastServer = u_mutex_new();
307     if (!gMutexUnicastServer)
308     {
309         OIC_LOG(ERROR, TAG, "Failed to created mutex!");
310         return CA_STATUS_FAILED;
311     }
312
313 #ifdef __WITH_DTLS__
314
315 #endif
316
317     gMutexMulticastServer = u_mutex_new();
318     if (!gMutexMulticastServer)
319     {
320         OIC_LOG(ERROR, TAG, "Failed to created mutex!");
321
322         CAEDRDestroyMutex();
323         return CA_STATUS_FAILED;
324     }
325
326     gMutexSocketListManager = u_mutex_new();
327     if (!gMutexSocketListManager)
328     {
329         OIC_LOG(ERROR, TAG, "Failed to created mutex!");
330
331         CAEDRDestroyMutex();
332         return CA_STATUS_FAILED;
333     }
334
335     OIC_LOG(DEBUG, TAG, "OUT");
336     return CA_STATUS_OK;
337 }
338
339 void CAEDRInitialize(u_thread_pool_t handle)
340 {
341     OIC_LOG(DEBUG, TAG, "CAEDRInitialize");
342
343     gThreadPoolHandle = handle;
344
345     // init mutex
346     CAEDRCreateMutex();
347
348     jboolean isAttached = FALSE;
349     JNIEnv* env;
350     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
351     if(res != JNI_OK)
352     {
353         OIC_LOG_V(DEBUG, TAG, "CAEDRInitialize - Could not get JNIEnv pointer");
354         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
355
356         if(res != JNI_OK)
357         {
358             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
359             return;
360         }
361         isAttached = TRUE;
362     }
363
364     jstring jni_address = CAEDRNativeGetLocalDeviceAddress(env);
365     if(jni_address)
366     {
367         const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
368         OIC_LOG_V(DEBUG, TAG, "My BT Address is %s", localAddress);
369     }
370
371     CAEDRNativeCreateDeviceStateList();
372     CAEDRNativeCreateDeviceSocketList();
373
374     if(isAttached)
375         (*g_jvm)->DetachCurrentThread(g_jvm);
376
377 //    CAEDRCreateJNIInterfaceObject(gContext); /* create java CAEDRInterface instance*/
378
379     OIC_LOG(DEBUG, TAG, "OUT");
380 }
381
382 void CAEDRTerminate()
383 {
384     OIC_LOG(DEBUG, TAG, "CAEDRTerminate");
385
386     jboolean isAttached = FALSE;
387     JNIEnv* env;
388     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
389     if(res != JNI_OK)
390     {
391         OIC_LOG_V(DEBUG, TAG, "CAEDRTerminate - Could not get JNIEnv pointer");
392         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
393
394         if(res != JNI_OK)
395         {
396             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
397             return;
398         }
399         isAttached = TRUE;
400     }
401
402     gStopAccept = TRUE;
403     gStopMulticast = TRUE;
404     gStopUnicast = TRUE;
405
406     if(isAttached)
407         (*g_jvm)->DetachCurrentThread(g_jvm);
408
409     // delete mutex
410     CAEDRDestroyMutex();
411
412     CAEDRNativeRemoveAllDeviceState();
413     CAEDRNativeRemoveAllDeviceSocket(env);
414 }
415
416 void CAEDRCoreJniInit(JNIEnv *env, JavaVM* jvm)
417 {
418     OIC_LOG_V(DEBUG, TAG, "CAEdrClientJniInit");
419     g_jvm = jvm;
420
421     CAEDRServerJniInit(env, jvm);
422 }
423
424 int32_t CAEDRSendUnicastMessage(const char* address, const char* data, uint32_t dataLen)
425 {
426     OIC_LOG_V(DEBUG, TAG, "CAEDRSendUnicastMessage(%s, %s)", address, data);
427
428     CAEDRSendUnicastMessageImpl(address, data, dataLen);
429     return 0;
430 }
431
432 int32_t CAEDRSendMulticastMessage(const char* data, uint32_t dataLen)
433 {
434     OIC_LOG_V(DEBUG, TAG, "CAEDRSendMulticastMessage(%s)", data);
435
436     jboolean isAttached = FALSE;
437     JNIEnv* env;
438     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
439     if(res != JNI_OK)
440     {
441         OIC_LOG_V(DEBUG, TAG, "CAEDRSendMulticastMessage - Could not get JNIEnv pointer");
442         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
443
444         if(res != JNI_OK)
445         {
446             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
447             return 0;
448         }
449         isAttached = TRUE;
450     }
451
452     CAEDRSendMulticastMessageImpl(env, data, dataLen);
453
454     OIC_LOG_V(DEBUG, TAG, "sent data");
455
456     if(isAttached)
457     {
458         OIC_LOG_V(DEBUG, TAG, "DetachCurrentThread");
459 //        (*g_jvm)->DetachCurrentThread(g_jvm);
460     }
461
462     OIC_LOG_V(DEBUG, TAG, "OUT - CAEDRSendMulticastMessage");
463     return 1;
464 }
465
466 CAResult_t CAEDRGetInterfaceInfo(char **address)
467 {
468     CAEDRGetLocalAddress(address);
469     return CA_STATUS_OK;
470 }
471
472 void CAEDRGetLocalAddress(char** address)
473 {
474     jboolean isAttached = FALSE;
475     JNIEnv* env;
476     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
477     if(res != JNI_OK)
478     {
479         OIC_LOG_V(DEBUG, TAG, "CAEDRGetLocalAddress - Could not get JNIEnv pointer");
480         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
481         if(res != JNI_OK)
482         {
483             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
484             return;
485         }
486         isAttached = TRUE;
487     }
488
489     jstring jni_address = CAEDRNativeGetLocalDeviceAddress(env);
490     if(jni_address)
491     {
492         const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
493         *address = (char*)OICMalloc(strlen(localAddress) + 1);
494         if (*address == NULL)
495         {
496             return;
497         }
498         memcpy(*address, localAddress, strlen(localAddress));
499     }
500
501     OIC_LOG_V(DEBUG, TAG, "Local Address : %s", *address);
502     if(isAttached)
503         (*g_jvm)->DetachCurrentThread(g_jvm);
504 }
505
506 int32_t CAEDRSendUnicastMessageImpl(const char* address, const char* data, uint32_t dataLen)
507 {
508     OIC_LOG_V(DEBUG, TAG, "CAEDRSendUnicastMessageImpl, address: %s, data: %s", address, data);
509
510     jboolean isAttached = FALSE;
511     JNIEnv* env;
512     jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
513     if(res != JNI_OK)
514     {
515         OIC_LOG_V(DEBUG, TAG, "CAEDRSendUnicastMessageImpl - Could not get JNIEnv pointer");
516         res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
517         if(res != JNI_OK)
518         {
519             OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
520             return 0;
521         }
522         isAttached = TRUE;
523     }
524
525     OIC_LOG(DEBUG, TAG, "[EDR][Native] set byteArray for data");
526     if(gSendBuffer)
527     {
528         (*env)->DeleteGlobalRef(env, gSendBuffer);
529     }
530     jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
531     (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*)data);
532     gSendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
533
534     // get bonded device list
535     jobjectArray jni_arrayPairedDevices = CAEDRNativeGetBondedDevices(env);
536     if(!jni_arrayPairedDevices)
537     {
538         OIC_LOG_V(DEBUG, TAG, "[EDR][Native] jni_arrayPairedDevices is empty");
539         return 0;
540     }
541     // Get information from array of devices
542     jsize length = (*env)->GetArrayLength(env, jni_arrayPairedDevices);
543     jsize i;
544     for( i = 0 ; i < length ; i++ )
545     {
546         OIC_LOG_V(DEBUG, TAG, "[EDR][Native] start to check device");
547         // get name, address from BT device
548         jobject j_obj_device = (*env)->GetObjectArrayElement(env, jni_arrayPairedDevices, i);
549
550         jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
551         jmethodID j_mid_getName = (*env)->GetMethodID(env, jni_cid_BTDevice, "getName", "()Ljava/lang/String;");
552         jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress", "()Ljava/lang/String;");
553
554         jstring j_str_name = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getName);
555
556         if(j_str_name)
557         {
558             const char * name = (*env)->GetStringUTFChars(env, j_str_name, NULL);
559             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] getBondedDevices: ~~device name is %s", name);
560             (*env)->ReleaseStringUTFChars(env, j_str_name, name);
561         }
562
563         jstring j_str_address = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getAddress);
564         const char * remoteAddress = (*env)->GetStringUTFChars(env, j_str_address, NULL);
565         OIC_LOG_V(DEBUG, TAG, "[EDR][Native] getBondedDevices: ~~device address is %s", remoteAddress);
566
567         // find address
568         if(!strcmp(remoteAddress, address))
569         {
570             CAEDRNativeSendData(env, remoteAddress, data, i);
571         }
572     }
573
574     if(isAttached)
575         (*g_jvm)->DetachCurrentThread(g_jvm);
576
577     return 1;
578 }
579
580 int32_t CAEDRSendMulticastMessageImpl(JNIEnv *env, const char* data, uint32_t dataLen)
581 {
582     OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s, %d", data, dataLen);
583
584     // get bonded device list
585     jobjectArray jni_arrayPairedDevices = CAEDRNativeGetBondedDevices(env);
586     if(!jni_arrayPairedDevices)
587     {
588         OIC_LOG_V(DEBUG, TAG, "[EDR][Native] jni_arrayPairedDevices is empty");
589         return 0;
590     }
591     // Get information from array of devices
592     jsize length = (*env)->GetArrayLength(env, jni_arrayPairedDevices);
593     jsize i;
594     for( i = 0 ; i < length ; i++ )
595     {
596         // get name, address from BT device
597         jobject j_obj_device = (*env)->GetObjectArrayElement(env, jni_arrayPairedDevices, i);
598
599         jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
600         jmethodID j_mid_getName = (*env)->GetMethodID(env, jni_cid_BTDevice, "getName", "()Ljava/lang/String;");
601         jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress", "()Ljava/lang/String;");
602
603         jstring j_str_name = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getName);
604
605         if(j_str_name)
606         {
607             const char * name = (*env)->GetStringUTFChars(env, j_str_name, NULL);
608             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] getBondedDevices: ~~device name is %s", name);
609             (*env)->ReleaseStringUTFChars(env, j_str_name, name);
610         }
611
612         jstring j_str_address = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getAddress);
613         const char * remoteAddress = (*env)->GetStringUTFChars(env, j_str_address, NULL);
614         OIC_LOG_V(DEBUG, TAG, "[EDR][Native] getBondedDevices: ~~device address is %s", remoteAddress);
615
616         // find address
617         CAEDRNativeSendData(env, remoteAddress, data, i);
618     }
619
620     return 1;
621 }
622
623 /**
624  * EDR Method
625  */
626 void CAEDRNativeSendData(JNIEnv *env, const char* address, const char* data, uint32_t id)
627 {
628     OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData logic start");
629
630     if(STATE_DISCONNECTED == CAEDRIsConnectedDevice(address))
631     {
632         // connect before send data
633         OIC_LOG(DEBUG, TAG, "[EDR][Native] connect before send data");
634
635         if(NULL == address)
636         {
637             OIC_LOG(DEBUG, TAG, "[EDR][Native] remote address is empty");
638             return;
639         }
640         else
641         {
642             CAEDRNativeConnect(env, address, id);
643         }
644     }
645
646     if(STATE_CONNECTED == CAEDRIsConnectedDevice(address))
647     {
648         if(!((*env)->ExceptionCheck(env)))
649         {
650             jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
651             if(!jni_cid_BTsocket)
652             {
653                 OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: jni_cid_BTsocket is null");
654                 return;
655             }
656
657             jmethodID jni_mid_getOutputStream = (*env)->GetMethodID(env, jni_cid_BTsocket, "getOutputStream", "()Ljava/io/OutputStream;");
658             if(!jni_mid_getOutputStream)
659             {
660                 OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: jni_mid_getOutputStream is null");
661                 return;
662             }
663             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btSendData: Get MethodID for i/o stream..%d", id);
664
665             jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
666             if(!jni_obj_socket)
667             {
668                 OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: jni_socket is not available");
669                 return;
670             }
671
672             jobject jni_obj_outputStream = (*env)->CallObjectMethod(env, jni_obj_socket, jni_mid_getOutputStream);
673             if(!jni_obj_outputStream)
674             {
675                 OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: jni_obj_outputStream is null");
676                 return;
677             }
678
679             OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: ready outputStream..");
680
681             jclass jni_cid_OutputStream = (*env)->FindClass(env, "java/io/OutputStream");
682             if(!jni_cid_OutputStream)
683             {
684                 OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: jni_cid_OutputStream is null");
685                 return;
686             }
687
688             jmethodID jni_mid_write = (*env)->GetMethodID(env, jni_cid_OutputStream, "write", "([BII)V");
689             if(!jni_mid_write)
690             {
691                 OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: jni_mid_write is null");
692                 return;
693             }
694
695 //            const char* tmpData = "HelloWorldHelloWorld..";
696 //            size_t nread = 20;
697 //            jbyteArray jni_arr = (*env)->NewByteArray(env, nread);
698 //            (*env)->SetByteArrayRegion(env, jni_arr, 0, nread, (jbyte*)tmpData);
699
700             jbyteArray jbuf;
701             int length = strlen(data);
702             jbuf = (*env)->NewByteArray(env, length);
703             (*env)->SetByteArrayRegion(env, jbuf, 0, length, (jbyte*)data);
704
705             (*env)->CallVoidMethod(env, jni_obj_outputStream, jni_mid_write, jbuf, (jint)0, (jint)length);
706
707             if((*env)->ExceptionCheck(env))
708             {
709                 OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: Write Error!!!");
710                 (*env)->ExceptionDescribe(env);
711                 (*env)->ExceptionClear(env);
712                 return;
713             }
714
715             OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: Write Success");
716
717             // remove socket to list
718             CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);
719
720             // update state
721             CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
722         }
723         else
724         {
725             (*env)->ExceptionDescribe(env);
726             (*env)->ExceptionClear(env);
727             OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: error!!");
728             return;
729         }
730     }
731     else
732     {
733         OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: BT connection is not completed!!");
734     }
735 }
736
737 void CAEDRNativeConnect(JNIEnv *env, const char* address, uint32_t id)
738 {
739     OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect..");
740
741     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
742     if(!jni_cid_BTAdapter)
743     {
744         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_cid_BTAdapter is null");
745         return;
746     }
747
748     // get BTadpater
749     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
750     if(!jni_mid_getDefaultAdapter)
751     {
752         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_mid_getDefaultAdapter is null");
753         return;
754     }
755
756     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
757     if(!jni_obj_BTAdapter)
758     {
759         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_obj_BTAdapter is null");
760         return;
761     }
762
763     // get remote bluetooth device
764     jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getRemoteDevice",
765          "(Ljava/lang/String;)Landroid/bluetooth/BluetoothDevice;");
766     if(!jni_mid_getRemoteDevice)
767     {
768         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_mid_getRemoteDevice is null");
769         return;
770     }
771
772     //jstring jni_address = (*env)->NewStringUTF(env, "B8:5E:7B:54:52:1C");
773     jstring jni_address = (*env)->NewStringUTF(env, address);
774     jobject jni_obj_remoteBTDevice = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getRemoteDevice, jni_address);
775     if(!jni_obj_remoteBTDevice)
776     {
777         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_obj_remoteBTDevice is null");
778         return;
779     }
780
781     // get create Rfcomm Socket method ID
782     jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
783     if(!jni_cid_BluetoothDevice)
784     {
785         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_cid_BluetoothDevice is null");
786         return;
787     }
788
789     jmethodID jni_mid_createSocket = (*env)->GetMethodID(env, jni_cid_BluetoothDevice,
790          "createInsecureRfcommSocketToServiceRecord","(Ljava/util/UUID;)Landroid/bluetooth/BluetoothSocket;");
791     if(!jni_mid_createSocket) {
792         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_mid_createSocket is null");
793         return;
794     }
795
796     // createInsecureRfcommSocketToServiceRecord / createRfcommSocketToServiceRecord
797     // setting UUID
798     jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
799     if(!jni_cid_uuid)
800     {
801         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_cid_uuid is null");
802         return;
803     }
804
805     jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString",
806             "(Ljava/lang/String;)Ljava/util/UUID;");
807     if(!jni_mid_fromString)
808     {
809         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_mid_fromString is null");
810         return;
811     }
812
813     jstring jni_uuid = (*env)->NewStringUTF(env, "00000000-0000-0000-0000-0000cdab0000");
814     jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString, jni_uuid);
815     if(!jni_obj_uuid)
816     {
817         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_obj_uuid is null");
818         return;
819     }
820     // create socket
821     jobject jni_obj_BTSocket = (*env)->CallObjectMethod(env, jni_obj_remoteBTDevice, jni_mid_createSocket, jni_obj_uuid);
822     if(!jni_obj_BTSocket)
823     {
824         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_obj_BTSocket is null");
825         return;
826     }
827
828     // connect
829     jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
830     if(!jni_cid_BTSocket)
831     {
832         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_cid_BTSocket is null");
833         return;
834     }
835
836     jmethodID jni_mid_connect = (*env)->GetMethodID(env, jni_cid_BTSocket, "connect", "()V");
837     if(!jni_mid_connect)
838     {
839         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: jni_mid_connect is null");
840         return;
841     }
842
843     OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: initiating connection...");
844     (*env)->CallVoidMethod(env, jni_obj_BTSocket, jni_mid_connect);
845
846     if((*env)->ExceptionCheck(env))
847     {
848         OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: Connect is Failed!!!");
849         (*env)->ExceptionDescribe(env);
850         (*env)->ExceptionClear(env);
851         return;
852     }
853
854     // set socket to list
855     jobject jni_socket = (*env)->NewGlobalRef(env, jni_obj_BTSocket);
856     CAEDRNativeAddDeviceSocketToList(env, jni_socket);
857
858     // update state
859     CAEDRUpdateDeviceState(STATE_CONNECTED, address);
860
861     OIC_LOG(DEBUG, TAG, "[EDR][Native] btConnect: connected");
862 }
863
864 void CAEDRNativeSocketClose(JNIEnv *env, const char* address, uint32_t id)
865 {
866
867     jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
868     if(!jni_cid_BTSocket)
869     {
870         OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
871         return;
872     }
873
874     jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
875     if(!jni_mid_close)
876     {
877         OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_mid_close is null");
878         return;
879     }
880
881     jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
882     if(!jni_obj_socket)
883     {
884         OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_obj_socket is not available");
885         return;
886     }
887
888     (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
889
890     if((*env)->ExceptionCheck(env))
891     {
892         OIC_LOG(DEBUG, TAG, "[EDR][Native] close: close is Failed!!!");
893         (*env)->ExceptionDescribe(env);
894         (*env)->ExceptionClear(env);
895         return;
896     }
897
898     // remove socket to list
899     CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);
900
901     // update state
902     CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
903
904     OIC_LOG(DEBUG, TAG, "[EDR][Native] close: disconnected");
905 }
906
907
908 /**
909  * BT State List
910  */
911 void CAEDRNativeCreateDeviceStateList()
912 {
913     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceStateList");
914
915     // create new object array
916     if (gdeviceStateList == NULL)
917     {
918         OIC_LOG_V(DEBUG, TAG, "Create device list");
919
920         gdeviceStateList = u_arraylist_create();
921     }
922 }
923
924 void CAEDRUpdateDeviceState(uint32_t state, const char* address)
925 {
926     state_t *newstate = (state_t*) OICMalloc( sizeof(state_t) );
927     memset(newstate->address, 0, strlen(newstate->address));
928     strcpy(newstate->address, address);
929     newstate->state = state;
930
931     CAEDRNativeAddDeviceStateToList(newstate);
932 }
933
934 void CAEDRNativeAddDeviceStateToList(state_t* state)
935 {
936     if(!state)
937     {
938         OIC_LOG(DEBUG, TAG, "[EDR][Native] device is null");
939         return;
940     }
941
942     if(!gdeviceStateList)
943     {
944         OIC_LOG(DEBUG, TAG, "[EDR][Native] gdevice_list is null");
945         return;
946     }
947
948     if(CAEDRNativeIsDeviceInList(state->address)) {
949         CAEDRNativeRemoveDevice(state->address); // delete previous state for update new state
950     }
951     u_arraylist_add(gdeviceStateList, state);          // update new state
952     OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d", state->state);
953 }
954
955 jboolean CAEDRNativeIsDeviceInList(const char* remoteAddress){
956
957     jint index;
958     for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
959     {
960         state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
961         if(!state)
962         {
963             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
964             return TRUE;
965         }
966
967         if(!strcmp(remoteAddress, state->address))
968         {
969             OIC_LOG_V(DEBUG, TAG, "the device is already set");
970             return TRUE;
971         }
972         else
973         {
974             continue;
975         }
976     }
977
978     OIC_LOG_V(DEBUG, TAG, "there are no the device in list.");
979     return FALSE;
980 }
981
982 void CAEDRNativeRemoveAllDeviceState()
983 {
984     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
985
986     if(!gdeviceStateList)
987     {
988         OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
989         return;
990     }
991
992     jint index;
993     for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
994     {
995         state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
996         if(!state)
997         {
998             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
999             continue;
1000         }
1001         OICFree(state);
1002     }
1003
1004     OICFree(gdeviceStateList);
1005     gdeviceStateList = NULL;
1006     return;
1007 }
1008
1009 void CAEDRNativeRemoveDevice(const char* remoteAddress)
1010 {
1011     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceforStateList");
1012
1013     if(!gdeviceStateList)
1014     {
1015         OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
1016         return;
1017     }
1018
1019     jint index;
1020     for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
1021     {
1022         state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
1023         if(!state)
1024         {
1025             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
1026             continue;
1027         }
1028
1029         if(!strcmp(state->address, remoteAddress))
1030         {
1031             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove state : %s", remoteAddress);
1032             OICFree(state);
1033
1034             CAEDRReorderingDeviceList(index);
1035             break;
1036         }
1037     }
1038     return;
1039 }
1040
1041 jboolean CAEDRIsConnectedDevice(const char* remoteAddress)
1042 {
1043     OIC_LOG_V(DEBUG, TAG, "CAEDRIsConnectedDevice");
1044
1045     if(!gdeviceStateList)
1046     {
1047         OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
1048         return FALSE;
1049     }
1050
1051     jint index;
1052     for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
1053     {
1054         state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
1055         if(!state)
1056         {
1057             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
1058             continue;
1059         }
1060
1061         if(!strcmp(state->address, remoteAddress))
1062         {
1063             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] check whether it is connected or not");
1064
1065             return state->state;
1066         }
1067     }
1068     return FALSE;
1069 }
1070
1071 void CAEDRReorderingDeviceList(uint32_t index)
1072 {
1073     if (index >= gdeviceStateList->length)
1074     {
1075         return;
1076     }
1077
1078     if (index < gdeviceStateList->length - 1)
1079     {
1080         memmove(&gdeviceStateList->data[index], &gdeviceStateList->data[index + 1],
1081                 (gdeviceStateList->length - index - 1) * sizeof(void *));
1082     }
1083
1084     gdeviceStateList->size--;
1085     gdeviceStateList->length--;
1086 }
1087
1088 /**
1089  * Device Socket Object List
1090  */
1091 void CAEDRNativeCreateDeviceSocketList()
1092 {
1093     OIC_LOG(DEBUG, TAG, "[BLE][Native] CAEDRNativeCreateDeviceSocketList");
1094
1095     // create new object array
1096     if (gdeviceObjectList == NULL)
1097     {
1098         OIC_LOG_V(DEBUG, TAG, "Create Device object list");
1099
1100         gdeviceObjectList = u_arraylist_create();
1101     }
1102 }
1103
1104 void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
1105 {
1106     OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeAddDeviceobjToList");
1107
1108     if(!deviceSocket)
1109     {
1110         OIC_LOG(DEBUG, TAG, "[BLE][Native] Device is null");
1111         return;
1112     }
1113
1114     if(!gdeviceObjectList)
1115     {
1116         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1117         return;
1118     }
1119
1120     jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
1121     if(!jni_remoteAddress)
1122     {
1123         OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1124         return;
1125     }
1126
1127     u_mutex_lock(gMutexSocketListManager);
1128
1129     const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1130
1131     if(!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
1132     {
1133         jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
1134         u_arraylist_add(gdeviceObjectList, gDeviceSocker);
1135         OIC_LOG_V(DEBUG, TAG, "Set Socket Object to Array");
1136     }
1137
1138     u_mutex_unlock(gMutexSocketListManager);
1139 }
1140
1141 jboolean CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
1142 {
1143     OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsDeviceObjInList");
1144
1145     jint index;
1146     for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
1147     {
1148
1149         jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
1150         if(!jarrayObj)
1151         {
1152             OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1153             return TRUE;
1154         }
1155
1156         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
1157         if(!jni_setAddress)
1158         {
1159             OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1160             return TRUE;
1161         }
1162
1163         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1164
1165         if(!strcmp(remoteAddress, setAddress))
1166         {
1167             OIC_LOG_V(DEBUG, TAG, "the device is already set");
1168             return TRUE;
1169         }
1170         else
1171         {
1172             continue;
1173         }
1174     }
1175
1176     OIC_LOG_V(DEBUG, TAG, "there are no the Device obejct in list. we can add");
1177     return FALSE;
1178 }
1179
1180 void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
1181 {
1182     OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
1183
1184     if(!gdeviceObjectList)
1185     {
1186         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1187         return;
1188     }
1189
1190     jint index;
1191     for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
1192     {
1193         jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
1194         if(!jarrayObj)
1195         {
1196             OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1197             return;
1198         }
1199         (*env)->DeleteGlobalRef(env, jarrayObj);
1200     }
1201
1202     OICFree(gdeviceObjectList);
1203     gdeviceObjectList = NULL;
1204     return;
1205 }
1206
1207 void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
1208 {
1209     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
1210
1211     if(!gdeviceObjectList)
1212     {
1213         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1214         return;
1215     }
1216
1217     u_mutex_lock(gMutexSocketListManager);
1218
1219     jint index;
1220     for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
1221     {
1222         jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
1223         if(!jarrayObj)
1224         {
1225             OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1226             continue;
1227         }
1228
1229         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
1230         if(!jni_setAddress)
1231         {
1232             OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1233             continue;
1234         }
1235         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1236
1237         jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
1238         if(!jni_remoteAddress)
1239         {
1240             OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1241             continue;
1242         }
1243         const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1244
1245         if(!strcmp(setAddress, remoteAddress))
1246         {
1247             OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
1248             (*env)->DeleteGlobalRef(env, jarrayObj);
1249
1250             CAEDRReorderingDeviceSocketList(index);
1251             break;
1252         }
1253     }
1254     u_mutex_unlock(gMutexSocketListManager);
1255
1256     OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
1257     return;
1258 }
1259
1260 jobject CAEDRNativeGetDeviceSocket(uint32_t idx)
1261 {
1262     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
1263
1264     if(idx < 0)
1265     {
1266         OIC_LOG(DEBUG, TAG, "[BLE][Native] index is not available");
1267         return NULL;
1268     }
1269
1270     if(!gdeviceObjectList)
1271     {
1272         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1273         return NULL;
1274     }
1275
1276     jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, idx);
1277     if(!jarrayObj)
1278     {
1279         OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is not available");
1280         return NULL;
1281     }
1282     return jarrayObj;
1283 }
1284
1285 uint32_t CAEDRGetSocketListLength()
1286 {
1287     if(!gdeviceObjectList)
1288     {
1289         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1290         return 0;
1291     }
1292
1293     uint32_t length = u_arraylist_length(gdeviceObjectList);
1294
1295     return length;
1296 }
1297
1298 void CAEDRReorderingDeviceSocketList(uint32_t index)
1299 {
1300     if (index >= gdeviceObjectList->length)
1301     {
1302         return;
1303     }
1304
1305     if (index < gdeviceObjectList->length - 1)
1306     {
1307         memmove(&gdeviceObjectList->data[index], &gdeviceObjectList->data[index + 1],
1308                 (gdeviceObjectList->length - index - 1) * sizeof(void *));
1309     }
1310
1311     gdeviceObjectList->size--;
1312     gdeviceObjectList->length--;
1313 }
1314
1315 void CAEDRInitializeClient(u_thread_pool_t handle)
1316 {
1317     OIC_LOG(DEBUG, TAG, "IN");
1318     CAEDRInitialize(handle);
1319     OIC_LOG(DEBUG, TAG, "OUT");
1320 }
1321