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;
692 for (uint32_t index = 0; index < length; index++)
694 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
697 OIC_LOG(ERROR, TAG, "jarrayObj is not available");
701 res = CALEClientSendData(env, jarrayObj);
702 if (res != CA_STATUS_OK)
704 OIC_LOG(ERROR, TAG, "BT device[%d] - send has failed");
708 CALEClientSetTheSendRequestFlag(true);
709 ca_cond_signal(g_threadSendCBCond);
711 if (!g_isReceivedWriteCB)
713 OIC_LOG(INFO, TAG, "wait..(multicast)");
714 ca_cond_wait(g_threadSendCond, g_threadSendMutex);
718 CALEClientSetWriteCharacteristicCBFlag(false);
722 jstring jni_address = CALEGetAddressFromBTDevice(env, jarrayObj);
725 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
729 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
732 OIC_LOG(ERROR, TAG, "address is not available");
736 res = CALECheckSendState(address);
737 if (CA_STATUS_OK != res)
739 OIC_LOG_V(INFO, TAG, "multicast : send has failed for this device[%s]", address);
740 (*env)->ReleaseStringUTFChars(env, jni_address, address);
744 (*env)->ReleaseStringUTFChars(env, jni_address, address);
747 OIC_LOG(DEBUG, TAG, "connection routine is finished");
749 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
750 if (!g_isFinishedSendData)
752 ca_mutex_lock(g_threadMutex);
753 ca_cond_wait(g_threadCond, g_threadMutex);
754 OIC_LOG(DEBUG, TAG, "the data was sent for All devices");
755 ca_mutex_unlock(g_threadMutex);
758 // start LE Scan again
759 res = CALEClientStartScan();
760 if (CA_STATUS_OK != res)
762 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
763 ca_mutex_unlock(g_threadSendMutex);
767 ca_mutex_unlock(g_threadSendMutex);
768 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
772 res = CALEClientStartScan();
773 if (CA_STATUS_OK != res)
775 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
776 ca_mutex_unlock(g_threadSendMutex);
780 ca_mutex_unlock(g_threadSendMutex);
781 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
782 return CA_SEND_FAILED;
785 CAResult_t CALECheckSendState(const char* address)
787 VERIFY_NON_NULL(address, TAG, "address is null");
789 ca_mutex_lock(g_deviceStateListMutex);
790 CALEState_t* state = CALEClientGetStateInfo(address);
793 OIC_LOG(ERROR, TAG, "state is null");
794 ca_mutex_unlock(g_deviceStateListMutex);
795 return CA_SEND_FAILED;
798 if (STATE_SEND_SUCCESS != state->sendState)
800 OIC_LOG(ERROR, TAG, "sendstate is not STATE_SEND_SUCCESS");
801 ca_mutex_unlock(g_deviceStateListMutex);
802 return CA_SEND_FAILED;
804 ca_mutex_unlock(g_deviceStateListMutex);
808 CAResult_t CALEClientSendData(JNIEnv *env, jobject device)
810 OIC_LOG(DEBUG, TAG, "IN - CALEClientSendData");
811 VERIFY_NON_NULL(device, TAG, "device is null");
812 VERIFY_NON_NULL(env, TAG, "env is null");
814 jstring jni_address = CALEGetAddressFromBTDevice(env, device);
817 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
818 return CA_STATUS_FAILED;
821 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
824 OIC_LOG(ERROR, TAG, "address is not available");
825 return CA_STATUS_FAILED;
828 ca_mutex_lock(g_deviceStateListMutex);
829 CALEState_t* state = CALEClientGetStateInfo(address);
830 ca_mutex_unlock(g_deviceStateListMutex);
833 OIC_LOG(DEBUG, TAG, "state is empty..start to connect LE");
834 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
835 if (CA_STATUS_OK != ret)
837 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
838 (*env)->ReleaseStringUTFChars(env, jni_address, address);
844 if (STATE_CONNECTED == state->connectedState)
846 OIC_LOG(INFO, TAG, "GATT has already connected");
847 jobject gatt = CALEClientGetGattObjInList(env, address);
850 OIC_LOG(ERROR, TAG, "CALEClientGetGattObjInList has failed");
851 (*env)->ReleaseStringUTFChars(env, jni_address, address);
852 return CA_STATUS_FAILED;
855 CAResult_t ret = CALEClientWriteCharacteristic(env, gatt);
856 if (CA_STATUS_OK != ret)
858 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
859 (*env)->ReleaseStringUTFChars(env, jni_address, address);
865 OIC_LOG(DEBUG, TAG, "start to connect LE");
866 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
867 if (CA_STATUS_OK != ret)
869 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
870 (*env)->ReleaseStringUTFChars(env, jni_address, address);
876 (*env)->ReleaseStringUTFChars(env, jni_address, address);
880 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt)
882 VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
883 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
885 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
886 if (!jni_cid_gattdevice_list)
888 OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null");
892 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
893 "()Landroid/bluetooth/BluetoothDevice;");
894 if (!jni_mid_getDevice)
896 OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
900 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
903 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
907 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
910 OIC_LOG(ERROR, TAG, "jni_address is null");
920 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt)
923 OIC_LOG(DEBUG, TAG, "Gatt Close");
924 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
925 VERIFY_NON_NULL(env, TAG, "env is null");
927 // get BluetoothGatt class
928 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
929 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
930 if (!jni_cid_BluetoothGatt)
932 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
933 return CA_STATUS_FAILED;
936 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V");
937 if (!jni_mid_closeGatt)
939 OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null");
943 // call disconnect gatt method
944 OIC_LOG(DEBUG, TAG, "request to close GATT");
945 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
947 if ((*env)->ExceptionCheck(env))
949 OIC_LOG(ERROR, TAG, "closeGATT has failed");
950 (*env)->ExceptionDescribe(env);
951 (*env)->ExceptionClear(env);
952 return CA_STATUS_FAILED;
958 CAResult_t CALEClientStartScan()
960 if (!g_isStartedMulticastServer)
962 OIC_LOG(ERROR, TAG, "server is not started yet..scan will be passed");
963 return CA_STATUS_FAILED;
966 if (!g_isStartedLEClient)
968 OIC_LOG(ERROR, TAG, "LE client is not started");
969 return CA_STATUS_FAILED;
974 OIC_LOG(ERROR, TAG, "g_jvm is null");
975 return CA_STATUS_FAILED;
980 OIC_LOG(INFO, TAG, "scanning is already started");
984 bool isAttached = false;
986 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
989 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
991 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
994 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
995 return CA_STATUS_FAILED;
1000 OIC_LOG(DEBUG, TAG, "CALEClientStartScan");
1002 CAResult_t ret = CA_STATUS_OK;
1003 // scan gatt server with UUID
1004 if (g_leScanCallback && g_uuidList)
1007 ret = CALEClientStartScanWithUUIDImpl(env, g_uuidList, g_leScanCallback);
1008 if(CA_STATUS_OK != ret)
1010 OIC_LOG(ERROR, TAG, "CALEClientStartScanWithUUIDImpl has failed");
1013 ret = CALEClientStartScanImpl(env, g_leScanCallback);
1014 if (CA_STATUS_OK != ret)
1016 OIC_LOG(ERROR, TAG, "CALEClientStartScanImpl has failed");
1023 (*g_jvm)->DetachCurrentThread(g_jvm);
1029 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback)
1031 VERIFY_NON_NULL(callback, TAG, "callback is null");
1032 VERIFY_NON_NULL(env, TAG, "env is null");
1034 if (!CALEIsEnableBTAdapter(env))
1036 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1037 return CA_ADAPTER_NOT_ENABLED;
1040 // get default bt adapter class
1041 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1042 if (!jni_cid_BTAdapter)
1044 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1045 return CA_STATUS_FAILED;
1048 // get remote bt adapter method
1049 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1050 "getDefaultAdapter",
1051 METHODID_OBJECTNONPARAM);
1052 if (!jni_mid_getDefaultAdapter)
1054 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1055 return CA_STATUS_FAILED;
1058 // get start le scan method
1059 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1060 "(Landroid/bluetooth/BluetoothAdapter$"
1061 "LeScanCallback;)Z");
1062 if (!jni_mid_startLeScan)
1064 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1065 return CA_STATUS_FAILED;
1068 // gat bt adapter object
1069 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1070 jni_mid_getDefaultAdapter);
1071 if (!jni_obj_BTAdapter)
1073 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1074 return CA_STATUS_FAILED;
1077 // call start le scan method
1078 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1079 jni_mid_startLeScan, callback);
1080 if (!jni_obj_startLeScan)
1082 OIC_LOG(ERROR, TAG, "startLeScan is failed");
1083 return CA_STATUS_FAILED;
1087 OIC_LOG(DEBUG, TAG, "startLeScan is started");
1088 g_isStartedScan = true;
1091 return CA_STATUS_OK;
1094 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
1096 VERIFY_NON_NULL(callback, TAG, "callback is null");
1097 VERIFY_NON_NULL(uuids, TAG, "uuids is null");
1098 VERIFY_NON_NULL(env, TAG, "env is null");
1100 if (!CALEIsEnableBTAdapter(env))
1102 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1103 return CA_ADAPTER_NOT_ENABLED;
1106 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1107 if (!jni_cid_BTAdapter)
1109 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1110 return CA_STATUS_FAILED;
1113 // get remote bt adapter method
1114 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1115 "getDefaultAdapter",
1116 METHODID_OBJECTNONPARAM);
1117 if (!jni_mid_getDefaultAdapter)
1119 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1120 return CA_STATUS_FAILED;
1123 // get start le scan method
1124 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1125 "([Ljava/util/UUID;Landroid/bluetooth/"
1126 "BluetoothAdapter$LeScanCallback;)Z");
1127 if (!jni_mid_startLeScan)
1129 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1130 return CA_STATUS_FAILED;
1133 // get bt adapter object
1134 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1135 jni_mid_getDefaultAdapter);
1136 if (!jni_obj_BTAdapter)
1138 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1139 return CA_STATUS_FAILED;
1142 // call start le scan method
1143 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1144 jni_mid_startLeScan, uuids, callback);
1145 if (!jni_obj_startLeScan)
1147 OIC_LOG(ERROR, TAG, "startLeScan With UUID is failed");
1148 return CA_STATUS_FAILED;
1152 OIC_LOG(DEBUG, TAG, "startLeScan With UUID is started");
1153 g_isStartedScan = true;
1156 return CA_STATUS_OK;
1159 jobject CALEClientGetUUIDObject(JNIEnv *env, const char* uuid)
1161 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
1162 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1165 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1168 OIC_LOG(ERROR, TAG, "jni_cid_uuid is null");
1172 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString",
1173 "(Ljava/lang/String;)"
1174 "Ljava/util/UUID;");
1175 if (!jni_mid_fromString)
1177 OIC_LOG(ERROR, TAG, "jni_mid_fromString is null");
1181 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1182 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString,
1186 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1190 return jni_obj_uuid;
1193 CAResult_t CALEClientStopScan()
1197 OIC_LOG(ERROR, TAG, "g_jvm is null");
1198 return CA_STATUS_FAILED;
1201 if (!g_isStartedScan)
1203 OIC_LOG(INFO, TAG, "scanning is already stopped");
1204 return CA_STATUS_OK;
1207 bool isAttached = false;
1209 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1212 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1213 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1216 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1217 return CA_STATUS_FAILED;
1222 CAResult_t ret = CALEClientStopScanImpl(env, g_leScanCallback);
1223 if (CA_STATUS_OK != ret)
1225 OIC_LOG(ERROR, TAG, "CALEClientStopScanImpl has failed");
1229 g_isStartedScan = false;
1234 (*g_jvm)->DetachCurrentThread(g_jvm);
1240 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback)
1242 OIC_LOG(DEBUG, TAG, "CALEClientStopScanImpl");
1243 VERIFY_NON_NULL(callback, TAG, "callback is null");
1244 VERIFY_NON_NULL(env, TAG, "env is null");
1246 if (!CALEIsEnableBTAdapter(env))
1248 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1249 return CA_ADAPTER_NOT_ENABLED;
1252 // get default bt adapter class
1253 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1254 if (!jni_cid_BTAdapter)
1256 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1257 return CA_STATUS_FAILED;
1260 // get remote bt adapter method
1261 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1262 "getDefaultAdapter",
1263 METHODID_OBJECTNONPARAM);
1264 if (!jni_mid_getDefaultAdapter)
1266 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1267 return CA_STATUS_FAILED;
1270 // get start le scan method
1271 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1272 "(Landroid/bluetooth/"
1273 "BluetoothAdapter$LeScanCallback;)V");
1274 if (!jni_mid_stopLeScan)
1276 OIC_LOG(ERROR, TAG, "stopLeScan: jni_mid_stopLeScan is null");
1277 return CA_STATUS_FAILED;
1280 // gat bt adapter object
1281 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1282 jni_mid_getDefaultAdapter);
1283 if (!jni_obj_BTAdapter)
1285 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
1286 return CA_STATUS_FAILED;
1289 OIC_LOG(DEBUG, TAG, "CALL API - request to stop LE Scan");
1290 // call start le scan method
1291 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1292 if ((*env)->ExceptionCheck(env))
1294 OIC_LOG(ERROR, TAG, "stopLeScan has failed");
1295 (*env)->ExceptionDescribe(env);
1296 (*env)->ExceptionClear(env);
1297 return CA_STATUS_FAILED;
1300 return CA_STATUS_OK;
1303 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
1306 OIC_LOG(DEBUG, TAG, "GATT CONNECT");
1307 VERIFY_NON_NULL(env, TAG, "env is null");
1308 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1309 VERIFY_NON_NULL(callback, TAG, "callback is null");
1311 if (!CALEIsEnableBTAdapter(env))
1313 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1314 return CA_ADAPTER_NOT_ENABLED;
1317 jstring jni_address = CALEGetAddressFromBTDevice(env, bluetoothDevice);
1320 OIC_LOG(ERROR, TAG, "bleConnect: CALEGetAddressFromBTDevice is null");
1321 return CA_STATUS_FAILED;
1324 // get BluetoothDevice class
1325 OIC_LOG(DEBUG, TAG, "get BluetoothDevice class");
1326 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1327 if (!jni_cid_BluetoothDevice)
1329 OIC_LOG(ERROR, TAG, "bleConnect: jni_cid_BluetoothDevice is null");
1330 return CA_STATUS_FAILED;
1333 // get connectGatt method
1334 OIC_LOG(DEBUG, TAG, "get connectGatt method");
1335 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice, "connectGatt",
1336 "(Landroid/content/Context;ZLandroid/"
1337 "bluetooth/BluetoothGattCallback;)"
1338 "Landroid/bluetooth/BluetoothGatt;");
1339 if (!jni_mid_connectGatt)
1341 OIC_LOG(ERROR, TAG, "bleConnect: jni_mid_connectGatt is null");
1342 return CA_STATUS_FAILED;
1345 OIC_LOG(DEBUG, TAG, "Call object method - connectGatt");
1346 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice,
1347 jni_mid_connectGatt,
1349 autoconnect, callback);
1350 if (!jni_obj_connectGatt)
1352 OIC_LOG(ERROR, TAG, "CALL API - connectGatt was failed..it will be removed");
1353 CALEClientRemoveDeviceInScanDeviceList(env, jni_address);
1354 CALEClientUpdateSendCnt(env);
1355 return CA_STATUS_FAILED;
1359 OIC_LOG(DEBUG, TAG, "le connecting..please wait..");
1361 return CA_STATUS_OK;
1364 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt)
1366 OIC_LOG(DEBUG, TAG, "GATT DISCONNECT");
1367 VERIFY_NON_NULL(env, TAG, "env is null");
1368 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1370 if (!CALEIsEnableBTAdapter(env))
1372 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1373 return CA_ADAPTER_NOT_ENABLED;
1376 // get BluetoothGatt class
1377 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1378 if (!jni_cid_BluetoothGatt)
1380 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1381 return CA_STATUS_FAILED;
1384 OIC_LOG(DEBUG, TAG, "get gatt disconnect method");
1385 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1386 "disconnect", "()V");
1387 if (!jni_mid_disconnectGatt)
1389 OIC_LOG(ERROR, TAG, "jni_mid_disconnectGatt is null");
1390 return CA_STATUS_FAILED;
1393 // call disconnect gatt method
1394 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1395 if ((*env)->ExceptionCheck(env))
1397 OIC_LOG(ERROR, TAG, "disconnect has failed");
1398 (*env)->ExceptionDescribe(env);
1399 (*env)->ExceptionClear(env);
1400 return CA_STATUS_FAILED;
1403 OIC_LOG(DEBUG, TAG, "disconnecting Gatt...");
1405 return CA_STATUS_OK;
1408 CAResult_t CALEClientDisconnectAll(JNIEnv *env)
1410 OIC_LOG(DEBUG, TAG, "CALEClientDisconnectAll");
1411 VERIFY_NON_NULL(env, TAG, "env is null");
1413 if (!g_gattObjectList)
1415 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
1416 return CA_STATUS_FAILED;
1419 uint32_t length = u_arraylist_length(g_gattObjectList);
1420 for (uint32_t index = 0; index < length; index++)
1422 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
1425 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1428 CAResult_t res = CALEClientDisconnect(env, jarrayObj);
1429 if (CA_STATUS_OK != res)
1431 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
1436 OICFree(g_gattObjectList);
1437 g_gattObjectList = NULL;
1439 return CA_STATUS_OK;
1442 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1444 VERIFY_NON_NULL(env, TAG, "env is null");
1445 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1447 if (!CALEIsEnableBTAdapter(env))
1449 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1450 return CA_ADAPTER_NOT_ENABLED;
1453 // get BluetoothGatt class
1454 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1455 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1456 if (!jni_cid_BluetoothGatt)
1458 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1459 return CA_STATUS_FAILED;
1462 OIC_LOG(DEBUG, TAG, "discovery gatt services method");
1463 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1464 "discoverServices", "()Z");
1465 if (!jni_mid_discoverServices)
1467 OIC_LOG(ERROR, TAG, "jni_mid_discoverServices is null");
1468 return CA_STATUS_FAILED;
1470 // call disconnect gatt method
1471 OIC_LOG(DEBUG, TAG, "CALL API - request discovery gatt services");
1472 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1475 OIC_LOG(ERROR, TAG, "discoverServices has not been started");
1476 return CA_STATUS_FAILED;
1479 return CA_STATUS_OK;
1482 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt)
1484 VERIFY_NON_NULL(env, TAG, "env is null");
1485 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
1488 jobject jni_obj_character = CALEClientCreateGattCharacteristic(env, gatt, g_sendBuffer);
1489 if (!jni_obj_character)
1491 CALEClientSendFinish(env, gatt);
1492 ca_cond_signal(g_threadSendCond);
1493 return CA_STATUS_FAILED;
1496 CAResult_t ret = CALEClientWriteCharacteristicImpl(env, gatt, jni_obj_character);
1497 if (CA_STATUS_OK != ret)
1499 CALEClientSendFinish(env, gatt);
1500 ca_cond_signal(g_threadSendCond);
1504 return CA_STATUS_OK;
1507 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
1508 jobject gattCharacteristic)
1510 OIC_LOG(DEBUG, TAG, "WRITE GATT CHARACTERISTIC");
1511 VERIFY_NON_NULL(env, TAG, "env is null");
1512 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1513 VERIFY_NON_NULL(gattCharacteristic, TAG, "gattCharacteristic is null");
1515 if (!CALEIsEnableBTAdapter(env))
1517 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1518 return CA_STATUS_FAILED;
1521 // get BluetoothGatt class
1522 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1523 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1524 if (!jni_cid_BluetoothGatt)
1526 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1527 return CA_STATUS_FAILED;
1530 OIC_LOG(DEBUG, TAG, "write characteristic method");
1531 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1532 "writeCharacteristic",
1533 "(Landroid/bluetooth/"
1534 "BluetoothGattCharacteristic;)Z");
1535 if (!jni_mid_writeCharacteristic)
1537 OIC_LOG(ERROR, TAG, "jni_mid_writeCharacteristic is null");
1538 return CA_STATUS_FAILED;
1541 // call disconnect gatt method
1542 OIC_LOG(DEBUG, TAG, "CALL API - request to write gatt characteristic");
1543 jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt,
1544 jni_mid_writeCharacteristic,
1545 gattCharacteristic);
1548 OIC_LOG(DEBUG, TAG, "writeCharacteristic success");
1552 OIC_LOG(ERROR, TAG, "writeCharacteristic has failed");
1553 return CA_STATUS_FAILED;
1556 return CA_STATUS_OK;
1559 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt)
1561 VERIFY_NON_NULL(env, TAG, "env is null");
1562 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1564 if (!CALEIsEnableBTAdapter(env))
1566 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1567 return CA_STATUS_FAILED;
1570 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1571 if (!jni_cid_BluetoothGatt)
1573 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1574 return CA_STATUS_FAILED;
1577 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
1580 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1581 return CA_STATUS_FAILED;
1584 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1585 if (!jni_obj_GattCharacteristic)
1587 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1588 return CA_STATUS_FAILED;
1591 OIC_LOG(DEBUG, TAG, "read characteristic method");
1592 jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1593 "readCharacteristic",
1594 "(Landroid/bluetooth/"
1595 "BluetoothGattCharacteristic;)Z");
1596 if (!jni_mid_readCharacteristic)
1598 OIC_LOG(ERROR, TAG, "jni_mid_readCharacteristic is null");
1599 return CA_STATUS_FAILED;
1602 // call disconnect gatt method
1603 OIC_LOG(DEBUG, TAG, "CALL API - request to read gatt characteristic");
1604 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic,
1605 jni_obj_GattCharacteristic);
1608 OIC_LOG(DEBUG, TAG, "readCharacteristic success");
1612 OIC_LOG(ERROR, TAG, "readCharacteristic has failed");
1613 return CA_STATUS_FAILED;
1616 return CA_STATUS_OK;
1619 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
1620 jobject characteristic)
1622 VERIFY_NON_NULL(env, TAG, "env is null");
1623 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1624 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1626 if (!CALEIsEnableBTAdapter(env))
1628 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1629 return CA_ADAPTER_NOT_ENABLED;
1632 // get BluetoothGatt class
1633 OIC_LOG(DEBUG, TAG, "CALEClientSetCharacteristicNotification");
1634 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1635 if (!jni_cid_BluetoothGatt)
1637 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1638 return CA_STATUS_FAILED;
1641 // set Characteristic Notification
1642 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1643 "setCharacteristicNotification",
1644 "(Landroid/bluetooth/"
1645 "BluetoothGattCharacteristic;Z)Z");
1646 if (!jni_mid_setNotification)
1648 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1649 return CA_STATUS_FAILED;
1652 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification,
1653 characteristic, JNI_TRUE);
1654 if (JNI_TRUE == ret)
1656 OIC_LOG(DEBUG, TAG, "CALL API - setCharacteristicNotification success");
1660 OIC_LOG(ERROR, TAG, "CALL API - setCharacteristicNotification has failed");
1661 return CA_STATUS_FAILED;
1664 return CA_STATUS_OK;
1667 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1669 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1670 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1671 VERIFY_NON_NULL_RET(characterUUID, TAG, "characterUUID is null", NULL);
1673 if (!CALEIsEnableBTAdapter(env))
1675 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1679 // get BluetoothGatt class
1680 OIC_LOG(DEBUG, TAG, "CALEClientGetGattService");
1681 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1682 if (!jni_cid_BluetoothGatt)
1684 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1688 jmethodID jni_mid_getService = (*env)->GetMethodID(
1689 env, jni_cid_BluetoothGatt, "getService",
1690 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1691 if (!jni_mid_getService)
1693 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1697 jobject jni_obj_service_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1698 if (!jni_obj_service_uuid)
1700 OIC_LOG(ERROR, TAG, "jni_obj_service_uuid is null");
1704 // get bluetooth gatt service
1705 OIC_LOG(DEBUG, TAG, "request to get service");
1706 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService,
1707 jni_obj_service_uuid);
1708 if (!jni_obj_gattService)
1710 OIC_LOG(ERROR, TAG, "jni_obj_gattService is null");
1714 // get bluetooth gatt service class
1715 jclass jni_cid_BluetoothGattService = (*env)->FindClass(
1716 env, "android/bluetooth/BluetoothGattService");
1717 if (!jni_cid_BluetoothGattService)
1719 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGattService is null");
1723 OIC_LOG(DEBUG, TAG, "get gatt getCharacteristic method");
1724 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService,
1725 "getCharacteristic",
1726 "(Ljava/util/UUID;)"
1727 "Landroid/bluetooth/"
1728 "BluetoothGattCharacteristic;");
1729 if (!jni_mid_getCharacteristic)
1731 OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null");
1735 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1738 OIC_LOG(ERROR, TAG, "uuid is null");
1742 jobject jni_obj_tx_uuid = CALEClientGetUUIDObject(env, uuid);
1743 if (!jni_obj_tx_uuid)
1745 OIC_LOG(ERROR, TAG, "jni_obj_tx_uuid is null");
1746 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1750 OIC_LOG(DEBUG, TAG, "request to get Characteristic");
1751 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService,
1752 jni_mid_getCharacteristic,
1755 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1756 return jni_obj_GattCharacteristic;
1759 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data)
1761 OIC_LOG(DEBUG, TAG, "CALEClientCreateGattCharacteristic");
1762 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1763 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1764 VERIFY_NON_NULL_RET(data, TAG, "data is null", NULL);
1766 if (!CALEIsEnableBTAdapter(env))
1768 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1772 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_REQUEST_UUID);
1775 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1779 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1780 if (!jni_obj_GattCharacteristic)
1782 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1786 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth"
1787 "/BluetoothGattCharacteristic");
1788 if (!jni_cid_BTGattCharacteristic)
1790 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1794 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1795 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1797 if (!jni_mid_setValue)
1799 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
1803 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue,
1805 if (JNI_TRUE == ret)
1807 OIC_LOG(DEBUG, TAG, "the locally stored value has been set");
1811 OIC_LOG(ERROR, TAG, "the locally stored value hasn't been set");
1816 jmethodID jni_mid_setWriteType = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
1817 "setWriteType", "(I)V");
1818 if (!jni_mid_setWriteType)
1820 OIC_LOG(ERROR, TAG, "jni_mid_setWriteType is null");
1824 jfieldID jni_fid_no_response = (*env)->GetStaticFieldID(env, jni_cid_BTGattCharacteristic,
1825 "WRITE_TYPE_NO_RESPONSE", "I");
1826 if (!jni_fid_no_response)
1828 OIC_LOG(ERROR, TAG, "jni_fid_no_response is not available");
1832 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTGattCharacteristic,
1833 jni_fid_no_response);
1835 (*env)->CallVoidMethod(env, jni_obj_GattCharacteristic, jni_mid_setWriteType, jni_int_val);
1837 return jni_obj_GattCharacteristic;
1840 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
1842 VERIFY_NON_NULL_RET(characteristic, TAG, "characteristic is null", NULL);
1843 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1845 if (!CALEIsEnableBTAdapter(env))
1847 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1851 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1852 "BluetoothGattCharacteristic");
1853 if (!jni_cid_BTGattCharacteristic)
1855 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1859 OIC_LOG(DEBUG, TAG, "get value in Characteristic");
1860 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
1862 if (!jni_mid_getValue)
1864 OIC_LOG(ERROR, TAG, "jni_mid_getValue is null");
1868 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic,
1870 return jni_obj_data_array;
1873 CAResult_t CALEClientCreateUUIDList()
1877 OIC_LOG(ERROR, TAG, "g_jvm is null");
1878 return CA_STATUS_FAILED;
1881 bool isAttached = false;
1883 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1886 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1887 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1891 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1892 return CA_STATUS_FAILED;
1897 // create new object array
1898 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1899 if (!jni_cid_uuid_list)
1901 OIC_LOG(ERROR, TAG, "jni_cid_uuid_list is null");
1905 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1,
1906 jni_cid_uuid_list, NULL);
1907 if (!jni_obj_uuid_list)
1909 OIC_LOG(ERROR, TAG, "jni_obj_uuid_list is null");
1914 jobject jni_obj_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1917 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1920 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
1922 g_uuidList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list);
1926 (*g_jvm)->DetachCurrentThread(g_jvm);
1929 return CA_STATUS_OK;
1936 (*g_jvm)->DetachCurrentThread(g_jvm);
1938 return CA_STATUS_FAILED;
1941 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
1942 jobject characteristic)
1944 VERIFY_NON_NULL(env, TAG, "env is null");
1945 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1946 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1948 if (!CALEIsEnableBTAdapter(env))
1950 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1951 return CA_ADAPTER_NOT_ENABLED;
1954 OIC_LOG(DEBUG, TAG, "CALEClientSetUUIDToDescriptor");
1955 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1956 "BluetoothGattCharacteristic");
1957 if (!jni_cid_BTGattCharacteristic)
1959 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1960 return CA_STATUS_FAILED;
1963 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1964 jmethodID jni_mid_getDescriptor = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
1966 "(Ljava/util/UUID;)Landroid/bluetooth/"
1967 "BluetoothGattDescriptor;");
1968 if (!jni_mid_getDescriptor)
1970 OIC_LOG(ERROR, TAG, "jni_mid_getDescriptor is null");
1971 return CA_STATUS_FAILED;
1974 jobject jni_obj_cc_uuid = CALEClientGetUUIDObject(env, OIC_GATT_CHARACTERISTIC_CONFIG_UUID);
1975 if (!jni_obj_cc_uuid)
1977 OIC_LOG(ERROR, TAG, "jni_obj_cc_uuid is null");
1978 return CA_STATUS_FAILED;
1981 OIC_LOG(DEBUG, TAG, "request to get descriptor");
1982 jobject jni_obj_descriptor = (*env)->CallObjectMethod(env, characteristic,
1983 jni_mid_getDescriptor, jni_obj_cc_uuid);
1984 if (!jni_obj_descriptor)
1986 OIC_LOG(ERROR, TAG, "jni_obj_descriptor is null");
1987 return CA_STATUS_FAILED;
1990 OIC_LOG(DEBUG, TAG, "set value in descriptor");
1991 jclass jni_cid_descriptor = (*env)->FindClass(env,
1992 "android/bluetooth/BluetoothGattDescriptor");
1993 if (!jni_cid_descriptor)
1995 OIC_LOG(ERROR, TAG, "jni_cid_descriptor is null");
1996 return CA_STATUS_FAILED;
1999 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_descriptor, "setValue", "([B)Z");
2000 if (!jni_mid_setValue)
2002 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
2003 return CA_STATUS_FAILED;
2006 jfieldID jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor,
2007 "ENABLE_NOTIFICATION_VALUE", "[B");
2008 if (!jni_fid_NotiValue)
2010 OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null");
2011 return CA_STATUS_FAILED;
2014 OIC_LOG(DEBUG, TAG, "get ENABLE_NOTIFICATION_VALUE");
2016 jboolean jni_setvalue = (*env)->CallBooleanMethod(
2017 env, jni_obj_descriptor, jni_mid_setValue,
2018 (jbyteArray)(*env)->GetStaticObjectField(env, jni_cid_descriptor, jni_fid_NotiValue));
2021 OIC_LOG(DEBUG, TAG, "setValue success");
2025 OIC_LOG(ERROR, TAG, "setValue has failed");
2026 return CA_STATUS_FAILED;
2029 jclass jni_cid_gatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
2032 OIC_LOG(ERROR, TAG, "jni_cid_gatt is null");
2033 return CA_STATUS_FAILED;
2036 OIC_LOG(DEBUG, TAG, "write Descriptor in gatt object");
2037 jmethodID jni_mid_writeDescriptor = (*env)->GetMethodID(env, jni_cid_gatt, "writeDescriptor",
2038 "(Landroid/bluetooth/"
2039 "BluetoothGattDescriptor;)Z");
2040 if (!jni_mid_writeDescriptor)
2042 OIC_LOG(ERROR, TAG, "jni_mid_writeDescriptor is null");
2043 return CA_STATUS_FAILED;
2046 OIC_LOG(DEBUG, TAG, "request to write descriptor");
2047 jboolean jni_ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeDescriptor,
2048 jni_obj_descriptor);
2051 OIC_LOG(DEBUG, TAG, "writeDescriptor success");
2055 OIC_LOG(ERROR, TAG, "writeDescriptor has failed");
2056 return CA_STATUS_FAILED;
2059 return CA_STATUS_OK;
2062 void CALEClientCreateScanDeviceList(JNIEnv *env)
2064 OIC_LOG(DEBUG, TAG, "CALEClientCreateScanDeviceList");
2065 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2067 ca_mutex_lock(g_deviceListMutex);
2068 // create new object array
2069 if (g_deviceList == NULL)
2071 OIC_LOG(DEBUG, TAG, "Create device list");
2073 g_deviceList = u_arraylist_create();
2075 ca_mutex_unlock(g_deviceListMutex);
2078 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device)
2080 OIC_LOG(DEBUG, TAG, "IN - CALEClientAddScanDeviceToList");
2081 VERIFY_NON_NULL(device, TAG, "device is null");
2082 VERIFY_NON_NULL(env, TAG, "env is null");
2084 ca_mutex_lock(g_deviceListMutex);
2088 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2089 ca_mutex_unlock(g_deviceListMutex);
2090 return CA_STATUS_FAILED;
2093 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
2094 if (!jni_remoteAddress)
2096 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2097 ca_mutex_unlock(g_deviceListMutex);
2098 return CA_STATUS_FAILED;
2101 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2104 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2105 ca_mutex_unlock(g_deviceListMutex);
2106 return CA_STATUS_FAILED;
2109 if (!CALEClientIsDeviceInScanDeviceList(env, remoteAddress))
2111 jobject gdevice = (*env)->NewGlobalRef(env, device);
2112 u_arraylist_add(g_deviceList, gdevice);
2113 OIC_LOG(DEBUG, TAG, "Set Object to Array as Element");
2115 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2117 ca_mutex_unlock(g_deviceListMutex);
2119 OIC_LOG(DEBUG, TAG, "OUT - CALEClientAddScanDeviceToList");
2120 return CA_STATUS_OK;
2123 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char* remoteAddress)
2125 OIC_LOG(DEBUG, TAG, "IN - CALEClientIsDeviceInScanDeviceList");
2126 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2127 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2131 OIC_LOG(DEBUG, TAG, "g_deviceList is null");
2135 uint32_t length = u_arraylist_length(g_deviceList);
2136 for (uint32_t index = 0; index < length; index++)
2138 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2141 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2145 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2146 if (!jni_setAddress)
2148 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2152 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2155 OIC_LOG(ERROR, TAG, "setAddress is null");
2159 if (!strcmp(remoteAddress, setAddress))
2161 OIC_LOG(DEBUG, TAG, "the device is already set");
2162 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2166 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2169 OIC_LOG(DEBUG, TAG, "OUT - CALEClientIsDeviceInScanDeviceList");
2170 OIC_LOG(DEBUG, TAG, "there are no the device in list. we can add");
2175 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env)
2177 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllScanDevices");
2178 VERIFY_NON_NULL(env, TAG, "env is null");
2180 ca_mutex_lock(g_deviceListMutex);
2184 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2185 ca_mutex_unlock(g_deviceListMutex);
2186 return CA_STATUS_FAILED;
2189 uint32_t length = u_arraylist_length(g_deviceList);
2190 for (uint32_t index = 0; index < length; index++)
2192 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2195 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2198 (*env)->DeleteGlobalRef(env, jarrayObj);
2201 OICFree(g_deviceList);
2202 g_deviceList = NULL;
2204 ca_mutex_unlock(g_deviceListMutex);
2205 return CA_STATUS_OK;
2208 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring address)
2210 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceInScanDeviceList");
2211 VERIFY_NON_NULL(address, TAG, "address is null");
2212 VERIFY_NON_NULL(env, TAG, "env is null");
2214 ca_mutex_lock(g_deviceListMutex);
2218 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2219 ca_mutex_unlock(g_deviceListMutex);
2220 return CA_STATUS_FAILED;
2223 uint32_t length = u_arraylist_length(g_deviceList);
2224 for (uint32_t index = 0; index < length; index++)
2226 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2229 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2230 ca_mutex_unlock(g_deviceListMutex);
2231 return CA_STATUS_FAILED;
2234 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2235 if (!jni_setAddress)
2237 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2238 ca_mutex_unlock(g_deviceListMutex);
2239 return CA_STATUS_FAILED;
2242 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2245 OIC_LOG(ERROR, TAG, "setAddress is null");
2246 ca_mutex_unlock(g_deviceListMutex);
2247 return CA_STATUS_FAILED;
2250 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
2253 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2254 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2255 ca_mutex_unlock(g_deviceListMutex);
2256 return CA_STATUS_FAILED;
2259 if (!strcmp(setAddress, remoteAddress))
2261 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2262 (*env)->DeleteGlobalRef(env, jarrayObj);
2263 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2264 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2266 CALEClientReorderingList(index, g_deviceList);
2267 ca_mutex_unlock(g_deviceListMutex);
2268 return CA_STATUS_OK;
2270 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2271 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2274 ca_mutex_unlock(g_deviceListMutex);
2275 OIC_LOG(DEBUG, TAG, "There are no object in the device list");
2277 return CA_STATUS_OK;
2284 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt)
2286 OIC_LOG(DEBUG, TAG, "CALEClientAddGattobjToList");
2287 VERIFY_NON_NULL(env, TAG, "env is null");
2288 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2290 ca_mutex_lock(g_gattObjectMutex);
2292 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2293 if (!jni_remoteAddress)
2295 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2296 ca_mutex_unlock(g_gattObjectMutex);
2297 return CA_STATUS_FAILED;
2300 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2303 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2304 ca_mutex_unlock(g_gattObjectMutex);
2305 return CA_STATUS_FAILED;
2308 if (!CALEClientIsGattObjInList(env, remoteAddress))
2310 jobject newGatt = (*env)->NewGlobalRef(env, gatt);
2311 u_arraylist_add(g_gattObjectList, newGatt);
2312 OIC_LOG(DEBUG, TAG, "Set GATT Object to Array as Element");
2315 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2316 ca_mutex_unlock(g_gattObjectMutex);
2317 return CA_STATUS_OK;
2320 bool CALEClientIsGattObjInList(JNIEnv *env, const char* remoteAddress)
2322 OIC_LOG(DEBUG, TAG, "CALEClientIsGattObjInList");
2323 VERIFY_NON_NULL(env, TAG, "env is null");
2324 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2326 uint32_t length = u_arraylist_length(g_gattObjectList);
2327 for (uint32_t index = 0; index < length; index++)
2330 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2333 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2337 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2338 if (!jni_setAddress)
2340 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2344 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2347 OIC_LOG(ERROR, TAG, "setAddress is null");
2351 if (!strcmp(remoteAddress, setAddress))
2353 OIC_LOG(DEBUG, TAG, "the device is already set");
2354 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2359 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2364 OIC_LOG(DEBUG, TAG, "There are no GATT object in list. it can be added");
2368 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress)
2370 OIC_LOG(DEBUG, TAG, "CALEClientGetGattObjInList");
2371 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2372 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2374 ca_mutex_lock(g_gattObjectMutex);
2375 uint32_t length = u_arraylist_length(g_gattObjectList);
2376 for (uint32_t index = 0; index < length; index++)
2378 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2381 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2382 ca_mutex_unlock(g_gattObjectMutex);
2386 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2387 if (!jni_setAddress)
2389 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2390 ca_mutex_unlock(g_gattObjectMutex);
2394 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2397 OIC_LOG(ERROR, TAG, "setAddress is null");
2398 ca_mutex_unlock(g_gattObjectMutex);
2402 if (!strcmp(remoteAddress, setAddress))
2404 OIC_LOG(DEBUG, TAG, "the device is already set");
2405 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2406 ca_mutex_unlock(g_gattObjectMutex);
2409 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2412 ca_mutex_unlock(g_gattObjectMutex);
2413 OIC_LOG(DEBUG, TAG, "There are no the gatt object in list");
2417 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env)
2419 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllGattObjs");
2420 VERIFY_NON_NULL(env, TAG, "env is null");
2422 ca_mutex_lock(g_gattObjectMutex);
2423 if (!g_gattObjectList)
2425 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2426 ca_mutex_unlock(g_gattObjectMutex);
2427 return CA_STATUS_FAILED;
2430 uint32_t length = u_arraylist_length(g_gattObjectList);
2431 for (uint32_t index = 0; index < length; index++)
2433 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2436 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2439 (*env)->DeleteGlobalRef(env, jarrayObj);
2442 OICFree(g_gattObjectList);
2443 g_gattObjectList = NULL;
2444 ca_mutex_unlock(g_gattObjectMutex);
2445 return CA_STATUS_OK;
2448 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt)
2450 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObj");
2451 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2452 VERIFY_NON_NULL(env, TAG, "env is null");
2454 ca_mutex_lock(g_gattObjectMutex);
2455 if (!g_gattObjectList)
2457 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2458 ca_mutex_unlock(g_gattObjectMutex);
2459 return CA_STATUS_FAILED;
2462 uint32_t length = u_arraylist_length(g_gattObjectList);
2463 for (uint32_t index = 0; index < length; index++)
2465 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2468 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2469 ca_mutex_unlock(g_gattObjectMutex);
2470 return CA_STATUS_FAILED;
2473 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2474 if (!jni_setAddress)
2476 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2477 ca_mutex_unlock(g_gattObjectMutex);
2478 return CA_STATUS_FAILED;
2481 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2484 OIC_LOG(ERROR, TAG, "setAddress is null");
2485 ca_mutex_unlock(g_gattObjectMutex);
2486 return CA_STATUS_FAILED;
2489 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2490 if (!jni_remoteAddress)
2492 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2493 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2494 ca_mutex_unlock(g_gattObjectMutex);
2495 return CA_STATUS_FAILED;
2498 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2501 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2502 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2503 ca_mutex_unlock(g_gattObjectMutex);
2504 return CA_STATUS_FAILED;
2507 if (!strcmp(setAddress, remoteAddress))
2509 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2510 (*env)->DeleteGlobalRef(env, jarrayObj);
2511 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2512 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2513 ca_mutex_unlock(g_gattObjectMutex);
2514 return CALEClientReorderingList(index, g_gattObjectList);
2516 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2517 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2520 ca_mutex_unlock(g_gattObjectMutex);
2521 OIC_LOG(DEBUG, TAG, "there are no target object");
2522 return CA_STATUS_OK;
2525 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr)
2527 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObjForAddr");
2528 VERIFY_NON_NULL(addr, TAG, "addr is null");
2529 VERIFY_NON_NULL(env, TAG, "env is null");
2531 ca_mutex_lock(g_gattObjectMutex);
2532 if (!g_gattObjectList)
2534 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2535 ca_mutex_unlock(g_gattObjectMutex);
2536 return CA_STATUS_FAILED;
2539 uint32_t length = u_arraylist_length(g_gattObjectList);
2540 for (uint32_t index = 0; index < length; index++)
2542 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2545 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2546 ca_mutex_unlock(g_gattObjectMutex);
2547 return CA_STATUS_FAILED;
2550 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2551 if (!jni_setAddress)
2553 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2554 ca_mutex_unlock(g_gattObjectMutex);
2555 return CA_STATUS_FAILED;
2558 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2561 OIC_LOG(ERROR, TAG, "setAddress is null");
2562 ca_mutex_unlock(g_gattObjectMutex);
2563 return CA_STATUS_FAILED;
2566 const char* remoteAddress = (*env)->GetStringUTFChars(env, addr, NULL);
2569 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2570 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2571 ca_mutex_unlock(g_gattObjectMutex);
2572 return CA_STATUS_FAILED;
2575 if (!strcmp(setAddress, remoteAddress))
2577 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2578 (*env)->DeleteGlobalRef(env, jarrayObj);
2580 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2581 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2582 ca_mutex_unlock(g_gattObjectMutex);
2583 return CALEClientReorderingList(index, g_gattObjectList);
2585 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2586 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2589 ca_mutex_unlock(g_gattObjectMutex);
2590 OIC_LOG(DEBUG, TAG, "there are no target object");
2591 return CA_STATUS_FAILED;
2598 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
2599 uint16_t notificationState, uint16_t sendState)
2601 VERIFY_NON_NULL(address, TAG, "address is null");
2603 CALEState_t *newstate = (CALEState_t*) OICMalloc(sizeof(CALEState_t));
2606 OIC_LOG(ERROR, TAG, "out of memory");
2607 return CA_MEMORY_ALLOC_FAILED;
2610 if (strlen(address) > CA_MACADDR_SIZE)
2612 OIC_LOG(ERROR, TAG, "address is not proper");
2614 return CA_STATUS_FAILED;
2617 OICStrcpy(newstate->address, sizeof(newstate->address), address);
2618 newstate->connectedState = connectedState;
2619 newstate->notificationState = notificationState;
2620 newstate->sendState = sendState;
2621 return CALEClientAddDeviceStateToList(newstate);
2624 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state)
2626 VERIFY_NON_NULL(state, TAG, "state is null");
2628 ca_mutex_lock(g_deviceStateListMutex);
2630 if (!g_deviceStateList)
2632 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2633 ca_mutex_unlock(g_deviceStateListMutex);
2634 return CA_STATUS_FAILED;
2637 if (CALEClientIsDeviceInList(state->address))
2639 CALEState_t* curState = CALEClientGetStateInfo(state->address);
2642 OIC_LOG(ERROR, TAG, "curState is null");
2643 ca_mutex_unlock(g_deviceStateListMutex);
2644 return CA_STATUS_FAILED;
2647 if (STATE_CHARACTER_NO_CHANGE == state->notificationState)
2649 state->notificationState = curState->notificationState;
2652 // delete previous state for update new state
2653 CAResult_t res = CALEClientRemoveDeviceState(state->address);
2654 if (CA_STATUS_OK != res)
2656 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
2657 ca_mutex_unlock(g_deviceStateListMutex);
2661 u_arraylist_add(g_deviceStateList, state); // update new state
2662 OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d, %d",
2663 state->connectedState, state->notificationState);
2665 ca_mutex_unlock(g_deviceStateListMutex);
2666 return CA_STATUS_OK;
2669 bool CALEClientIsDeviceInList(const char* remoteAddress)
2671 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2673 if (!g_deviceStateList)
2675 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2679 uint32_t length = u_arraylist_length(g_deviceStateList);
2680 for (uint32_t index = 0; index < length; index++)
2682 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2685 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2689 if (!strcmp(remoteAddress, state->address))
2691 OIC_LOG(DEBUG, TAG, "the device is already set");
2700 OIC_LOG(DEBUG, TAG, "there are no the device in list.");
2704 CAResult_t CALEClientRemoveAllDeviceState()
2706 OIC_LOG(DEBUG, TAG, "CALENativeRemoveAllDevices");
2708 ca_mutex_lock(g_deviceStateListMutex);
2709 if (!g_deviceStateList)
2711 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2712 ca_mutex_unlock(g_deviceStateListMutex);
2713 return CA_STATUS_FAILED;
2716 uint32_t length = u_arraylist_length(g_deviceStateList);
2717 for (uint32_t index = 0; index < length; index++)
2719 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2722 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2728 OICFree(g_deviceStateList);
2729 g_deviceStateList = NULL;
2730 ca_mutex_unlock(g_deviceStateListMutex);
2732 return CA_STATUS_OK;
2735 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
2737 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
2738 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
2740 if (!g_deviceStateList)
2742 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2743 return CA_STATUS_FAILED;
2746 uint32_t length = u_arraylist_length(g_deviceStateList);
2747 for (uint32_t index = 0; index < length; index++)
2749 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2752 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2756 if (!strcmp(state->address, remoteAddress))
2758 OIC_LOG_V(DEBUG, TAG, "remove state : %s", remoteAddress);
2761 CAResult_t res = CALEClientReorderingList(index, g_deviceStateList);
2762 if(CA_STATUS_OK != res)
2764 OIC_LOG(ERROR, TAG, "CALEClientReorderingList has failed");
2767 return CA_STATUS_OK;
2771 return CA_STATUS_FAILED;
2774 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress)
2776 OIC_LOG(DEBUG, TAG, "CALEClientGetStateInfo");
2777 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2779 if (!g_deviceStateList)
2781 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2785 uint32_t length = u_arraylist_length(g_deviceStateList);
2786 for (uint32_t index = 0; index < length; index++)
2788 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2791 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2795 if (!strcmp(state->address, remoteAddress))
2797 OIC_LOG_V(DEBUG, TAG, "get state : %s", remoteAddress);
2804 bool CALEClientIsConnectedDevice(const char* remoteAddress)
2806 OIC_LOG(DEBUG, TAG, "CALEClientIsConnectedDevice");
2807 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2809 ca_mutex_lock(g_deviceStateListMutex);
2810 if (!g_deviceStateList)
2812 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2813 ca_mutex_unlock(g_deviceStateListMutex);
2817 uint32_t length = u_arraylist_length(g_deviceStateList);
2818 for (uint32_t index = 0; index < length; index++)
2820 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2823 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2827 if (!strcmp(state->address, remoteAddress))
2829 OIC_LOG(DEBUG, TAG, "check whether it is connected or not");
2831 if (STATE_CONNECTED == state->connectedState)
2833 ca_mutex_unlock(g_deviceStateListMutex);
2838 ca_mutex_unlock(g_deviceStateListMutex);
2843 ca_mutex_unlock(g_deviceStateListMutex);
2847 bool CALEClientIsSetCharacteristic(const char* remoteAddress)
2849 OIC_LOG(DEBUG, TAG, "CALEClientIsSetCharacteristic");
2850 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2852 ca_mutex_lock(g_deviceStateListMutex);
2853 if (!g_deviceStateList)
2855 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2856 ca_mutex_unlock(g_deviceStateListMutex);
2860 uint32_t length = u_arraylist_length(g_deviceStateList);
2861 for (uint32_t index = 0; index < length; index++)
2863 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2866 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2870 if (!strcmp(state->address, remoteAddress))
2872 OIC_LOG_V(DEBUG, TAG, "check whether it was set or not:%d", state->notificationState);
2874 if (STATE_CHARACTER_SET == state->notificationState)
2876 ca_mutex_unlock(g_deviceStateListMutex);
2881 ca_mutex_unlock(g_deviceStateListMutex);
2887 ca_mutex_unlock(g_deviceStateListMutex);
2891 void CALEClientCreateDeviceList()
2893 OIC_LOG(DEBUG, TAG, "CALEClientCreateDeviceList");
2895 // create new object array
2896 if (!g_gattObjectList)
2898 OIC_LOG(DEBUG, TAG, "Create g_gattObjectList");
2900 g_gattObjectList = u_arraylist_create();
2903 if (!g_deviceStateList)
2905 OIC_LOG(DEBUG, TAG, "Create g_deviceStateList");
2907 g_deviceStateList = u_arraylist_create();
2912 OIC_LOG(DEBUG, TAG, "Create g_deviceList");
2914 g_deviceList = u_arraylist_create();
2918 CAResult_t CALEClientReorderingList(uint32_t index, u_arraylist_t *list)
2922 OIC_LOG(ERROR, TAG, "list is null");
2923 return CA_STATUS_FAILED;
2926 if (index >= list->length)
2928 OIC_LOG(ERROR, TAG, "index is not available");
2929 return CA_STATUS_FAILED;
2932 if (index < list->length - 1)
2934 memmove(&list->data[index], &list->data[index + 1],
2935 (list->length - index - 1) * sizeof(void *));
2941 return CA_STATUS_OK;
2945 * Check Sent Count for remove g_sendBuffer
2947 void CALEClientUpdateSendCnt(JNIEnv *env)
2949 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2951 ca_mutex_lock(g_threadMutex);
2955 if (g_targetCnt <= g_currentSentCnt)
2958 g_currentSentCnt = 0;
2962 (*env)->DeleteGlobalRef(env, g_sendBuffer);
2963 g_sendBuffer = NULL;
2965 // notity the thread
2966 ca_cond_signal(g_threadCond);
2967 CALEClientSetSendFinishFlag(true);
2968 OIC_LOG(DEBUG, TAG, "set signal for send data");
2971 ca_mutex_unlock(g_threadMutex);
2974 CAResult_t CALEClientInitGattMutexVaraibles()
2976 OIC_LOG(DEBUG, TAG, "IN");
2978 if (NULL == g_bleReqRespClientCbMutex)
2980 g_bleReqRespClientCbMutex = ca_mutex_new();
2981 if (NULL == g_bleReqRespClientCbMutex)
2983 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2984 return CA_STATUS_FAILED;
2988 if (NULL == g_bleServerBDAddressMutex)
2990 g_bleServerBDAddressMutex = ca_mutex_new();
2991 if (NULL == g_bleServerBDAddressMutex)
2993 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2994 return CA_STATUS_FAILED;
2998 if (NULL == g_threadMutex)
3000 g_threadMutex = ca_mutex_new();
3001 if (NULL == g_threadMutex)
3003 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3004 return CA_STATUS_FAILED;
3008 if (NULL == g_threadSendMutex)
3010 g_threadSendMutex = ca_mutex_new();
3011 if (NULL == g_threadSendMutex)
3013 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3014 return CA_STATUS_FAILED;
3018 if (NULL == g_threadSendCBMutex)
3020 g_threadSendCBMutex = ca_mutex_new();
3021 if (NULL == g_threadSendCBMutex)
3023 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3024 return CA_STATUS_FAILED;
3028 if (NULL == g_deviceListMutex)
3030 g_deviceListMutex = ca_mutex_new();
3031 if (NULL == g_deviceListMutex)
3033 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3034 return CA_STATUS_FAILED;
3038 if (NULL == g_gattObjectMutex)
3040 g_gattObjectMutex = ca_mutex_new();
3041 if (NULL == g_gattObjectMutex)
3043 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3044 return CA_STATUS_FAILED;
3048 if (NULL == g_deviceStateListMutex)
3050 g_deviceStateListMutex = ca_mutex_new();
3051 if (NULL == g_deviceStateListMutex)
3053 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3054 return CA_STATUS_FAILED;
3058 if (NULL == g_SendFinishMutex)
3060 g_SendFinishMutex = ca_mutex_new();
3061 if (NULL == g_SendFinishMutex)
3063 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3064 return CA_STATUS_FAILED;
3068 if (NULL == g_writeCharacteristicCBMutex)
3070 g_writeCharacteristicCBMutex = ca_mutex_new();
3071 if (NULL == g_writeCharacteristicCBMutex)
3073 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3074 return CA_STATUS_FAILED;
3078 if (NULL == g_theSendRequestMutex)
3080 g_theSendRequestMutex = ca_mutex_new();
3081 if (NULL == g_theSendRequestMutex)
3083 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3084 return CA_STATUS_FAILED;
3088 OIC_LOG(DEBUG, TAG, "OUT");
3089 return CA_STATUS_OK;
3092 void CALEClientTerminateGattMutexVariables()
3094 OIC_LOG(DEBUG, TAG, "IN");
3096 ca_mutex_free(g_bleReqRespClientCbMutex);
3097 g_bleReqRespClientCbMutex = NULL;
3099 ca_mutex_free(g_bleServerBDAddressMutex);
3100 g_bleServerBDAddressMutex = NULL;
3102 ca_mutex_free(g_threadMutex);
3103 g_threadMutex = NULL;
3105 ca_mutex_free(g_threadSendMutex);
3106 g_threadSendMutex = NULL;
3108 ca_mutex_free(g_threadSendCBMutex);
3109 g_threadSendCBMutex = NULL;
3111 ca_mutex_free(g_deviceListMutex);
3112 g_deviceListMutex = NULL;
3114 ca_mutex_free(g_SendFinishMutex);
3115 g_SendFinishMutex = NULL;
3117 ca_mutex_free(g_writeCharacteristicCBMutex);
3118 g_writeCharacteristicCBMutex = NULL;
3120 ca_mutex_free(g_theSendRequestMutex);
3121 g_theSendRequestMutex = NULL;
3123 OIC_LOG(DEBUG, TAG, "OUT");
3126 void CALEClientSetSendFinishFlag(bool flag)
3128 OIC_LOG_V(DEBUG, TAG, "g_isFinishedSendData is %d", flag);
3130 ca_mutex_lock(g_SendFinishMutex);
3131 g_isFinishedSendData = flag;
3132 ca_mutex_unlock(g_SendFinishMutex);
3135 void CALEClientSetWriteCharacteristicCBFlag(bool flag)
3137 OIC_LOG_V(DEBUG, TAG, "g_isReceivedWriteCB is %d", flag);
3139 ca_mutex_lock(g_writeCharacteristicCBMutex);
3140 g_isReceivedWriteCB = flag;
3141 ca_mutex_unlock(g_writeCharacteristicCBMutex);
3144 void CALEClientSetTheSendRequestFlag(bool flag)
3146 OIC_LOG_V(DEBUG, TAG, "g_isRequestedSend is %d", flag);
3148 ca_mutex_lock(g_theSendRequestMutex);
3149 g_isRequestedSend = flag;
3150 ca_mutex_unlock(g_theSendRequestMutex);
3157 CAResult_t CAStartBLEGattClient()
3159 CAResult_t res = CALEClientStartMulticastServer();
3160 if (CA_STATUS_OK != res)
3162 OIC_LOG(ERROR, TAG, "CALEClientStartMulticastServer has failed");
3166 g_isStartedLEClient = true;
3172 void CAStopBLEGattClient()
3174 OIC_LOG(DEBUG, TAG, "CAStopBLEGattClient");
3178 OIC_LOG(ERROR, TAG, "g_jvm is null");
3182 bool isAttached = false;
3184 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
3187 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
3188 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
3192 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
3198 CAResult_t ret = CALEClientDisconnectAll(env);
3199 if (CA_STATUS_OK != ret)
3201 OIC_LOG(ERROR, TAG, "CALEClientDisconnectAll has failed");
3204 ret = CALEClientStopScan();
3205 if(CA_STATUS_OK != ret)
3207 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
3210 ca_cond_signal(g_threadCond);
3211 ca_cond_signal(g_threadSendCond);
3212 g_isStartedLEClient = false;
3216 (*g_jvm)->DetachCurrentThread(g_jvm);
3221 void CATerminateBLEGattClient()
3223 OIC_LOG(DEBUG, TAG, "Terminate GATT Client");
3224 CALEClientTerminate();
3227 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const char *data,
3228 const uint32_t dataLen, CALETransferType_t type,
3229 const int32_t position)
3231 OIC_LOG(DEBUG, TAG, "call CALEClientSendUnicastMessage");
3232 VERIFY_NON_NULL(data, TAG, "data is null");
3233 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
3235 return CALEClientSendUnicastMessage(remoteAddress, data, dataLen);
3238 CAResult_t CAUpdateCharacteristicsToAllGattServers(const char *data, uint32_t dataLen)
3240 OIC_LOG(DEBUG, TAG, "call CALEClientSendMulticastMessage");
3241 VERIFY_NON_NULL(data, TAG, "data is null");
3243 return CALEClientSendMulticastMessage(data, dataLen);
3246 void CASetBLEReqRespClientCallback(CABLEClientDataReceivedCallback callback)
3248 OIC_LOG(DEBUG, TAG, "IN");
3250 ca_mutex_lock(g_bleReqRespClientCbMutex);
3251 g_CABLEClientDataReceivedCallback = callback;
3252 ca_mutex_unlock(g_bleReqRespClientCbMutex);
3254 OIC_LOG(DEBUG, TAG, "OUT");
3257 void CASetBleClientThreadPoolHandle(ca_thread_pool_t handle)
3259 OIC_LOG(DEBUG, TAG, "IN");
3261 CALEClientInitialize(handle);
3263 OIC_LOG(DEBUG, TAG, "OUT");
3266 CAResult_t CAGetLEAddress(char **local_address)
3268 OIC_LOG(DEBUG, TAG, "IN");
3270 VERIFY_NON_NULL(local_address, TAG, "local_address is null");
3271 CAResult_t res = CALEClientGetInterfaceInfo(local_address);
3272 if (CA_STATUS_OK != res)
3274 OIC_LOG(INFO, TAG, "it didn't get local address");
3277 OIC_LOG(DEBUG, TAG, "OUT");
3278 return CA_STATUS_OK;
3281 JNIEXPORT void JNICALL
3282 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, jobject obj,
3285 OIC_LOG(DEBUG, TAG, "CaLeRegisterLeScanCallback");
3286 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3287 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3289 g_leScanCallback = (*env)->NewGlobalRef(env, callback);
3292 JNIEXPORT void JNICALL
3293 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, jobject obj,
3296 OIC_LOG(DEBUG, TAG, "CaLeRegisterGattCallback");
3297 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3298 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3300 g_leGattCallback = (*env)->NewGlobalRef(env, callback);
3303 JNIEXPORT void JNICALL
3304 Java_org_iotivity_ca_CaLeClientInterface_caLeScanCallback(JNIEnv *env, jobject obj,
3305 jobject device, jint rssi,
3306 jbyteArray scanRecord)
3308 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3309 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
3311 CAResult_t res = CALEClientAddScanDeviceToList(env, device);
3312 if (CA_STATUS_OK != res)
3314 OIC_LOG_V(ERROR, TAG, "CALEClientAddScanDeviceToList has failed : %d", res);
3319 * Class: org_iotivity_ca_jar_caleinterface
3320 * Method: CALeGattConnectionStateChangeCallback
3321 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3323 JNIEXPORT void JNICALL
3324 Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env,
3330 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status,
3332 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3333 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3335 if (GATT_SUCCESS == status && STATE_CONNECTED == newstate) // le connected
3337 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3343 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3346 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
3347 STATE_CHARACTER_NO_CHANGE,
3349 if (CA_STATUS_OK != res)
3351 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3352 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3355 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3358 CAResult_t res = CALEClientAddGattobjToList(env, gatt);
3359 if (CA_STATUS_OK != res)
3361 OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed");
3365 res = CALEClientDiscoverServices(env, gatt);
3366 if (CA_STATUS_OK != res)
3368 OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed");
3372 else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate) // le disconnected
3374 CAResult_t res = CALEClientStartScan();
3375 if (CA_STATUS_OK != res)
3377 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
3380 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3383 OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
3386 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3389 res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3390 STATE_CHARACTER_NO_CHANGE,
3392 if (CA_STATUS_OK != res)
3394 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3396 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3399 res = CALEClientGattClose(env, gatt);
3400 if (CA_STATUS_OK != res)
3402 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3405 ca_cond_signal(g_threadSendCond);
3410 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3413 OIC_LOG(ERROR, TAG, "jni_address is null");
3418 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3421 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3422 STATE_CHARACTER_NO_CHANGE,
3424 if (CA_STATUS_OK != res)
3426 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3429 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3438 CALEClientSendFinish(env, gatt);
3439 ca_cond_signal(g_threadSendCond);
3444 * Class: org_iotivity_ca_jar_caleinterface
3445 * Method: CALeGattServicesDiscoveredCallback
3446 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3448 JNIEXPORT void JNICALL
3449 Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env,
3454 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
3455 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3456 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3458 if (0 != status) // discovery error
3460 CALEClientSendFinish(env, gatt);
3461 ca_cond_signal(g_threadSendCond);
3465 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3468 CALEClientSendFinish(env, gatt);
3469 ca_cond_signal(g_threadSendCond);
3473 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3476 CALEClientSendFinish(env, gatt);
3477 ca_cond_signal(g_threadSendCond);
3481 if (!CALEClientIsSetCharacteristic(address))
3483 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
3486 OIC_LOG(ERROR, TAG, "jni_uuid is null");
3490 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid);
3491 if (!jni_obj_GattCharacteristic)
3493 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
3497 CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt,
3498 jni_obj_GattCharacteristic);
3499 if (CA_STATUS_OK != res)
3501 OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed");
3505 res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic);
3506 if (CA_STATUS_OK != res)
3508 OIC_LOG(ERROR, TAG, "CALEClientSetUUIDToDescriptor has failed");
3512 res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3514 if (CA_STATUS_OK != res)
3516 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3522 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3523 if (CA_STATUS_OK != res)
3525 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3529 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3534 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3535 CALEClientSendFinish(env, gatt);
3536 ca_cond_signal(g_threadSendCond);
3541 * Class: org_iotivity_ca_jar_caleinterface
3542 * Method: CALeGattCharacteristicReadCallback
3543 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3545 JNIEXPORT void JNICALL
3546 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicReadCallback(JNIEnv *env,
3549 jobject characteristic,
3553 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicReadCallback - status : %d", status);
3557 * Class: org_iotivity_ca_jar_caleinterface
3558 * Method: CALeGattCharacteristicWritjclasseCallback
3559 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3561 JNIEXPORT void JNICALL
3562 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback(
3563 JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data,
3566 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
3567 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3568 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3570 ca_mutex_lock(g_threadSendCBMutex);
3571 if (!g_isRequestedSend)
3573 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - waiting");
3574 ca_cond_wait(g_threadSendCBCond, g_threadSendCBMutex);
3576 ca_mutex_unlock(g_threadSendCBMutex);
3579 char* wroteData = (char*) (*env)->GetByteArrayElements(env, data, &isCopy);
3581 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
3583 // send success & signal
3584 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3590 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3596 if (GATT_SUCCESS != status) // error case
3598 OIC_LOG(ERROR, TAG, "send failure");
3599 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3601 if (CA_STATUS_OK != res)
3603 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3605 CALEClientSendFinish(env, gatt);
3609 OIC_LOG(DEBUG, TAG, "send success");
3610 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3611 STATE_SEND_SUCCESS);
3612 if (CA_STATUS_OK != res)
3614 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3616 CALEClientUpdateSendCnt(env);
3619 CALEClientSetWriteCharacteristicCBFlag(true);
3620 CALEClientSetTheSendRequestFlag(false);
3621 ca_cond_signal(g_threadSendCond);
3622 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3628 CALEClientSetWriteCharacteristicCBFlag(true);
3629 CALEClientSetTheSendRequestFlag(false);
3630 CALEClientSendFinish(env, gatt);
3631 ca_cond_signal(g_threadSendCond);
3636 * Class: org_iotivity_ca_jar_caleinterface
3637 * Method: CALeGattCharacteristicChangedCallback
3638 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
3640 JNIEXPORT void JNICALL
3641 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback(
3642 JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data)
3644 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
3645 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3646 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3647 VERIFY_NON_NULL_VOID(data, TAG, "data is null");
3649 // get Byte Array and covert to char*
3650 jint length = (*env)->GetArrayLength(env, data);
3653 jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy);
3655 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - raw data received : %s",
3656 jni_byte_responseData);
3658 char* receivedData = (char*) OICMalloc(sizeof(char) * length + 1);
3661 OIC_LOG(ERROR, TAG, "recevicedData is null");
3665 memcpy(receivedData, (const char*) jni_byte_responseData, length);
3666 receivedData[length] = '\0';
3667 (*env)->ReleaseByteArrayElements(env, data, jni_byte_responseData, JNI_ABORT);
3669 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3672 OIC_LOG(ERROR, TAG, "jni_address is null");
3673 OICFree(receivedData);
3677 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3680 OIC_LOG(ERROR, TAG, "address is null");
3681 OICFree(receivedData);
3685 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %s, %d",
3686 receivedData, length);
3688 ca_mutex_lock(g_bleServerBDAddressMutex);
3689 uint32_t sentLength = 0;
3690 g_CABLEClientDataReceivedCallback(address, OIC_GATT_SERVICE_UUID, receivedData, length,
3692 ca_mutex_unlock(g_bleServerBDAddressMutex);
3694 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3698 * Class: org_iotivity_ca_jar_caleinterface
3699 * Method: CALeGattDescriptorReadCallback
3700 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3702 JNIEXPORT void JNICALL
3703 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorReadCallback(JNIEnv *env, jobject obj,
3708 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorReadCallback - status %d: ", status);
3712 * Class: org_iotivity_ca_jar_caleinterface
3713 * Method: CALeGattDescriptorWriteCallback
3714 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3716 JNIEXPORT void JNICALL
3717 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv *env, jobject obj,
3722 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
3723 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3724 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3726 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3727 if (CA_STATUS_OK != res)
3729 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3737 CALEClientSendFinish(env, gatt);
3738 ca_cond_signal(g_threadSendCond);
3743 * Class: org_iotivity_ca_jar_caleinterface
3744 * Method: CALeGattReliableWriteCompletedCallback
3745 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3747 JNIEXPORT void JNICALL
3748 Java_org_iotivity_ca_CaLeClientInterface_caLeGattReliableWriteCompletedCallback(JNIEnv *env,
3753 OIC_LOG_V(DEBUG, TAG, "CALeGattReliableWriteCompletedCallback - status %d: ", status);
3757 * Class: org_iotivity_ca_jar_caleinterface
3758 * Method: CALeGattReadRemoteRssiCallback
3759 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3761 JNIEXPORT void JNICALL
3762 Java_org_iotivity_ca_CaLeClientInterface_caLeGattReadRemoteRssiCallback(JNIEnv *env, jobject obj,
3763 jobject gatt, jint rssi,
3766 OIC_LOG_V(DEBUG, TAG, "CALeGattReadRemoteRssiCallback - rssi %d, status %d: ", rssi, status);