1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
26 #include "caleclient.h"
27 #include "caleserver.h"
28 #include "caleutils.h"
29 #include "caleinterface.h"
30 #include "caadapterutils.h"
33 #include "oic_malloc.h"
34 #include "cathreadpool.h" /* for thread pool */
36 #include "uarraylist.h"
37 #include "org_iotivity_ca_CaLeClientInterface.h"
39 #define TAG PCF("CA_LE_CLIENT")
41 static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
42 static const char CLASSPATH_BT_ADAPTER[] = "android/bluetooth/BluetoothAdapter";
43 static const char CLASSPATH_BT_UUID[] = "java/util/UUID";
44 static const char CLASSPATH_BT_GATT[] = "android/bluetooth/BluetoothGatt";
47 static u_arraylist_t *g_deviceList = NULL; // device list to have same UUID
48 static u_arraylist_t *g_gattObjectList = NULL;
49 static u_arraylist_t *g_deviceStateList = NULL;
51 static CAPacketReceiveCallback g_packetReceiveCallback = NULL;
52 static ca_thread_pool_t g_threadPoolHandle = NULL;
53 static jobject g_leScanCallback = NULL;
54 static jobject g_leGattCallback = NULL;
55 static jobject g_context = NULL;
56 static jobjectArray g_uuidList = NULL;
58 // it will be prevent to start send logic when adapter has stopped.
59 static bool g_isStartedLEClient = false;
60 static bool g_isStartedMulticastServer = false;
61 static bool g_isStartedScan = false;
63 static jbyteArray g_sendBuffer = NULL;
64 static uint32_t g_targetCnt = 0;
65 static uint32_t g_currentSentCnt = 0;
66 static bool g_isFinishedSendData = false;
67 static ca_mutex g_SendFinishMutex = false;
68 static ca_mutex g_threadMutex = NULL;
69 static ca_cond g_threadCond = NULL;
71 static bool g_isRequestedSend = false;
72 static bool g_isReceivedWriteCB = false;
73 static ca_mutex g_writeCharacteristicCBMutex = false;
74 static ca_mutex g_theSendRequestMutex = false;
75 static ca_mutex g_threadSendCBMutex = NULL;
76 static ca_cond g_threadSendCBCond = NULL;
78 static ca_mutex g_threadSendMutex = NULL;
79 static ca_cond g_threadSendCond = NULL;
81 static ca_mutex g_bleReqRespClientCbMutex = NULL;
82 static ca_mutex g_bleServerBDAddressMutex = NULL;
84 static ca_mutex g_deviceListMutex = NULL;
85 static ca_mutex g_gattObjectMutex = NULL;
86 static ca_mutex g_deviceStateListMutex = NULL;
88 static CABLEClientDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL;
91 void CALEClientJniInit()
93 OIC_LOG(DEBUG, TAG, "CALEClientJniInit");
94 g_jvm = (JavaVM*) CANativeJNIGetJavaVM();
97 void CALEClientJNISetContext()
99 OIC_LOG(DEBUG, TAG, "CALEClientJNISetContext");
100 g_context = (jobject) CANativeJNIGetContext();
103 CAResult_t CALECreateJniInterfaceObject()
105 OIC_LOG(DEBUG, TAG, "CALECreateJniInterfaceObject");
109 OIC_LOG(ERROR, TAG, "g_context is null");
110 return CA_STATUS_FAILED;
115 OIC_LOG(ERROR, TAG, "g_jvm is null");
116 return CA_STATUS_FAILED;
119 bool isAttached = false;
121 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
124 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
125 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
129 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
130 return CA_STATUS_FAILED;
135 jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface");
136 if (!jni_LEInterface)
138 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface class");
142 jmethodID LeInterfaceConstructorMethod = (*env)->GetMethodID(env, jni_LEInterface, "<init>",
143 "(Landroid/content/Context;)V");
144 if (!LeInterfaceConstructorMethod)
146 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface constructor method");
150 (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, g_context);
151 OIC_LOG(DEBUG, TAG, "Create instance for CaLeClientInterface");
155 (*g_jvm)->DetachCurrentThread(g_jvm);
164 (*g_jvm)->DetachCurrentThread(g_jvm);
167 return CA_STATUS_FAILED;
170 CAResult_t CALEClientInitialize(ca_thread_pool_t handle)
172 OIC_LOG(DEBUG, TAG, "CALEClientInitialize");
178 OIC_LOG(ERROR, TAG, "g_jvm is null");
179 return CA_STATUS_FAILED;
182 bool isAttached = false;
184 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
187 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
188 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
192 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
193 return CA_STATUS_FAILED;
198 CAResult_t ret = CALECheckPlatformVersion(env, 18);
199 if (CA_STATUS_OK != ret)
201 OIC_LOG(ERROR, TAG, "it is not supported");
205 (*g_jvm)->DetachCurrentThread(g_jvm);
211 g_threadPoolHandle = handle;
213 ret = CALEClientInitGattMutexVaraibles();
214 if (CA_STATUS_OK != ret)
216 OIC_LOG(ERROR, TAG, "CALEClientInitGattMutexVaraibles has failed!");
217 CALEClientTerminateGattMutexVariables();
221 (*g_jvm)->DetachCurrentThread(g_jvm);
227 // init mutex for send logic
228 g_threadCond = ca_cond_new();
229 g_threadSendCond = ca_cond_new();
230 g_threadSendCBCond = ca_cond_new();
232 CALEClientCreateDeviceList();
233 CALEClientJNISetContext();
235 ret = CALEClientCreateUUIDList();
236 if (CA_STATUS_OK != ret)
238 OIC_LOG(ERROR, TAG, "CALEClientCreateUUIDList has failed");
242 (*g_jvm)->DetachCurrentThread(g_jvm);
248 ret = CALECreateJniInterfaceObject(); /* create java caleinterface instance*/
249 if (CA_STATUS_OK != ret)
251 OIC_LOG(ERROR, TAG, "CALECreateJniInterfaceObject has failed");
255 (*g_jvm)->DetachCurrentThread(g_jvm);
260 g_isStartedLEClient = true;
265 void CALEClientTerminate()
267 OIC_LOG(DEBUG, TAG, "CALEClientTerminate");
271 OIC_LOG(ERROR, TAG, "g_jvm is null");
275 bool isAttached = false;
277 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
280 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
281 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
285 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
291 if (g_leScanCallback)
293 (*env)->DeleteGlobalRef(env, g_leScanCallback);
296 if (g_leGattCallback)
298 (*env)->DeleteGlobalRef(env, g_leGattCallback);
303 (*env)->DeleteGlobalRef(env, g_sendBuffer);
308 (*env)->DeleteGlobalRef(env, g_uuidList);
311 CAResult_t ret = CALEClientRemoveAllDeviceState();
312 if (CA_STATUS_OK != ret)
314 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
317 ret = CALEClientRemoveAllScanDevices(env);
318 if (CA_STATUS_OK != ret)
320 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
323 ret = CALEClientRemoveAllGattObjs(env);
324 if (CA_STATUS_OK != ret)
326 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
329 g_isStartedMulticastServer = false;
330 g_isStartedScan = false;
331 CALEClientSetSendFinishFlag(false);
332 CALEClientSetTheSendRequestFlag(false);
333 CALEClientSetWriteCharacteristicCBFlag(false);
335 CALEClientTerminateGattMutexVariables();
337 ca_cond_free(g_threadCond);
338 ca_cond_free(g_threadSendCond);
339 ca_cond_free(g_threadSendCBCond);
343 (*g_jvm)->DetachCurrentThread(g_jvm);
347 void CALEClientSendFinish(JNIEnv *env, jobject gatt)
349 OIC_LOG(DEBUG, TAG, "CALEClientSendFinish");
350 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
354 CAResult_t res = CALEClientDisconnect(env, gatt);
355 if (CA_STATUS_OK != res)
357 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
360 CALEClientUpdateSendCnt(env);
363 CAResult_t CALEClientSendUnicastMessage(const char* address, const char* data,
364 const uint32_t dataLen)
366 OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessage(%s, %s)", address, data);
367 VERIFY_NON_NULL(address, TAG, "address is null");
368 VERIFY_NON_NULL(data, TAG, "data is null");
370 return CALEClientSendUnicastMessageImpl(address, data, dataLen);
373 CAResult_t CALEClientSendMulticastMessage(const char* data, const uint32_t dataLen)
375 OIC_LOG_V(DEBUG, TAG, "CALEClientSendMulticastMessage(%s)", data);
376 VERIFY_NON_NULL(data, TAG, "data is null");
380 OIC_LOG(ERROR, TAG, "g_jvm is null");
381 return CA_STATUS_FAILED;
384 bool isAttached = false;
386 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
389 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
390 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
394 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
395 return CA_STATUS_FAILED;
400 CAResult_t ret = CALEClientSendMulticastMessageImpl(env, data, dataLen);
401 if (CA_STATUS_OK != ret)
403 OIC_LOG(ERROR, TAG, "CALEClientSendMulticastMessageImpl has failed");
408 (*g_jvm)->DetachCurrentThread(g_jvm);
414 CAResult_t CALEClientStartUnicastServer(const char* address)
416 OIC_LOG_V(DEBUG, TAG, "it is not needed in this platform (%s)", address);
418 return CA_NOT_SUPPORTED;
421 CAResult_t CALEClientStartMulticastServer()
423 OIC_LOG(DEBUG, TAG, "CALEClientStartMulticastServer");
425 if (g_isStartedMulticastServer)
427 OIC_LOG(ERROR, TAG, "server is already started..it will be skipped");
428 return CA_STATUS_FAILED;
433 OIC_LOG(ERROR, TAG, "g_jvm is null");
434 return CA_STATUS_FAILED;
437 bool isAttached = false;
439 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
442 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
443 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
447 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
448 return CA_STATUS_FAILED;
453 g_isStartedMulticastServer = true;
454 CAResult_t ret = CALEClientStartScan();
455 if (CA_STATUS_OK != ret)
457 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
462 (*g_jvm)->DetachCurrentThread(g_jvm);
468 void CALEClientStopUnicastServer()
470 OIC_LOG(DEBUG, TAG, "CALEClientStopUnicastServer");
473 void CALEClientStopMulticastServer()
475 OIC_LOG(DEBUG, TAG, "CALEClientStopMulticastServer");
476 g_isStartedMulticastServer = false;
477 CAResult_t res = CALEClientStopScan();
478 if (CA_STATUS_OK != res)
480 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
485 void CALEClientSetCallback(CAPacketReceiveCallback callback)
487 g_packetReceiveCallback = callback;
490 CAResult_t CALEClientGetInterfaceInfo(char **address)
492 OIC_LOG(INFO, TAG, "CALEClientGetInterfaceInfo is not supported");
493 return CA_NOT_SUPPORTED;
496 CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const char* data,
497 const uint32_t dataLen)
499 OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessageImpl, address: %s, data: %s", address,
501 VERIFY_NON_NULL(address, TAG, "address is null");
502 VERIFY_NON_NULL(data, TAG, "data is null");
506 OIC_LOG(ERROR, TAG, "g_jvm is null");
507 return CA_STATUS_FAILED;
510 bool isAttached = false;
512 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
515 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
516 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
519 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
520 return CA_STATUS_FAILED;
525 ca_mutex_lock(g_threadSendMutex);
527 CAResult_t ret = CA_STATUS_OK;
528 if (g_context && g_deviceList)
530 uint32_t length = u_arraylist_length(g_deviceList);
531 for (uint32_t index = 0; index < length; index++)
533 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
536 OIC_LOG(ERROR, TAG, "jarrayObj is null");
540 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
543 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
547 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
550 OIC_LOG(ERROR, TAG, "setAddress is null");
554 OIC_LOG_V(DEBUG, TAG, "remote device address is %s", setAddress);
556 if (!strcmp(setAddress, address))
558 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
560 // connect to gatt server
561 ret = CALEClientStopScan();
562 if (CA_STATUS_OK != ret)
564 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
570 (*env)->DeleteGlobalRef(env, g_sendBuffer);
572 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
573 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
574 g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
576 ret = CALEClientSendData(env, jarrayObj);
577 if (CA_STATUS_OK != ret)
579 OIC_LOG(ERROR, TAG, "CALEClientSendData in unicast is failed");
584 CALEClientSetTheSendRequestFlag(true);
585 ca_cond_signal(g_threadSendCBCond);
587 if (!g_isReceivedWriteCB)
589 OIC_LOG(INFO, TAG, "wait..(unicast)");
590 ca_cond_wait(g_threadSendCond, g_threadSendMutex);
594 CALEClientSetWriteCharacteristicCBFlag(false);
598 OIC_LOG(INFO, TAG, "wake up");
601 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
607 (*g_jvm)->DetachCurrentThread(g_jvm);
610 ret = CALECheckSendState(address);
611 if(CA_STATUS_OK != ret)
613 OIC_LOG(ERROR, TAG, "send has failed");
617 // start LE Scan again
618 ret = CALEClientStartScan();
619 if (CA_STATUS_OK != ret)
621 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
622 ca_mutex_unlock(g_threadSendMutex);
626 ca_mutex_unlock(g_threadSendMutex);
627 OIC_LOG(INFO, TAG, "unicast - send success");
633 // start LE Scan again
634 ret = CALEClientStartScan();
635 if (CA_STATUS_OK != ret)
637 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
638 ca_mutex_unlock(g_threadSendMutex);
644 (*g_jvm)->DetachCurrentThread(g_jvm);
646 ca_mutex_unlock(g_threadSendMutex);
647 return CA_SEND_FAILED;
650 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const char* data,
651 const uint32_t dataLen)
653 OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s, %d", data, dataLen);
654 VERIFY_NON_NULL(data, TAG, "data is null");
655 VERIFY_NON_NULL(env, TAG, "env is null");
659 OIC_LOG(ERROR, TAG, "g_deviceList is null");
660 return CA_STATUS_FAILED;
663 ca_mutex_lock(g_threadSendMutex);
665 CALEClientSetSendFinishFlag(false);
667 OIC_LOG(DEBUG, TAG, "set byteArray for data");
670 (*env)->DeleteGlobalRef(env, g_sendBuffer);
672 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
673 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
674 g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
676 // connect to gatt server
677 CAResult_t res = CALEClientStopScan();
678 if (CA_STATUS_OK != res)
680 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
681 ca_mutex_unlock(g_threadSendMutex);
685 uint32_t length = u_arraylist_length(g_deviceList);
686 g_targetCnt = length;
693 while (index < length)
695 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
698 OIC_LOG(ERROR, TAG, "jarrayObj is null");
702 res = CALEClientSendData(env, jarrayObj);
703 if (res != CA_STATUS_OK)
705 OIC_LOG(ERROR, TAG, "BT device[%d] - send has failed");
709 CALEClientSetTheSendRequestFlag(true);
710 ca_cond_signal(g_threadSendCBCond);
712 if (!g_isReceivedWriteCB)
714 OIC_LOG(INFO, TAG, "wait..(multicast)");
715 ca_cond_wait(g_threadSendCond, g_threadSendMutex);
719 CALEClientSetWriteCharacteristicCBFlag(false);
723 jstring jni_address = CALEGetAddressFromBTDevice(env, jarrayObj);
726 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
730 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
733 OIC_LOG(ERROR, TAG, "address is not available");
737 res = CALECheckSendState(address);
738 if (CA_STATUS_OK != res)
740 OIC_LOG(ERROR, TAG, "send has failed");
741 (*env)->ReleaseStringUTFChars(env, jni_address, address);
745 (*env)->ReleaseStringUTFChars(env, jni_address, address);
750 OIC_LOG(DEBUG, TAG, "connection routine is finished");
752 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
753 if (!g_isFinishedSendData)
755 ca_mutex_lock(g_threadMutex);
756 ca_cond_wait(g_threadCond, g_threadMutex);
757 OIC_LOG(DEBUG, TAG, "the data was sent for All devices");
758 ca_mutex_unlock(g_threadMutex);
761 // start LE Scan again
762 res = CALEClientStartScan();
763 if (CA_STATUS_OK != res)
765 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
766 ca_mutex_unlock(g_threadSendMutex);
770 ca_mutex_unlock(g_threadSendMutex);
771 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
775 res = CALEClientStartScan();
776 if (CA_STATUS_OK != res)
778 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
779 ca_mutex_unlock(g_threadSendMutex);
783 ca_mutex_unlock(g_threadSendMutex);
784 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
785 return CA_SEND_FAILED;
788 CAResult_t CALECheckSendState(const char* address)
790 VERIFY_NON_NULL(address, TAG, "address is null");
792 ca_mutex_lock(g_deviceStateListMutex);
793 CALEState_t* state = CALEClientGetStateInfo(address);
796 OIC_LOG(ERROR, TAG, "state is null");
797 ca_mutex_unlock(g_deviceStateListMutex);
798 return CA_SEND_FAILED;
801 if (STATE_SEND_SUCCESS != state->sendState)
803 OIC_LOG(ERROR, TAG, "sendstate is not STATE_SEND_SUCCESS");
804 ca_mutex_unlock(g_deviceStateListMutex);
805 return CA_SEND_FAILED;
807 ca_mutex_unlock(g_deviceStateListMutex);
811 CAResult_t CALEClientSendData(JNIEnv *env, jobject device)
813 OIC_LOG(DEBUG, TAG, "IN - CALEClientSendData");
814 VERIFY_NON_NULL(device, TAG, "device is null");
815 VERIFY_NON_NULL(env, TAG, "env is null");
817 jstring jni_address = CALEGetAddressFromBTDevice(env, device);
820 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
821 return CA_STATUS_FAILED;
824 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
827 OIC_LOG(ERROR, TAG, "address is not available");
828 return CA_STATUS_FAILED;
831 ca_mutex_lock(g_deviceStateListMutex);
832 CALEState_t* state = CALEClientGetStateInfo(address);
833 ca_mutex_unlock(g_deviceStateListMutex);
836 OIC_LOG(DEBUG, TAG, "state is empty..start to connect LE");
837 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
838 if (CA_STATUS_OK != ret)
840 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
841 (*env)->ReleaseStringUTFChars(env, jni_address, address);
847 if (STATE_CONNECTED == state->connectedState)
849 OIC_LOG(INFO, TAG, "GATT has already connected");
850 jobject gatt = CALEClientGetGattObjInList(env, address);
853 OIC_LOG(ERROR, TAG, "CALEClientGetGattObjInList has failed");
854 (*env)->ReleaseStringUTFChars(env, jni_address, address);
855 return CA_STATUS_FAILED;
858 CAResult_t ret = CALEClientWriteCharacteristic(env, gatt);
859 if (CA_STATUS_OK != ret)
861 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
862 (*env)->ReleaseStringUTFChars(env, jni_address, address);
868 OIC_LOG(DEBUG, TAG, "start to connect LE");
869 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
870 if (CA_STATUS_OK != ret)
872 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
873 (*env)->ReleaseStringUTFChars(env, jni_address, address);
879 (*env)->ReleaseStringUTFChars(env, jni_address, address);
883 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt)
885 VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
886 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
888 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
889 if (!jni_cid_gattdevice_list)
891 OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null");
895 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
896 "()Landroid/bluetooth/BluetoothDevice;");
897 if (!jni_mid_getDevice)
899 OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
903 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
906 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
910 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
913 OIC_LOG(ERROR, TAG, "jni_address is null");
923 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt)
926 OIC_LOG(DEBUG, TAG, "Gatt Close");
927 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
928 VERIFY_NON_NULL(env, TAG, "env is null");
930 // get BluetoothGatt class
931 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
932 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
933 if (!jni_cid_BluetoothGatt)
935 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
936 return CA_STATUS_FAILED;
939 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V");
940 if (!jni_mid_closeGatt)
942 OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null");
946 // call disconnect gatt method
947 OIC_LOG(DEBUG, TAG, "request to close GATT");
948 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
950 if ((*env)->ExceptionCheck(env))
952 OIC_LOG(ERROR, TAG, "closeGATT has failed");
953 (*env)->ExceptionDescribe(env);
954 (*env)->ExceptionClear(env);
955 return CA_STATUS_FAILED;
961 CAResult_t CALEClientStartScan()
963 if (!g_isStartedMulticastServer)
965 OIC_LOG(ERROR, TAG, "server is not started yet..scan will be passed");
966 return CA_STATUS_FAILED;
969 if (!g_isStartedLEClient)
971 OIC_LOG(ERROR, TAG, "LE client is not started");
972 return CA_STATUS_FAILED;
977 OIC_LOG(ERROR, TAG, "g_jvm is null");
978 return CA_STATUS_FAILED;
983 OIC_LOG(INFO, TAG, "scanning is already started");
987 bool isAttached = false;
989 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
992 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
994 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
997 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
998 return CA_STATUS_FAILED;
1003 OIC_LOG(DEBUG, TAG, "CALEClientStartScan");
1005 CAResult_t ret = CA_STATUS_OK;
1006 // scan gatt server with UUID
1007 if (g_leScanCallback && g_uuidList)
1010 ret = CALEClientStartScanWithUUIDImpl(env, g_uuidList, g_leScanCallback);
1011 if(CA_STATUS_OK != ret)
1013 OIC_LOG(ERROR, TAG, "CALEClientStartScanWithUUIDImpl has failed");
1016 ret = CALEClientStartScanImpl(env, g_leScanCallback);
1017 if (CA_STATUS_OK != ret)
1019 OIC_LOG(ERROR, TAG, "CALEClientStartScanImpl has failed");
1026 (*g_jvm)->DetachCurrentThread(g_jvm);
1032 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback)
1034 VERIFY_NON_NULL(callback, TAG, "callback is null");
1035 VERIFY_NON_NULL(env, TAG, "env is null");
1037 if (!CALEIsEnableBTAdapter(env))
1039 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1040 return CA_ADAPTER_NOT_ENABLED;
1043 // get default bt adapter class
1044 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1045 if (!jni_cid_BTAdapter)
1047 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1048 return CA_STATUS_FAILED;
1051 // get remote bt adapter method
1052 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1053 "getDefaultAdapter",
1054 METHODID_OBJECTNONPARAM);
1055 if (!jni_mid_getDefaultAdapter)
1057 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1058 return CA_STATUS_FAILED;
1061 // get start le scan method
1062 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1063 "(Landroid/bluetooth/BluetoothAdapter$"
1064 "LeScanCallback;)Z");
1065 if (!jni_mid_startLeScan)
1067 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1068 return CA_STATUS_FAILED;
1071 // gat bt adapter object
1072 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1073 jni_mid_getDefaultAdapter);
1074 if (!jni_obj_BTAdapter)
1076 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1077 return CA_STATUS_FAILED;
1080 // call start le scan method
1081 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1082 jni_mid_startLeScan, callback);
1083 if (!jni_obj_startLeScan)
1085 OIC_LOG(ERROR, TAG, "startLeScan is failed");
1086 return CA_STATUS_FAILED;
1090 OIC_LOG(DEBUG, TAG, "startLeScan is started");
1091 g_isStartedScan = true;
1094 return CA_STATUS_OK;
1097 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
1099 VERIFY_NON_NULL(callback, TAG, "callback is null");
1100 VERIFY_NON_NULL(uuids, TAG, "uuids is null");
1101 VERIFY_NON_NULL(env, TAG, "env is null");
1103 if (!CALEIsEnableBTAdapter(env))
1105 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1106 return CA_ADAPTER_NOT_ENABLED;
1109 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1110 if (!jni_cid_BTAdapter)
1112 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1113 return CA_STATUS_FAILED;
1116 // get remote bt adapter method
1117 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1118 "getDefaultAdapter",
1119 METHODID_OBJECTNONPARAM);
1120 if (!jni_mid_getDefaultAdapter)
1122 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1123 return CA_STATUS_FAILED;
1126 // get start le scan method
1127 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1128 "([Ljava/util/UUID;Landroid/bluetooth/"
1129 "BluetoothAdapter$LeScanCallback;)Z");
1130 if (!jni_mid_startLeScan)
1132 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1133 return CA_STATUS_FAILED;
1136 // get bt adapter object
1137 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1138 jni_mid_getDefaultAdapter);
1139 if (!jni_obj_BTAdapter)
1141 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1142 return CA_STATUS_FAILED;
1145 // call start le scan method
1146 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1147 jni_mid_startLeScan, uuids, callback);
1148 if (!jni_obj_startLeScan)
1150 OIC_LOG(ERROR, TAG, "startLeScan With UUID is failed");
1151 return CA_STATUS_FAILED;
1155 OIC_LOG(DEBUG, TAG, "startLeScan With UUID is started");
1156 g_isStartedScan = true;
1159 return CA_STATUS_OK;
1162 jobject CALEClientGetUUIDObject(JNIEnv *env, const char* uuid)
1164 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
1165 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1168 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1171 OIC_LOG(ERROR, TAG, "jni_cid_uuid is null");
1175 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString",
1176 "(Ljava/lang/String;)"
1177 "Ljava/util/UUID;");
1178 if (!jni_mid_fromString)
1180 OIC_LOG(ERROR, TAG, "jni_mid_fromString is null");
1184 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1185 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString,
1189 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1193 return jni_obj_uuid;
1196 CAResult_t CALEClientStopScan()
1200 OIC_LOG(ERROR, TAG, "g_jvm is null");
1201 return CA_STATUS_FAILED;
1204 if (!g_isStartedScan)
1206 OIC_LOG(INFO, TAG, "scanning is already stopped");
1207 return CA_STATUS_OK;
1210 bool isAttached = false;
1212 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1215 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1216 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1219 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1220 return CA_STATUS_FAILED;
1225 CAResult_t ret = CALEClientStopScanImpl(env, g_leScanCallback);
1226 if (CA_STATUS_OK != ret)
1228 OIC_LOG(ERROR, TAG, "CALEClientStopScanImpl has failed");
1232 g_isStartedScan = false;
1237 (*g_jvm)->DetachCurrentThread(g_jvm);
1243 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback)
1245 OIC_LOG(DEBUG, TAG, "CALEClientStopScanImpl");
1246 VERIFY_NON_NULL(callback, TAG, "callback is null");
1247 VERIFY_NON_NULL(env, TAG, "env is null");
1249 if (!CALEIsEnableBTAdapter(env))
1251 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1252 return CA_ADAPTER_NOT_ENABLED;
1255 // get default bt adapter class
1256 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1257 if (!jni_cid_BTAdapter)
1259 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1260 return CA_STATUS_FAILED;
1263 // get remote bt adapter method
1264 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1265 "getDefaultAdapter",
1266 METHODID_OBJECTNONPARAM);
1267 if (!jni_mid_getDefaultAdapter)
1269 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1270 return CA_STATUS_FAILED;
1273 // get start le scan method
1274 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1275 "(Landroid/bluetooth/"
1276 "BluetoothAdapter$LeScanCallback;)V");
1277 if (!jni_mid_stopLeScan)
1279 OIC_LOG(ERROR, TAG, "stopLeScan: jni_mid_stopLeScan is null");
1280 return CA_STATUS_FAILED;
1283 // gat bt adapter object
1284 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1285 jni_mid_getDefaultAdapter);
1286 if (!jni_obj_BTAdapter)
1288 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
1289 return CA_STATUS_FAILED;
1292 OIC_LOG(DEBUG, TAG, "CALL API - request to stop LE Scan");
1293 // call start le scan method
1294 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1295 if ((*env)->ExceptionCheck(env))
1297 OIC_LOG(ERROR, TAG, "stopLeScan has failed");
1298 (*env)->ExceptionDescribe(env);
1299 (*env)->ExceptionClear(env);
1300 return CA_STATUS_FAILED;
1303 return CA_STATUS_OK;
1306 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
1309 OIC_LOG(DEBUG, TAG, "GATT CONNECT");
1310 VERIFY_NON_NULL(env, TAG, "env is null");
1311 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1312 VERIFY_NON_NULL(callback, TAG, "callback is null");
1314 if (!CALEIsEnableBTAdapter(env))
1316 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1317 return CA_ADAPTER_NOT_ENABLED;
1320 jstring jni_address = CALEGetAddressFromBTDevice(env, bluetoothDevice);
1323 OIC_LOG(ERROR, TAG, "bleConnect: CALEGetAddressFromBTDevice is null");
1324 return CA_STATUS_FAILED;
1327 // get BluetoothDevice class
1328 OIC_LOG(DEBUG, TAG, "get BluetoothDevice class");
1329 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1330 if (!jni_cid_BluetoothDevice)
1332 OIC_LOG(ERROR, TAG, "bleConnect: jni_cid_BluetoothDevice is null");
1333 return CA_STATUS_FAILED;
1336 // get connectGatt method
1337 OIC_LOG(DEBUG, TAG, "get connectGatt method");
1338 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice, "connectGatt",
1339 "(Landroid/content/Context;ZLandroid/"
1340 "bluetooth/BluetoothGattCallback;)"
1341 "Landroid/bluetooth/BluetoothGatt;");
1342 if (!jni_mid_connectGatt)
1344 OIC_LOG(ERROR, TAG, "bleConnect: jni_mid_connectGatt is null");
1345 return CA_STATUS_FAILED;
1348 OIC_LOG(DEBUG, TAG, "Call object method - connectGatt");
1349 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice,
1350 jni_mid_connectGatt,
1352 autoconnect, callback);
1353 if (!jni_obj_connectGatt)
1355 OIC_LOG(ERROR, TAG, "CALL API - connectGatt was failed..it will be removed");
1356 CALEClientRemoveDeviceInScanDeviceList(env, jni_address);
1357 CALEClientUpdateSendCnt(env);
1358 return CA_STATUS_FAILED;
1362 OIC_LOG(DEBUG, TAG, "le connecting..please wait..");
1364 return CA_STATUS_OK;
1367 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt)
1369 OIC_LOG(DEBUG, TAG, "GATT DISCONNECT");
1370 VERIFY_NON_NULL(env, TAG, "env is null");
1371 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1373 if (!CALEIsEnableBTAdapter(env))
1375 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1376 return CA_ADAPTER_NOT_ENABLED;
1379 // get BluetoothGatt class
1380 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1381 if (!jni_cid_BluetoothGatt)
1383 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1384 return CA_STATUS_FAILED;
1387 OIC_LOG(DEBUG, TAG, "get gatt disconnect method");
1388 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1389 "disconnect", "()V");
1390 if (!jni_mid_disconnectGatt)
1392 OIC_LOG(ERROR, TAG, "jni_mid_disconnectGatt is null");
1393 return CA_STATUS_FAILED;
1396 // call disconnect gatt method
1397 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1398 if ((*env)->ExceptionCheck(env))
1400 OIC_LOG(ERROR, TAG, "disconnect has failed");
1401 (*env)->ExceptionDescribe(env);
1402 (*env)->ExceptionClear(env);
1403 return CA_STATUS_FAILED;
1406 OIC_LOG(DEBUG, TAG, "disconnecting Gatt...");
1408 return CA_STATUS_OK;
1411 CAResult_t CALEClientDisconnectAll(JNIEnv *env)
1413 OIC_LOG(DEBUG, TAG, "CALEClientDisconnectAll");
1414 VERIFY_NON_NULL(env, TAG, "env is null");
1416 if (!g_gattObjectList)
1418 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
1419 return CA_STATUS_FAILED;
1422 uint32_t length = u_arraylist_length(g_gattObjectList);
1423 for (uint32_t index = 0; index < length; index++)
1425 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
1428 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1431 CAResult_t res = CALEClientDisconnect(env, jarrayObj);
1432 if (CA_STATUS_OK != res)
1434 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
1439 OICFree(g_gattObjectList);
1440 g_gattObjectList = NULL;
1442 return CA_STATUS_OK;
1445 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1447 VERIFY_NON_NULL(env, TAG, "env is null");
1448 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1450 if (!CALEIsEnableBTAdapter(env))
1452 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1453 return CA_ADAPTER_NOT_ENABLED;
1456 // get BluetoothGatt class
1457 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1458 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1459 if (!jni_cid_BluetoothGatt)
1461 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1462 return CA_STATUS_FAILED;
1465 OIC_LOG(DEBUG, TAG, "discovery gatt services method");
1466 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1467 "discoverServices", "()Z");
1468 if (!jni_mid_discoverServices)
1470 OIC_LOG(ERROR, TAG, "jni_mid_discoverServices is null");
1471 return CA_STATUS_FAILED;
1473 // call disconnect gatt method
1474 OIC_LOG(DEBUG, TAG, "CALL API - request discovery gatt services");
1475 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1478 OIC_LOG(ERROR, TAG, "discoverServices has not been started");
1479 return CA_STATUS_FAILED;
1482 return CA_STATUS_OK;
1485 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt)
1487 VERIFY_NON_NULL(env, TAG, "env is null");
1488 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
1491 jobject jni_obj_character = CALEClientCreateGattCharacteristic(env, gatt, g_sendBuffer);
1492 if (!jni_obj_character)
1494 CALEClientSendFinish(env, gatt);
1495 ca_cond_signal(g_threadSendCond);
1496 return CA_STATUS_FAILED;
1499 CAResult_t ret = CALEClientWriteCharacteristicImpl(env, gatt, jni_obj_character);
1500 if (CA_STATUS_OK != ret)
1502 CALEClientSendFinish(env, gatt);
1503 ca_cond_signal(g_threadSendCond);
1507 return CA_STATUS_OK;
1510 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
1511 jobject gattCharacteristic)
1513 OIC_LOG(DEBUG, TAG, "WRITE GATT CHARACTERISTIC");
1514 VERIFY_NON_NULL(env, TAG, "env is null");
1515 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1516 VERIFY_NON_NULL(gattCharacteristic, TAG, "gattCharacteristic is null");
1518 if (!CALEIsEnableBTAdapter(env))
1520 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1521 return CA_STATUS_FAILED;
1524 // get BluetoothGatt class
1525 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1526 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1527 if (!jni_cid_BluetoothGatt)
1529 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1530 return CA_STATUS_FAILED;
1533 OIC_LOG(DEBUG, TAG, "write characteristic method");
1534 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1535 "writeCharacteristic",
1536 "(Landroid/bluetooth/"
1537 "BluetoothGattCharacteristic;)Z");
1538 if (!jni_mid_writeCharacteristic)
1540 OIC_LOG(ERROR, TAG, "jni_mid_writeCharacteristic is null");
1541 return CA_STATUS_FAILED;
1544 // call disconnect gatt method
1545 OIC_LOG(DEBUG, TAG, "CALL API - request to write gatt characteristic");
1546 jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt,
1547 jni_mid_writeCharacteristic,
1548 gattCharacteristic);
1551 OIC_LOG(DEBUG, TAG, "writeCharacteristic success");
1555 OIC_LOG(ERROR, TAG, "writeCharacteristic has failed");
1556 return CA_STATUS_FAILED;
1559 return CA_STATUS_OK;
1562 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt)
1564 VERIFY_NON_NULL(env, TAG, "env is null");
1565 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1567 if (!CALEIsEnableBTAdapter(env))
1569 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1570 return CA_STATUS_FAILED;
1573 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1574 if (!jni_cid_BluetoothGatt)
1576 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1577 return CA_STATUS_FAILED;
1580 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
1583 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1584 return CA_STATUS_FAILED;
1587 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1588 if (!jni_obj_GattCharacteristic)
1590 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1591 return CA_STATUS_FAILED;
1594 OIC_LOG(DEBUG, TAG, "read characteristic method");
1595 jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1596 "readCharacteristic",
1597 "(Landroid/bluetooth/"
1598 "BluetoothGattCharacteristic;)Z");
1599 if (!jni_mid_readCharacteristic)
1601 OIC_LOG(ERROR, TAG, "jni_mid_readCharacteristic is null");
1602 return CA_STATUS_FAILED;
1605 // call disconnect gatt method
1606 OIC_LOG(DEBUG, TAG, "CALL API - request to read gatt characteristic");
1607 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic,
1608 jni_obj_GattCharacteristic);
1611 OIC_LOG(DEBUG, TAG, "readCharacteristic success");
1615 OIC_LOG(ERROR, TAG, "readCharacteristic has failed");
1616 return CA_STATUS_FAILED;
1619 return CA_STATUS_OK;
1622 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
1623 jobject characteristic)
1625 VERIFY_NON_NULL(env, TAG, "env is null");
1626 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1627 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1629 if (!CALEIsEnableBTAdapter(env))
1631 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1632 return CA_ADAPTER_NOT_ENABLED;
1635 // get BluetoothGatt class
1636 OIC_LOG(DEBUG, TAG, "CALEClientSetCharacteristicNotification");
1637 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1638 if (!jni_cid_BluetoothGatt)
1640 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1641 return CA_STATUS_FAILED;
1644 // set Characteristic Notification
1645 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1646 "setCharacteristicNotification",
1647 "(Landroid/bluetooth/"
1648 "BluetoothGattCharacteristic;Z)Z");
1649 if (!jni_mid_setNotification)
1651 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1652 return CA_STATUS_FAILED;
1655 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification,
1656 characteristic, JNI_TRUE);
1657 if (JNI_TRUE == ret)
1659 OIC_LOG(DEBUG, TAG, "CALL API - setCharacteristicNotification success");
1663 OIC_LOG(ERROR, TAG, "CALL API - setCharacteristicNotification has failed");
1664 return CA_STATUS_FAILED;
1667 return CA_STATUS_OK;
1670 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1672 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1673 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1674 VERIFY_NON_NULL_RET(characterUUID, TAG, "characterUUID is null", NULL);
1676 if (!CALEIsEnableBTAdapter(env))
1678 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1682 // get BluetoothGatt class
1683 OIC_LOG(DEBUG, TAG, "CALEClientGetGattService");
1684 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1685 if (!jni_cid_BluetoothGatt)
1687 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1691 jmethodID jni_mid_getService = (*env)->GetMethodID(
1692 env, jni_cid_BluetoothGatt, "getService",
1693 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1694 if (!jni_mid_getService)
1696 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1700 jobject jni_obj_service_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1701 if (!jni_obj_service_uuid)
1703 OIC_LOG(ERROR, TAG, "jni_obj_service_uuid is null");
1707 // get bluetooth gatt service
1708 OIC_LOG(DEBUG, TAG, "request to get service");
1709 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService,
1710 jni_obj_service_uuid);
1711 if (!jni_obj_gattService)
1713 OIC_LOG(ERROR, TAG, "jni_obj_gattService is null");
1717 // get bluetooth gatt service class
1718 jclass jni_cid_BluetoothGattService = (*env)->FindClass(
1719 env, "android/bluetooth/BluetoothGattService");
1720 if (!jni_cid_BluetoothGattService)
1722 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGattService is null");
1726 OIC_LOG(DEBUG, TAG, "get gatt getCharacteristic method");
1727 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService,
1728 "getCharacteristic",
1729 "(Ljava/util/UUID;)"
1730 "Landroid/bluetooth/"
1731 "BluetoothGattCharacteristic;");
1732 if (!jni_mid_getCharacteristic)
1734 OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null");
1738 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1741 OIC_LOG(ERROR, TAG, "uuid is null");
1745 jobject jni_obj_tx_uuid = CALEClientGetUUIDObject(env, uuid);
1746 if (!jni_obj_tx_uuid)
1748 OIC_LOG(ERROR, TAG, "jni_obj_tx_uuid is null");
1749 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1753 OIC_LOG(DEBUG, TAG, "request to get Characteristic");
1754 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService,
1755 jni_mid_getCharacteristic,
1758 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1759 return jni_obj_GattCharacteristic;
1762 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data)
1764 OIC_LOG(DEBUG, TAG, "CALEClientCreateGattCharacteristic");
1765 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1766 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1767 VERIFY_NON_NULL_RET(data, TAG, "data is null", NULL);
1769 if (!CALEIsEnableBTAdapter(env))
1771 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1775 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_REQUEST_UUID);
1778 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1782 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1783 if (!jni_obj_GattCharacteristic)
1785 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1789 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth"
1790 "/BluetoothGattCharacteristic");
1791 if (!jni_cid_BTGattCharacteristic)
1793 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1797 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1798 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1800 if (!jni_mid_setValue)
1802 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
1806 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue,
1808 if (JNI_TRUE == ret)
1810 OIC_LOG(DEBUG, TAG, "the locally stored value has been set");
1814 OIC_LOG(ERROR, TAG, "the locally stored value hasn't been set");
1819 jmethodID jni_mid_setWriteType = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
1820 "setWriteType", "(I)V");
1821 if (!jni_mid_setWriteType)
1823 OIC_LOG(ERROR, TAG, "jni_mid_setWriteType is null");
1827 jfieldID jni_fid_no_response = (*env)->GetStaticFieldID(env, jni_cid_BTGattCharacteristic,
1828 "WRITE_TYPE_NO_RESPONSE", "I");
1829 if (!jni_fid_no_response)
1831 OIC_LOG(ERROR, TAG, "jni_fid_no_response is not available");
1835 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTGattCharacteristic,
1836 jni_fid_no_response);
1838 (*env)->CallVoidMethod(env, jni_obj_GattCharacteristic, jni_mid_setWriteType, jni_int_val);
1840 return jni_obj_GattCharacteristic;
1843 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
1845 VERIFY_NON_NULL_RET(characteristic, TAG, "characteristic is null", NULL);
1846 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1848 if (!CALEIsEnableBTAdapter(env))
1850 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1854 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1855 "BluetoothGattCharacteristic");
1856 if (!jni_cid_BTGattCharacteristic)
1858 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1862 OIC_LOG(DEBUG, TAG, "get value in Characteristic");
1863 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
1865 if (!jni_mid_getValue)
1867 OIC_LOG(ERROR, TAG, "jni_mid_getValue is null");
1871 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic,
1873 return jni_obj_data_array;
1876 CAResult_t CALEClientCreateUUIDList()
1880 OIC_LOG(ERROR, TAG, "g_jvm is null");
1881 return CA_STATUS_FAILED;
1884 bool isAttached = false;
1886 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1889 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1890 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1894 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1895 return CA_STATUS_FAILED;
1900 // create new object array
1901 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1902 if (!jni_cid_uuid_list)
1904 OIC_LOG(ERROR, TAG, "jni_cid_uuid_list is null");
1908 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1,
1909 jni_cid_uuid_list, NULL);
1910 if (!jni_obj_uuid_list)
1912 OIC_LOG(ERROR, TAG, "jni_obj_uuid_list is null");
1917 jobject jni_obj_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1920 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1923 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
1925 g_uuidList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list);
1929 (*g_jvm)->DetachCurrentThread(g_jvm);
1932 return CA_STATUS_OK;
1939 (*g_jvm)->DetachCurrentThread(g_jvm);
1941 return CA_STATUS_FAILED;
1944 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
1945 jobject characteristic)
1947 VERIFY_NON_NULL(env, TAG, "env is null");
1948 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1949 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1951 if (!CALEIsEnableBTAdapter(env))
1953 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1954 return CA_ADAPTER_NOT_ENABLED;
1957 OIC_LOG(DEBUG, TAG, "CALEClientSetUUIDToDescriptor");
1958 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1959 "BluetoothGattCharacteristic");
1960 if (!jni_cid_BTGattCharacteristic)
1962 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1963 return CA_STATUS_FAILED;
1966 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1967 jmethodID jni_mid_getDescriptor = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
1969 "(Ljava/util/UUID;)Landroid/bluetooth/"
1970 "BluetoothGattDescriptor;");
1971 if (!jni_mid_getDescriptor)
1973 OIC_LOG(ERROR, TAG, "jni_mid_getDescriptor is null");
1974 return CA_STATUS_FAILED;
1977 jobject jni_obj_cc_uuid = CALEClientGetUUIDObject(env, OIC_GATT_CHARACTERISTIC_CONFIG_UUID);
1978 if (!jni_obj_cc_uuid)
1980 OIC_LOG(ERROR, TAG, "jni_obj_cc_uuid is null");
1981 return CA_STATUS_FAILED;
1984 OIC_LOG(DEBUG, TAG, "request to get descriptor");
1985 jobject jni_obj_descriptor = (*env)->CallObjectMethod(env, characteristic,
1986 jni_mid_getDescriptor, jni_obj_cc_uuid);
1987 if (!jni_obj_descriptor)
1989 OIC_LOG(ERROR, TAG, "jni_obj_descriptor is null");
1990 return CA_STATUS_FAILED;
1993 OIC_LOG(DEBUG, TAG, "set value in descriptor");
1994 jclass jni_cid_descriptor = (*env)->FindClass(env,
1995 "android/bluetooth/BluetoothGattDescriptor");
1996 if (!jni_cid_descriptor)
1998 OIC_LOG(ERROR, TAG, "jni_cid_descriptor is null");
1999 return CA_STATUS_FAILED;
2002 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_descriptor, "setValue", "([B)Z");
2003 if (!jni_mid_setValue)
2005 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
2006 return CA_STATUS_FAILED;
2009 jfieldID jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor,
2010 "ENABLE_NOTIFICATION_VALUE", "[B");
2011 if (!jni_fid_NotiValue)
2013 OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null");
2014 return CA_STATUS_FAILED;
2017 OIC_LOG(DEBUG, TAG, "get ENABLE_NOTIFICATION_VALUE");
2019 jboolean jni_setvalue = (*env)->CallBooleanMethod(
2020 env, jni_obj_descriptor, jni_mid_setValue,
2021 (jbyteArray)(*env)->GetStaticObjectField(env, jni_cid_descriptor, jni_fid_NotiValue));
2024 OIC_LOG(DEBUG, TAG, "setValue success");
2028 OIC_LOG(ERROR, TAG, "setValue has failed");
2029 return CA_STATUS_FAILED;
2032 jclass jni_cid_gatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
2035 OIC_LOG(ERROR, TAG, "jni_cid_gatt is null");
2036 return CA_STATUS_FAILED;
2039 OIC_LOG(DEBUG, TAG, "write Descriptor in gatt object");
2040 jmethodID jni_mid_writeDescriptor = (*env)->GetMethodID(env, jni_cid_gatt, "writeDescriptor",
2041 "(Landroid/bluetooth/"
2042 "BluetoothGattDescriptor;)Z");
2043 if (!jni_mid_writeDescriptor)
2045 OIC_LOG(ERROR, TAG, "jni_mid_writeDescriptor is null");
2046 return CA_STATUS_FAILED;
2049 OIC_LOG(DEBUG, TAG, "request to write descriptor");
2050 jboolean jni_ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeDescriptor,
2051 jni_obj_descriptor);
2054 OIC_LOG(DEBUG, TAG, "writeDescriptor success");
2058 OIC_LOG(ERROR, TAG, "writeDescriptor has failed");
2059 return CA_STATUS_FAILED;
2062 return CA_STATUS_OK;
2065 void CALEClientCreateScanDeviceList(JNIEnv *env)
2067 OIC_LOG(DEBUG, TAG, "CALEClientCreateScanDeviceList");
2068 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2070 ca_mutex_lock(g_deviceListMutex);
2071 // create new object array
2072 if (g_deviceList == NULL)
2074 OIC_LOG(DEBUG, TAG, "Create device list");
2076 g_deviceList = u_arraylist_create();
2078 ca_mutex_unlock(g_deviceListMutex);
2081 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device)
2083 OIC_LOG(DEBUG, TAG, "IN - CALEClientAddScanDeviceToList");
2084 VERIFY_NON_NULL(device, TAG, "device is null");
2085 VERIFY_NON_NULL(env, TAG, "env is null");
2087 ca_mutex_lock(g_deviceListMutex);
2091 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2092 ca_mutex_unlock(g_deviceListMutex);
2093 return CA_STATUS_FAILED;
2096 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
2097 if (!jni_remoteAddress)
2099 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2100 ca_mutex_unlock(g_deviceListMutex);
2101 return CA_STATUS_FAILED;
2104 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2107 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2108 ca_mutex_unlock(g_deviceListMutex);
2109 return CA_STATUS_FAILED;
2112 if (!CALEClientIsDeviceInScanDeviceList(env, remoteAddress))
2114 jobject gdevice = (*env)->NewGlobalRef(env, device);
2115 u_arraylist_add(g_deviceList, gdevice);
2116 OIC_LOG(DEBUG, TAG, "Set Object to Array as Element");
2118 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2120 ca_mutex_unlock(g_deviceListMutex);
2122 OIC_LOG(DEBUG, TAG, "OUT - CALEClientAddScanDeviceToList");
2123 return CA_STATUS_OK;
2126 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char* remoteAddress)
2128 OIC_LOG(DEBUG, TAG, "IN - CALEClientIsDeviceInScanDeviceList");
2129 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2130 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2134 OIC_LOG(DEBUG, TAG, "g_deviceList is null");
2138 uint32_t length = u_arraylist_length(g_deviceList);
2139 for (uint32_t index = 0; index < length; index++)
2141 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2144 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2148 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2149 if (!jni_setAddress)
2151 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2155 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2158 OIC_LOG(ERROR, TAG, "setAddress is null");
2162 if (!strcmp(remoteAddress, setAddress))
2164 OIC_LOG(DEBUG, TAG, "the device is already set");
2165 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2169 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2172 OIC_LOG(DEBUG, TAG, "OUT - CALEClientIsDeviceInScanDeviceList");
2173 OIC_LOG(DEBUG, TAG, "there are no the device in list. we can add");
2178 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env)
2180 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllScanDevices");
2181 VERIFY_NON_NULL(env, TAG, "env is null");
2183 ca_mutex_lock(g_deviceListMutex);
2187 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2188 ca_mutex_unlock(g_deviceListMutex);
2189 return CA_STATUS_FAILED;
2192 uint32_t length = u_arraylist_length(g_deviceList);
2193 for (uint32_t index = 0; index < length; index++)
2195 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2198 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2201 (*env)->DeleteGlobalRef(env, jarrayObj);
2204 OICFree(g_deviceList);
2205 g_deviceList = NULL;
2207 ca_mutex_unlock(g_deviceListMutex);
2208 return CA_STATUS_OK;
2211 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring address)
2213 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceInScanDeviceList");
2214 VERIFY_NON_NULL(address, TAG, "address is null");
2215 VERIFY_NON_NULL(env, TAG, "env is null");
2217 ca_mutex_lock(g_deviceListMutex);
2221 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2222 ca_mutex_unlock(g_deviceListMutex);
2223 return CA_STATUS_FAILED;
2226 uint32_t length = u_arraylist_length(g_deviceList);
2227 for (uint32_t index = 0; index < length; index++)
2229 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2232 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2233 ca_mutex_unlock(g_deviceListMutex);
2234 return CA_STATUS_FAILED;
2237 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2238 if (!jni_setAddress)
2240 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2241 ca_mutex_unlock(g_deviceListMutex);
2242 return CA_STATUS_FAILED;
2245 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2248 OIC_LOG(ERROR, TAG, "setAddress is null");
2249 ca_mutex_unlock(g_deviceListMutex);
2250 return CA_STATUS_FAILED;
2253 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
2256 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2257 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2258 ca_mutex_unlock(g_deviceListMutex);
2259 return CA_STATUS_FAILED;
2262 if (!strcmp(setAddress, remoteAddress))
2264 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2265 (*env)->DeleteGlobalRef(env, jarrayObj);
2266 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2267 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2269 CALEClientReorderingList(index, g_deviceList);
2270 ca_mutex_unlock(g_deviceListMutex);
2271 return CA_STATUS_OK;
2273 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2274 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2277 ca_mutex_unlock(g_deviceListMutex);
2278 OIC_LOG(DEBUG, TAG, "There are no object in the device list");
2280 return CA_STATUS_OK;
2287 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt)
2289 OIC_LOG(DEBUG, TAG, "CALEClientAddGattobjToList");
2290 VERIFY_NON_NULL(env, TAG, "env is null");
2291 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2293 ca_mutex_lock(g_gattObjectMutex);
2295 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2296 if (!jni_remoteAddress)
2298 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2299 ca_mutex_unlock(g_gattObjectMutex);
2300 return CA_STATUS_FAILED;
2303 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2306 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2307 ca_mutex_unlock(g_gattObjectMutex);
2308 return CA_STATUS_FAILED;
2311 if (!CALEClientIsGattObjInList(env, remoteAddress))
2313 jobject newGatt = (*env)->NewGlobalRef(env, gatt);
2314 u_arraylist_add(g_gattObjectList, newGatt);
2315 OIC_LOG(DEBUG, TAG, "Set GATT Object to Array as Element");
2318 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2319 ca_mutex_unlock(g_gattObjectMutex);
2320 return CA_STATUS_OK;
2323 bool CALEClientIsGattObjInList(JNIEnv *env, const char* remoteAddress)
2325 OIC_LOG(DEBUG, TAG, "CALEClientIsGattObjInList");
2326 VERIFY_NON_NULL(env, TAG, "env is null");
2327 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2329 uint32_t length = u_arraylist_length(g_gattObjectList);
2330 for (uint32_t index = 0; index < length; index++)
2333 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2336 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2340 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2341 if (!jni_setAddress)
2343 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2347 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2350 OIC_LOG(ERROR, TAG, "setAddress is null");
2354 if (!strcmp(remoteAddress, setAddress))
2356 OIC_LOG(DEBUG, TAG, "the device is already set");
2357 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2362 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2367 OIC_LOG(DEBUG, TAG, "There are no GATT object in list. it can be added");
2371 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress)
2373 OIC_LOG(DEBUG, TAG, "CALEClientGetGattObjInList");
2374 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2375 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2377 ca_mutex_lock(g_gattObjectMutex);
2378 uint32_t length = u_arraylist_length(g_gattObjectList);
2379 for (uint32_t index = 0; index < length; index++)
2381 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2384 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2385 ca_mutex_unlock(g_gattObjectMutex);
2389 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2390 if (!jni_setAddress)
2392 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2393 ca_mutex_unlock(g_gattObjectMutex);
2397 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2400 OIC_LOG(ERROR, TAG, "setAddress is null");
2401 ca_mutex_unlock(g_gattObjectMutex);
2405 if (!strcmp(remoteAddress, setAddress))
2407 OIC_LOG(DEBUG, TAG, "the device is already set");
2408 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2409 ca_mutex_unlock(g_gattObjectMutex);
2412 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2415 ca_mutex_unlock(g_gattObjectMutex);
2416 OIC_LOG(DEBUG, TAG, "There are no the gatt object in list");
2420 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env)
2422 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllGattObjs");
2423 VERIFY_NON_NULL(env, TAG, "env is null");
2425 ca_mutex_lock(g_gattObjectMutex);
2426 if (!g_gattObjectList)
2428 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2429 ca_mutex_unlock(g_gattObjectMutex);
2430 return CA_STATUS_FAILED;
2433 uint32_t length = u_arraylist_length(g_gattObjectList);
2434 for (uint32_t index = 0; index < length; index++)
2436 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2439 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2442 (*env)->DeleteGlobalRef(env, jarrayObj);
2445 OICFree(g_gattObjectList);
2446 g_gattObjectList = NULL;
2447 ca_mutex_unlock(g_gattObjectMutex);
2448 return CA_STATUS_OK;
2451 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt)
2453 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObj");
2454 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2455 VERIFY_NON_NULL(env, TAG, "env is null");
2457 ca_mutex_lock(g_gattObjectMutex);
2458 if (!g_gattObjectList)
2460 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2461 ca_mutex_unlock(g_gattObjectMutex);
2462 return CA_STATUS_FAILED;
2465 uint32_t length = u_arraylist_length(g_gattObjectList);
2466 for (uint32_t index = 0; index < length; index++)
2468 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2471 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2472 ca_mutex_unlock(g_gattObjectMutex);
2473 return CA_STATUS_FAILED;
2476 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2477 if (!jni_setAddress)
2479 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2480 ca_mutex_unlock(g_gattObjectMutex);
2481 return CA_STATUS_FAILED;
2484 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2487 OIC_LOG(ERROR, TAG, "setAddress is null");
2488 ca_mutex_unlock(g_gattObjectMutex);
2489 return CA_STATUS_FAILED;
2492 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2493 if (!jni_remoteAddress)
2495 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2496 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2497 ca_mutex_unlock(g_gattObjectMutex);
2498 return CA_STATUS_FAILED;
2501 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2504 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2505 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2506 ca_mutex_unlock(g_gattObjectMutex);
2507 return CA_STATUS_FAILED;
2510 if (!strcmp(setAddress, remoteAddress))
2512 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2513 (*env)->DeleteGlobalRef(env, jarrayObj);
2514 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2515 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2516 ca_mutex_unlock(g_gattObjectMutex);
2517 return CALEClientReorderingList(index, g_gattObjectList);
2519 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2520 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2523 ca_mutex_unlock(g_gattObjectMutex);
2524 OIC_LOG(DEBUG, TAG, "there are no target object");
2525 return CA_STATUS_OK;
2528 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr)
2530 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObjForAddr");
2531 VERIFY_NON_NULL(addr, TAG, "addr is null");
2532 VERIFY_NON_NULL(env, TAG, "env is null");
2534 ca_mutex_lock(g_gattObjectMutex);
2535 if (!g_gattObjectList)
2537 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2538 ca_mutex_unlock(g_gattObjectMutex);
2539 return CA_STATUS_FAILED;
2542 uint32_t length = u_arraylist_length(g_gattObjectList);
2543 for (uint32_t index = 0; index < length; index++)
2545 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2548 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2549 ca_mutex_unlock(g_gattObjectMutex);
2550 return CA_STATUS_FAILED;
2553 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2554 if (!jni_setAddress)
2556 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2557 ca_mutex_unlock(g_gattObjectMutex);
2558 return CA_STATUS_FAILED;
2561 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2564 OIC_LOG(ERROR, TAG, "setAddress is null");
2565 ca_mutex_unlock(g_gattObjectMutex);
2566 return CA_STATUS_FAILED;
2569 const char* remoteAddress = (*env)->GetStringUTFChars(env, addr, NULL);
2572 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2573 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2574 ca_mutex_unlock(g_gattObjectMutex);
2575 return CA_STATUS_FAILED;
2578 if (!strcmp(setAddress, remoteAddress))
2580 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2581 (*env)->DeleteGlobalRef(env, jarrayObj);
2583 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2584 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2585 ca_mutex_unlock(g_gattObjectMutex);
2586 return CALEClientReorderingList(index, g_gattObjectList);
2588 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2589 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2592 ca_mutex_unlock(g_gattObjectMutex);
2593 OIC_LOG(DEBUG, TAG, "there are no target object");
2594 return CA_STATUS_FAILED;
2601 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
2602 uint16_t notificationState, uint16_t sendState)
2604 VERIFY_NON_NULL(address, TAG, "address is null");
2606 CALEState_t *newstate = (CALEState_t*) OICMalloc(sizeof(CALEState_t));
2609 OIC_LOG(ERROR, TAG, "out of memory");
2610 return CA_MEMORY_ALLOC_FAILED;
2613 if (strlen(address) > CA_MACADDR_SIZE)
2615 OIC_LOG(ERROR, TAG, "address is not proper");
2617 return CA_STATUS_FAILED;
2620 strcpy(newstate->address, address);
2621 newstate->connectedState = connectedState;
2622 newstate->notificationState = notificationState;
2623 newstate->sendState = sendState;
2624 return CALEClientAddDeviceStateToList(newstate);
2627 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state)
2629 VERIFY_NON_NULL(state, TAG, "state is null");
2631 ca_mutex_lock(g_deviceStateListMutex);
2633 if (!g_deviceStateList)
2635 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2636 ca_mutex_unlock(g_deviceStateListMutex);
2637 return CA_STATUS_FAILED;
2640 if (CALEClientIsDeviceInList(state->address))
2642 CALEState_t* curState = CALEClientGetStateInfo(state->address);
2645 OIC_LOG(ERROR, TAG, "curState is null");
2646 ca_mutex_unlock(g_deviceStateListMutex);
2647 return CA_STATUS_FAILED;
2650 if (STATE_CHARACTER_NO_CHANGE == state->notificationState)
2652 state->notificationState = curState->notificationState;
2655 // delete previous state for update new state
2656 CAResult_t res = CALEClientRemoveDeviceState(state->address);
2657 if (CA_STATUS_OK != res)
2659 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
2660 ca_mutex_unlock(g_deviceStateListMutex);
2664 u_arraylist_add(g_deviceStateList, state); // update new state
2665 OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d, %d",
2666 state->connectedState, state->notificationState);
2668 ca_mutex_unlock(g_deviceStateListMutex);
2669 return CA_STATUS_OK;
2672 bool CALEClientIsDeviceInList(const char* remoteAddress)
2674 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2676 if (!g_deviceStateList)
2678 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2682 uint32_t length = u_arraylist_length(g_deviceStateList);
2683 for (uint32_t index = 0; index < length; index++)
2685 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2688 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2692 if (!strcmp(remoteAddress, state->address))
2694 OIC_LOG(DEBUG, TAG, "the device is already set");
2703 OIC_LOG(DEBUG, TAG, "there are no the device in list.");
2707 CAResult_t CALEClientRemoveAllDeviceState()
2709 OIC_LOG(DEBUG, TAG, "CALENativeRemoveAllDevices");
2711 ca_mutex_lock(g_deviceStateListMutex);
2712 if (!g_deviceStateList)
2714 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2715 ca_mutex_unlock(g_deviceStateListMutex);
2716 return CA_STATUS_FAILED;
2719 uint32_t length = u_arraylist_length(g_deviceStateList);
2720 for (uint32_t index = 0; index < length; index++)
2722 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2725 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2731 OICFree(g_deviceStateList);
2732 g_deviceStateList = NULL;
2733 ca_mutex_unlock(g_deviceStateListMutex);
2735 return CA_STATUS_OK;
2738 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
2740 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
2741 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
2743 if (!g_deviceStateList)
2745 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2746 return CA_STATUS_FAILED;
2749 uint32_t length = u_arraylist_length(g_deviceStateList);
2750 for (uint32_t index = 0; index < length; index++)
2752 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2755 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2759 if (!strcmp(state->address, remoteAddress))
2761 OIC_LOG_V(DEBUG, TAG, "remove state : %s", remoteAddress);
2764 CAResult_t res = CALEClientReorderingList(index, g_deviceStateList);
2765 if(CA_STATUS_OK != res)
2767 OIC_LOG(ERROR, TAG, "CALEClientReorderingList has failed");
2770 return CA_STATUS_OK;
2774 return CA_STATUS_FAILED;
2777 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress)
2779 OIC_LOG(DEBUG, TAG, "CALEClientGetStateInfo");
2780 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2782 if (!g_deviceStateList)
2784 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2788 uint32_t length = u_arraylist_length(g_deviceStateList);
2789 for (uint32_t index = 0; index < length; index++)
2791 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2794 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2798 if (!strcmp(state->address, remoteAddress))
2800 OIC_LOG_V(DEBUG, TAG, "get state : %s", remoteAddress);
2807 bool CALEClientIsConnectedDevice(const char* remoteAddress)
2809 OIC_LOG(DEBUG, TAG, "CALEClientIsConnectedDevice");
2810 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2812 ca_mutex_lock(g_deviceStateListMutex);
2813 if (!g_deviceStateList)
2815 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2816 ca_mutex_unlock(g_deviceStateListMutex);
2820 uint32_t length = u_arraylist_length(g_deviceStateList);
2821 for (uint32_t index = 0; index < length; index++)
2823 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2826 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2830 if (!strcmp(state->address, remoteAddress))
2832 OIC_LOG(DEBUG, TAG, "check whether it is connected or not");
2834 if (STATE_CONNECTED == state->connectedState)
2836 ca_mutex_unlock(g_deviceStateListMutex);
2841 ca_mutex_unlock(g_deviceStateListMutex);
2846 ca_mutex_unlock(g_deviceStateListMutex);
2850 bool CALEClientIsSetCharacteristic(const char* remoteAddress)
2852 OIC_LOG(DEBUG, TAG, "CALEClientIsSetCharacteristic");
2853 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2855 ca_mutex_lock(g_deviceStateListMutex);
2856 if (!g_deviceStateList)
2858 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2859 ca_mutex_unlock(g_deviceStateListMutex);
2863 uint32_t length = u_arraylist_length(g_deviceStateList);
2864 for (uint32_t index = 0; index < length; index++)
2866 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2869 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2873 if (!strcmp(state->address, remoteAddress))
2875 OIC_LOG_V(DEBUG, TAG, "check whether it was set or not:%d", state->notificationState);
2877 if (STATE_CHARACTER_SET == state->notificationState)
2879 ca_mutex_unlock(g_deviceStateListMutex);
2884 ca_mutex_unlock(g_deviceStateListMutex);
2890 ca_mutex_unlock(g_deviceStateListMutex);
2894 void CALEClientCreateDeviceList()
2896 OIC_LOG(DEBUG, TAG, "CALEClientCreateDeviceList");
2898 // create new object array
2899 if (!g_gattObjectList)
2901 OIC_LOG(DEBUG, TAG, "Create g_gattObjectList");
2903 g_gattObjectList = u_arraylist_create();
2906 if (!g_deviceStateList)
2908 OIC_LOG(DEBUG, TAG, "Create g_deviceStateList");
2910 g_deviceStateList = u_arraylist_create();
2915 OIC_LOG(DEBUG, TAG, "Create g_deviceList");
2917 g_deviceList = u_arraylist_create();
2921 CAResult_t CALEClientReorderingList(uint32_t index, u_arraylist_t *list)
2925 OIC_LOG(ERROR, TAG, "list is null");
2926 return CA_STATUS_FAILED;
2929 if (index >= list->length)
2931 OIC_LOG(ERROR, TAG, "index is not available");
2932 return CA_STATUS_FAILED;
2935 if (index < list->length - 1)
2937 memmove(&list->data[index], &list->data[index + 1],
2938 (list->length - index - 1) * sizeof(void *));
2944 return CA_STATUS_OK;
2948 * Check Sent Count for remove g_sendBuffer
2950 void CALEClientUpdateSendCnt(JNIEnv *env)
2952 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2954 ca_mutex_lock(g_threadMutex);
2958 if (g_targetCnt <= g_currentSentCnt)
2961 g_currentSentCnt = 0;
2965 (*env)->DeleteGlobalRef(env, g_sendBuffer);
2966 g_sendBuffer = NULL;
2968 // notity the thread
2969 ca_cond_signal(g_threadCond);
2970 CALEClientSetSendFinishFlag(true);
2971 OIC_LOG(DEBUG, TAG, "set signal for send data");
2974 ca_mutex_unlock(g_threadMutex);
2977 CAResult_t CALEClientInitGattMutexVaraibles()
2979 OIC_LOG(DEBUG, TAG, "IN");
2981 if (NULL == g_bleReqRespClientCbMutex)
2983 g_bleReqRespClientCbMutex = ca_mutex_new();
2984 if (NULL == g_bleReqRespClientCbMutex)
2986 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2987 return CA_STATUS_FAILED;
2991 if (NULL == g_bleServerBDAddressMutex)
2993 g_bleServerBDAddressMutex = ca_mutex_new();
2994 if (NULL == g_bleServerBDAddressMutex)
2996 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2997 return CA_STATUS_FAILED;
3001 if (NULL == g_threadMutex)
3003 g_threadMutex = ca_mutex_new();
3004 if (NULL == g_threadMutex)
3006 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3007 return CA_STATUS_FAILED;
3011 if (NULL == g_threadSendMutex)
3013 g_threadSendMutex = ca_mutex_new();
3014 if (NULL == g_threadSendMutex)
3016 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3017 return CA_STATUS_FAILED;
3021 if (NULL == g_threadSendCBMutex)
3023 g_threadSendCBMutex = ca_mutex_new();
3024 if (NULL == g_threadSendCBMutex)
3026 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3027 return CA_STATUS_FAILED;
3031 if (NULL == g_deviceListMutex)
3033 g_deviceListMutex = ca_mutex_new();
3034 if (NULL == g_deviceListMutex)
3036 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3037 return CA_STATUS_FAILED;
3041 if (NULL == g_gattObjectMutex)
3043 g_gattObjectMutex = ca_mutex_new();
3044 if (NULL == g_gattObjectMutex)
3046 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3047 return CA_STATUS_FAILED;
3051 if (NULL == g_deviceStateListMutex)
3053 g_deviceStateListMutex = ca_mutex_new();
3054 if (NULL == g_deviceStateListMutex)
3056 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3057 return CA_STATUS_FAILED;
3061 if (NULL == g_SendFinishMutex)
3063 g_SendFinishMutex = ca_mutex_new();
3064 if (NULL == g_SendFinishMutex)
3066 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3067 return CA_STATUS_FAILED;
3071 if (NULL == g_writeCharacteristicCBMutex)
3073 g_writeCharacteristicCBMutex = ca_mutex_new();
3074 if (NULL == g_writeCharacteristicCBMutex)
3076 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3077 return CA_STATUS_FAILED;
3081 if (NULL == g_theSendRequestMutex)
3083 g_theSendRequestMutex = ca_mutex_new();
3084 if (NULL == g_theSendRequestMutex)
3086 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3087 return CA_STATUS_FAILED;
3091 OIC_LOG(DEBUG, TAG, "OUT");
3092 return CA_STATUS_OK;
3095 void CALEClientTerminateGattMutexVariables()
3097 OIC_LOG(DEBUG, TAG, "IN");
3099 ca_mutex_free(g_bleReqRespClientCbMutex);
3100 g_bleReqRespClientCbMutex = NULL;
3102 ca_mutex_free(g_bleServerBDAddressMutex);
3103 g_bleServerBDAddressMutex = NULL;
3105 ca_mutex_free(g_threadMutex);
3106 g_threadMutex = NULL;
3108 ca_mutex_free(g_threadSendMutex);
3109 g_threadSendMutex = NULL;
3111 ca_mutex_free(g_threadSendCBMutex);
3112 g_threadSendCBMutex = NULL;
3114 ca_mutex_free(g_deviceListMutex);
3115 g_deviceListMutex = NULL;
3117 ca_mutex_free(g_SendFinishMutex);
3118 g_SendFinishMutex = NULL;
3120 ca_mutex_free(g_writeCharacteristicCBMutex);
3121 g_writeCharacteristicCBMutex = NULL;
3123 ca_mutex_free(g_theSendRequestMutex);
3124 g_theSendRequestMutex = NULL;
3126 OIC_LOG(DEBUG, TAG, "OUT");
3129 void CALEClientSetSendFinishFlag(bool flag)
3131 OIC_LOG_V(DEBUG, TAG, "g_isFinishedSendData is %d", flag);
3133 ca_mutex_lock(g_SendFinishMutex);
3134 g_isFinishedSendData = flag;
3135 ca_mutex_unlock(g_SendFinishMutex);
3138 void CALEClientSetWriteCharacteristicCBFlag(bool flag)
3140 OIC_LOG_V(DEBUG, TAG, "g_isReceivedWriteCB is %d", flag);
3142 ca_mutex_lock(g_writeCharacteristicCBMutex);
3143 g_isReceivedWriteCB = flag;
3144 ca_mutex_unlock(g_writeCharacteristicCBMutex);
3147 void CALEClientSetTheSendRequestFlag(bool flag)
3149 OIC_LOG_V(DEBUG, TAG, "g_isRequestedSend is %d", flag);
3151 ca_mutex_lock(g_theSendRequestMutex);
3152 g_isRequestedSend = flag;
3153 ca_mutex_unlock(g_theSendRequestMutex);
3160 CAResult_t CAStartBLEGattClient()
3162 CAResult_t res = CALEClientStartMulticastServer();
3163 if (CA_STATUS_OK != res)
3165 OIC_LOG(ERROR, TAG, "CALEClientStartMulticastServer has failed");
3169 g_isStartedLEClient = true;
3175 void CAStopBLEGattClient()
3177 OIC_LOG(DEBUG, TAG, "CAStopBLEGattClient");
3181 OIC_LOG(ERROR, TAG, "g_jvm is null");
3185 bool isAttached = false;
3187 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
3190 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
3191 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
3195 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
3201 CAResult_t ret = CALEClientDisconnectAll(env);
3202 if (CA_STATUS_OK != ret)
3204 OIC_LOG(ERROR, TAG, "CALEClientDisconnectAll has failed");
3207 ret = CALEClientStopScan();
3208 if(CA_STATUS_OK != ret)
3210 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
3213 ca_cond_signal(g_threadCond);
3214 ca_cond_signal(g_threadSendCond);
3215 g_isStartedLEClient = false;
3219 (*g_jvm)->DetachCurrentThread(g_jvm);
3224 void CATerminateBLEGattClient()
3226 OIC_LOG(DEBUG, TAG, "Terminate GATT Client");
3227 CALEClientTerminate();
3230 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const char *data,
3231 const uint32_t dataLen, CALETransferType_t type,
3232 const int32_t position)
3234 OIC_LOG(DEBUG, TAG, "call CALEClientSendUnicastMessage");
3235 VERIFY_NON_NULL(data, TAG, "data is null");
3236 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
3238 return CALEClientSendUnicastMessage(remoteAddress, data, dataLen);
3241 CAResult_t CAUpdateCharacteristicsToAllGattServers(const char *data, uint32_t dataLen)
3243 OIC_LOG(DEBUG, TAG, "call CALEClientSendMulticastMessage");
3244 VERIFY_NON_NULL(data, TAG, "data is null");
3246 return CALEClientSendMulticastMessage(data, dataLen);
3249 void CASetBLEReqRespClientCallback(CABLEClientDataReceivedCallback callback)
3251 OIC_LOG(DEBUG, TAG, "IN");
3253 ca_mutex_lock(g_bleReqRespClientCbMutex);
3254 g_CABLEClientDataReceivedCallback = callback;
3255 ca_mutex_unlock(g_bleReqRespClientCbMutex);
3257 OIC_LOG(DEBUG, TAG, "OUT");
3260 void CASetBleClientThreadPoolHandle(ca_thread_pool_t handle)
3262 OIC_LOG(DEBUG, TAG, "IN");
3264 CALEClientInitialize(handle);
3266 OIC_LOG(DEBUG, TAG, "OUT");
3269 CAResult_t CAGetLEAddress(char **local_address)
3271 OIC_LOG(DEBUG, TAG, "IN");
3273 VERIFY_NON_NULL(local_address, TAG, "local_address is null");
3274 CAResult_t res = CALEClientGetInterfaceInfo(local_address);
3275 if (CA_STATUS_OK != res)
3277 OIC_LOG(INFO, TAG, "it didn't get local address");
3280 OIC_LOG(DEBUG, TAG, "OUT");
3281 return CA_STATUS_OK;
3284 JNIEXPORT void JNICALL
3285 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, jobject obj,
3288 OIC_LOG(DEBUG, TAG, "CaLeRegisterLeScanCallback");
3289 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3290 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3292 g_leScanCallback = (*env)->NewGlobalRef(env, callback);
3295 JNIEXPORT void JNICALL
3296 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, jobject obj,
3299 OIC_LOG(DEBUG, TAG, "CaLeRegisterGattCallback");
3300 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3301 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3303 g_leGattCallback = (*env)->NewGlobalRef(env, callback);
3306 JNIEXPORT void JNICALL
3307 Java_org_iotivity_ca_CaLeClientInterface_caLeScanCallback(JNIEnv *env, jobject obj,
3308 jobject device, jint rssi,
3309 jbyteArray scanRecord)
3311 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3312 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
3314 CAResult_t res = CALEClientAddScanDeviceToList(env, device);
3315 if (CA_STATUS_OK != res)
3317 OIC_LOG_V(ERROR, TAG, "CALEClientAddScanDeviceToList has failed : %d", res);
3322 * Class: org_iotivity_ca_jar_caleinterface
3323 * Method: CALeGattConnectionStateChangeCallback
3324 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3326 JNIEXPORT void JNICALL
3327 Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env,
3333 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status,
3335 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3336 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3338 if (GATT_SUCCESS == status && STATE_CONNECTED == newstate) // le connected
3340 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3346 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3349 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
3350 STATE_CHARACTER_NO_CHANGE,
3352 if (CA_STATUS_OK != res)
3354 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3355 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3358 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3361 CAResult_t res = CALEClientAddGattobjToList(env, gatt);
3362 if (CA_STATUS_OK != res)
3364 OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed");
3368 res = CALEClientDiscoverServices(env, gatt);
3369 if (CA_STATUS_OK != res)
3371 OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed");
3375 else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate) // le disconnected
3377 CAResult_t res = CALEClientStartScan();
3378 if (CA_STATUS_OK != res)
3380 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
3383 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3386 OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
3389 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3392 res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3393 STATE_CHARACTER_NO_CHANGE,
3395 if (CA_STATUS_OK != res)
3397 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3399 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3402 res = CALEClientGattClose(env, gatt);
3403 if (CA_STATUS_OK != res)
3405 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3408 ca_cond_signal(g_threadSendCond);
3413 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3416 OIC_LOG(ERROR, TAG, "jni_address is null");
3421 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3424 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3425 STATE_CHARACTER_NO_CHANGE,
3427 if (CA_STATUS_OK != res)
3429 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3432 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3441 CALEClientSendFinish(env, gatt);
3442 ca_cond_signal(g_threadSendCond);
3447 * Class: org_iotivity_ca_jar_caleinterface
3448 * Method: CALeGattServicesDiscoveredCallback
3449 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3451 JNIEXPORT void JNICALL
3452 Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env,
3457 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
3458 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3459 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3461 if (0 != status) // discovery error
3463 CALEClientSendFinish(env, gatt);
3464 ca_cond_signal(g_threadSendCond);
3468 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3471 CALEClientSendFinish(env, gatt);
3472 ca_cond_signal(g_threadSendCond);
3476 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3479 CALEClientSendFinish(env, gatt);
3480 ca_cond_signal(g_threadSendCond);
3484 if (!CALEClientIsSetCharacteristic(address))
3486 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
3489 OIC_LOG(ERROR, TAG, "jni_uuid is null");
3493 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid);
3494 if (!jni_obj_GattCharacteristic)
3496 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
3500 CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt,
3501 jni_obj_GattCharacteristic);
3502 if (CA_STATUS_OK != res)
3504 OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed");
3508 res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic);
3509 if (CA_STATUS_OK != res)
3511 OIC_LOG(ERROR, TAG, "CALEClientSetUUIDToDescriptor has failed");
3515 res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3517 if (CA_STATUS_OK != res)
3519 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3525 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3526 if (CA_STATUS_OK != res)
3528 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3532 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3537 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3538 CALEClientSendFinish(env, gatt);
3539 ca_cond_signal(g_threadSendCond);
3544 * Class: org_iotivity_ca_jar_caleinterface
3545 * Method: CALeGattCharacteristicReadCallback
3546 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3548 JNIEXPORT void JNICALL
3549 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicReadCallback(JNIEnv *env,
3552 jobject characteristic,
3556 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicReadCallback - status : %d", status);
3560 * Class: org_iotivity_ca_jar_caleinterface
3561 * Method: CALeGattCharacteristicWritjclasseCallback
3562 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3564 JNIEXPORT void JNICALL
3565 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback(
3566 JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data,
3569 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
3570 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3571 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3573 ca_mutex_lock(g_threadSendCBMutex);
3574 if (!g_isRequestedSend)
3576 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - waiting");
3577 ca_cond_wait(g_threadSendCBCond, g_threadSendCBMutex);
3579 ca_mutex_unlock(g_threadSendCBMutex);
3582 char* wroteData = (char*) (*env)->GetByteArrayElements(env, data, &isCopy);
3584 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
3586 // send success & signal
3587 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3593 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3599 if (GATT_SUCCESS != status) // error case
3601 OIC_LOG(ERROR, TAG, "send failure");
3602 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3604 if (CA_STATUS_OK != res)
3606 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3608 CALEClientSendFinish(env, gatt);
3612 OIC_LOG(DEBUG, TAG, "send success");
3613 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3614 STATE_SEND_SUCCESS);
3615 if (CA_STATUS_OK != res)
3617 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3619 CALEClientUpdateSendCnt(env);
3622 CALEClientSetWriteCharacteristicCBFlag(true);
3623 CALEClientSetTheSendRequestFlag(false);
3624 ca_cond_signal(g_threadSendCond);
3625 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3631 CALEClientSetWriteCharacteristicCBFlag(true);
3632 CALEClientSetTheSendRequestFlag(false);
3633 CALEClientSendFinish(env, gatt);
3634 ca_cond_signal(g_threadSendCond);
3639 * Class: org_iotivity_ca_jar_caleinterface
3640 * Method: CALeGattCharacteristicChangedCallback
3641 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
3643 JNIEXPORT void JNICALL
3644 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback(
3645 JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data)
3647 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
3648 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3649 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3650 VERIFY_NON_NULL_VOID(data, TAG, "data is null");
3652 // get Byte Array and covert to char*
3653 jint length = (*env)->GetArrayLength(env, data);
3656 jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy);
3658 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - raw data received : %s",
3659 jni_byte_responseData);
3661 char* receivedData = (char*) OICMalloc(sizeof(char) * length + 1);
3664 OIC_LOG(ERROR, TAG, "recevicedData is null");
3668 memcpy(receivedData, (const char*) jni_byte_responseData, length);
3669 receivedData[length] = '\0';
3670 (*env)->ReleaseByteArrayElements(env, data, jni_byte_responseData, JNI_ABORT);
3672 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3675 OIC_LOG(ERROR, TAG, "jni_address is null");
3676 OICFree(receivedData);
3680 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3683 OIC_LOG(ERROR, TAG, "address is null");
3684 OICFree(receivedData);
3688 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %s, %d",
3689 receivedData, length);
3691 ca_mutex_lock(g_bleServerBDAddressMutex);
3692 uint32_t sentLength = 0;
3693 g_CABLEClientDataReceivedCallback(address, OIC_GATT_SERVICE_UUID, receivedData, length,
3695 ca_mutex_unlock(g_bleServerBDAddressMutex);
3697 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3701 * Class: org_iotivity_ca_jar_caleinterface
3702 * Method: CALeGattDescriptorReadCallback
3703 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3705 JNIEXPORT void JNICALL
3706 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorReadCallback(JNIEnv *env, jobject obj,
3711 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorReadCallback - status %d: ", status);
3715 * Class: org_iotivity_ca_jar_caleinterface
3716 * Method: CALeGattDescriptorWriteCallback
3717 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3719 JNIEXPORT void JNICALL
3720 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv *env, jobject obj,
3725 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
3726 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3727 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3729 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3730 if (CA_STATUS_OK != res)
3732 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3740 CALEClientSendFinish(env, gatt);
3741 ca_cond_signal(g_threadSendCond);
3746 * Class: org_iotivity_ca_jar_caleinterface
3747 * Method: CALeGattReliableWriteCompletedCallback
3748 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3750 JNIEXPORT void JNICALL
3751 Java_org_iotivity_ca_CaLeClientInterface_caLeGattReliableWriteCompletedCallback(JNIEnv *env,
3756 OIC_LOG_V(DEBUG, TAG, "CALeGattReliableWriteCompletedCallback - status %d: ", status);
3760 * Class: org_iotivity_ca_jar_caleinterface
3761 * Method: CALeGattReadRemoteRssiCallback
3762 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3764 JNIEXPORT void JNICALL
3765 Java_org_iotivity_ca_CaLeClientInterface_caLeGattReadRemoteRssiCallback(JNIEnv *env, jobject obj,
3766 jobject gatt, jint rssi,
3769 OIC_LOG_V(DEBUG, TAG, "CALeGattReadRemoteRssiCallback - rssi %d, status %d: ", rssi, status);