6 #include "caleserver.h"
10 #include "oic_malloc.h"
11 #include "uthreadpool.h" /* for thread pool */
13 #include "uarraylist.h"
14 #include "com_iotivity_jar_CALeInterface.h"
17 #define TAG PCF("CA_LE_CLIENT")
19 static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
20 static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
21 static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
22 static const char *CLASSPATH_BT_UUID = "java/util/UUID";
23 static const char *CLASSPATH_BT_GATT = "android/bluetooth/BluetoothGatt";
25 static const char *IOTIVITY_GATT_SERVIE_UUID = "713d0000-503e-4c75-ba94-3148f18d941e";
26 static const char *IOTIVITY_GATT_TX_UUID = "713d0003-503e-4c75-ba94-3148f18d941e";
27 static const char *IOTIVITY_GATT_RX_UUID = "713d0002-503e-4c75-ba94-3148f18d941e";
29 static const uint32_t STATE_CONNECTED = 2;
30 static const uint32_t STATE_DISCONNECTED = 0;
31 static const uint32_t GATT_SUCCESS = 0;
33 static const uint32_t MAX_PDU_BUFFER = 1400;
36 static u_arraylist_t *gdeviceList = NULL;
37 static u_arraylist_t *gGattObjectList = NULL;
38 static CAPacketReceiveCallback gPacketReceiveCallback = NULL;
39 static u_thread_pool_t gThreadPoolHandle = NULL;
40 static jobject gLeScanCallback;
41 static jobject gLeGattCallback;
42 static jobject gContext;
43 static jobjectArray gUUIDList;
44 static jboolean gIsStartServer;
45 static jboolean gIsFinishSendData;
47 static jbyteArray gSendBuffer;
48 static uint32_t gTargetCnt = 0;
49 static uint32_t gCurrentSentCnt = 0;
51 /** mutex for synchrnoization **/
52 static u_mutex gThreadMutex;
53 /** conditional mutex for synchrnoization **/
54 static u_cond gThreadCond;
56 ////////////////////////////////////////////////////////////////////////////////////////////////////
57 //FIXME getting context
58 void CALEClientJNISetContext(JNIEnv *env, jobject context)
60 OIC_LOG_V(DEBUG, TAG, "CALEClientJNISetContext");
64 OIC_LOG_V(DEBUG, TAG, "context is null");
68 gContext = (*env)->NewGlobalRef(env, context);
71 void CALeCreateJniInterfaceObject()
73 OIC_LOG_V(DEBUG, TAG, "CALeCreateJniInterfaceObject");
75 jboolean isAttached = FALSE;
77 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
80 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
81 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
85 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
91 jclass LeJniInterface = (*env)->FindClass(env, "com/iotivity/jar/CALeInterface");
94 OIC_LOG_V(DEBUG, TAG, "Could not get CALeInterface class");
98 jmethodID LeInterfaceConstructorMethod =
99 (*env)->GetMethodID(env, LeJniInterface, "<init>", "(Landroid/content/Context;)V");
100 if (!LeInterfaceConstructorMethod)
102 OIC_LOG_V(DEBUG, TAG, "Could not get CALeInterface constructor method");
106 (*env)->NewObject(env, LeJniInterface, LeInterfaceConstructorMethod, gContext);
107 OIC_LOG_V(DEBUG, TAG, "Create CALeInterface instance");
110 (*g_jvm)->DetachCurrentThread(g_jvm);
113 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
115 OIC_LOG_V(DEBUG, TAG, "JNI_OnLoad in calecore");
118 if((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
122 g_jvm = jvm; /* cache the JavaVM pointer */
124 //JVM required for WifiCore to work with JNI interface
126 CALeServerJniInit(env, jvm);
127 CAEDRCoreJniInit(env, jvm);
129 return JNI_VERSION_1_6;
132 void JNI_OnUnload(JavaVM *jvm, void *reserved)
134 OIC_LOG_V(DEBUG, TAG, "JNI_OnUnload in calecore");
137 if((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
145 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CARegisterLeScanCallback(JNIEnv *env,
146 jobject obj, jobject callback)
148 OIC_LOG_V(DEBUG, TAG, "CARegisterLeScanCallback");
150 gLeScanCallback = (*env)->NewGlobalRef(env, callback);
153 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CARegisterLeGattCallback(JNIEnv *env,
154 jobject obj, jobject callback)
156 OIC_LOG_V(DEBUG, TAG, "CARegisterLeGattCallback");
158 gLeGattCallback = (*env)->NewGlobalRef(env, callback);
161 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeScanCallback (JNIEnv *env,
162 jobject obj, jobject device, jint rssi, jbyteArray scanRecord)
164 CANativeAddScanDeviceToList(env, device);
168 * Class: com_iotivity_jar_CALeInterface
169 * Method: CALeGattConnectionStateChangeCallback
170 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
172 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattConnectionStateChangeCallback
173 (JNIEnv *env, jobject obj, jobject gatt, jint status, jint newstate)
175 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status, newstate);
177 if(GATT_SUCCESS == status && STATE_CONNECTED == newstate) // le connected
180 CANativeAddGattobjToList(env, gatt);
181 CANativeLEDiscoverServices(env, gatt);
184 else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate) // le disconnected
186 // CANativeLEStartScan();
189 //CANativeGattClose(env, gatt); // 20150105
190 //CANativeRemoveGattObj(env, gatt); // 20150105
195 CANativeSendFinsih(env, gatt);
200 * Class: com_iotivity_jar_CALeInterface
201 * Method: CALeGattServicesDiscoveredCallback
202 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
204 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattServicesDiscoveredCallback
205 (JNIEnv *env, jobject obj, jobject gatt, jint status)
207 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
209 if(0 != status) // discovery error
211 CANativeSendFinsih(env, gatt);
215 // read Characteristic
216 // CANativeReadCharacteristic(env, gatt);
218 jboolean ret_rx = CANativeSetCharacteristicNoti(env, gatt, IOTIVITY_GATT_RX_UUID);
219 if(!ret_rx) // SetCharacteristicNoti is failed
221 CANativeSendFinsih(env, gatt);
225 // jstring data = (*env)->NewStringUTF(env, "HelloWorld");
226 jobject jni_obj_character = CANativeCreateGattCharacteristic(env, gatt, gSendBuffer);
227 if(!jni_obj_character)
229 CANativeSendFinsih(env, gatt);
234 jboolean ret = CANativeLESendData(env, gatt, jni_obj_character);
237 CANativeSendFinsih(env, gatt);
243 * Class: com_iotivity_jar_CALeInterface
244 * Method: CALeGattCharacteristicReadCallback
245 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
247 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicReadCallback
248 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data, jint status)
250 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicReadCallback - status : %d", status);
254 * Class: com_iotivity_jar_CALeInterface
255 * Method: CALeGattCharacteristicWritjclasseCallback
256 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
258 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicWriteCallback
259 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data, jint status)
261 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
264 char* wroteData = (char*)(*env)->GetByteArrayElements(env, data, &isCopy);
266 jstring jni_address = CANativeGetAddressFromGattObj(env, gatt);
267 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
269 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
272 CANativeSendFinsih(env, gatt);
275 if(0 != status) // error case
277 CANativeSendFinsih(env, gatt);
282 * Class: com_iotivity_jar_CALeInterface
283 * Method: CALeGattCharacteristicChangedCallback
284 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
286 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicChangedCallback
287 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data)
289 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
291 // get Byte Array and covert to char*
292 jint length = (*env)->GetArrayLength(env, data);
295 jbyte *jni_byte_responseData = (char*)(*env)->GetByteArrayElements(env, data, &isCopy);
297 char responseData[MAX_PDU_BUFFER];
298 memset(responseData, 0, MAX_PDU_BUFFER);
299 strncpy(responseData, (char*)jni_byte_responseData, length);
300 responseData[length] = '\0';
301 (*env)->ReleaseByteArrayElements(env, data, (jbyte*)jni_byte_responseData, JNI_ABORT);
303 jstring jni_address = CANativeGetAddressFromGattObj(env, gatt);
304 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
306 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %s, %d", responseData, length);
309 gPacketReceiveCallback(address, responseData);
311 // LE disconnect and finish BLE write routine
312 CANativeSendFinsih(env, gatt);
316 * Class: com_iotivity_jar_CALeInterface
317 * Method: CALeGattDescriptorReadCallback
318 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
320 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattDescriptorReadCallback
321 (JNIEnv *env, jobject obj, jobject gatt, jobject descriptor, jint status)
323 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorReadCallback - status %d: ", status);
327 * Class: com_iotivity_jar_CALeInterface
328 * Method: CALeGattDescriptorWriteCallback
329 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
331 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattDescriptorWriteCallback
332 (JNIEnv *env, jobject obj, jobject gatt, jobject descriptor, jint status)
334 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
338 * Class: com_iotivity_jar_CALeInterface
339 * Method: CALeGattReliableWriteCompletedCallback
340 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
342 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattReliableWriteCompletedCallback
343 (JNIEnv *env, jobject obj, jobject gatt, jint status)
345 OIC_LOG_V(DEBUG, TAG, "CALeGattReliableWriteCompletedCallback - status %d: ", status);
349 * Class: com_iotivity_jar_CALeInterface
350 * Method: CALeGattReadRemoteRssiCallback
351 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
353 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattReadRemoteRssiCallback
354 (JNIEnv *env, jobject obj, jobject gatt, jint rssi, jint status)
356 OIC_LOG_V(DEBUG, TAG, "CALeGattReadRemoteRssiCallback - rssi %d, status %d: ", rssi, status);
360 void CALEInitialize(u_thread_pool_t handle)
362 OIC_LOG(DEBUG, TAG, "CALEInitialize");
364 gThreadPoolHandle = handle;
366 // init mutex for send logic
367 gThreadMutex = u_mutex_new();
368 gThreadCond = u_cond_new();
370 CANativeCreateUUIDList();
372 CALeCreateJniInterfaceObject(); /* create java CALeInterface instance*/
377 OIC_LOG(DEBUG, TAG, "CALETerminate");
379 jboolean isAttached = FALSE;
381 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
384 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
385 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
389 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
395 CANativeLEDisconnectAll(env);
399 CANativeLEStopScanImpl(env, gLeScanCallback);
405 (*env)->DeleteGlobalRef(env, gLeScanCallback);
410 (*env)->DeleteGlobalRef(env, gLeGattCallback);
415 (*env)->DeleteGlobalRef(env, gContext);
420 (*env)->DeleteGlobalRef(env, gSendBuffer);
425 (*env)->DeleteGlobalRef(env, gUUIDList);
428 CANativeRemoveAllDevices(env);
429 CANativeRemoveAllGattObjsList(env);
430 gIsStartServer = FALSE;
431 gIsFinishSendData = FALSE;
434 (*g_jvm)->DetachCurrentThread(g_jvm);
436 // delete mutex object
437 u_mutex_free(gThreadMutex);
439 u_cond_signal(gThreadCond);
440 u_cond_free(gThreadCond);
443 void CANativeSendFinsih(JNIEnv *env, jobject gatt)
445 OIC_LOG_V(DEBUG, TAG, "CANativeSendFinsih");
449 CANativeLEDisconnect(env, gatt);
451 CANativeupdateSendCnt(env);
454 int32_t CALESendUnicastMessage(const char* address, const char* data, uint32_t dataLen)
456 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastMessage(%s, %s)", address, data);
458 CALESendUnicastMessageImpl(address, data, dataLen);
462 int32_t CALESendMulticastMessage(const char* data, uint32_t dataLen)
464 OIC_LOG_V(DEBUG, TAG, "CALESendMulticastMessage(%s)", data);
466 jboolean isAttached = FALSE;
468 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
471 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
472 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
476 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
482 CALESendMulticastMessageImpl(env, data, dataLen);
485 (*g_jvm)->DetachCurrentThread(g_jvm);
490 int32_t CALEStartUnicastServer(const char* address)
492 OIC_LOG_V(DEBUG, TAG, "CALEStartUnicastServer(%s)", address);
497 int32_t CALEStartMulticastServer()
499 OIC_LOG_V(DEBUG, TAG, "CALEStartMulticastServer");
503 OIC_LOG_V(DEBUG, TAG, "server is already started..it will be skipped");
507 jboolean isAttached = FALSE;
509 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
512 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
513 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
517 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
523 gIsStartServer = TRUE;
525 CANativeLEStartScan();
528 (*g_jvm)->DetachCurrentThread(g_jvm);
533 int32_t CALEStopUnicastServer(int32_t serverID)
535 OIC_LOG(DEBUG, TAG, "CALEStopUnicastServer");
540 int32_t CALEStopMulticastServer(int32_t serverID)
542 OIC_LOG(DEBUG, TAG, "CALEStopMulticastServer");
543 gIsStartServer = FALSE;
544 CANativeLEStopScan();
548 void CALESetCallback(CAPacketReceiveCallback callback)
550 gPacketReceiveCallback = callback;
553 CAResult_t CALEGetInterfaceInfo(char **address)
555 CALEGetLocalAddress(address);
559 void CALEGetLocalAddress(char** address)
561 jboolean isAttached = FALSE;
563 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
566 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
567 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
570 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
576 jstring jni_address = CALEGetLocalDeviceAddress(env);
579 const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
580 *address = (char*)OICMalloc(strlen(localAddress) + 1);
581 if (*address == NULL)
585 memcpy(*address, localAddress, strlen(localAddress));
588 OIC_LOG_V(DEBUG, TAG, "Local Address : %s", *address);
590 (*g_jvm)->DetachCurrentThread(g_jvm);
593 int32_t CALESendUnicastMessageImpl(const char* address, const char* data, uint32_t dataLen)
595 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastMessageImpl, address: %s, data: %s", address, data);
597 jboolean isAttached = FALSE;
599 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
602 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
603 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
606 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
612 OIC_LOG(DEBUG, TAG, "[BLE][Native] set byteArray for data");
615 (*env)->DeleteGlobalRef(env, gSendBuffer);
617 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
618 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*)data);
619 gSendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
621 // connect to gatt server
622 CANativeLEStopScanImpl(env, gLeScanCallback);
625 jobject jni_obj_bluetoothDevice = NULL;
626 if(gContext && gdeviceList)
629 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
631 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
634 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
638 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
641 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
644 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
646 if(!strcmp(setAddress, address))
648 jni_obj_bluetoothDevice = jarrayObj;
653 if(jni_obj_bluetoothDevice)
655 jboolean autoConnect = FALSE;
656 CANativeLEConnect(env, jni_obj_bluetoothDevice, gContext, autoConnect, gLeGattCallback);
661 (*g_jvm)->DetachCurrentThread(g_jvm);
666 int32_t CALESendMulticastMessageImpl(JNIEnv *env, const char* data, uint32_t dataLen)
668 OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s, %d", data, dataLen);
672 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
675 OIC_LOG(DEBUG, TAG, "set wait");
678 gIsFinishSendData = FALSE;
680 OIC_LOG(DEBUG, TAG, "[BLE][Native] set byteArray for data");
683 (*env)->DeleteGlobalRef(env, gSendBuffer);
685 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
686 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*)data);
687 gSendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
689 // connect to gatt server
690 CANativeLEStopScanImpl(env, gLeScanCallback);
694 CANativeRemoveAllGattObjsList(env);
695 CANativeCreateGattObjList(env);
697 gTargetCnt = u_arraylist_length(gdeviceList);
701 while (index < u_arraylist_length(gdeviceList)) {
702 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
705 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
709 if(0 <= CANativeLEConnect(env, jarrayObj, gContext, FALSE, gLeGattCallback))
711 // connection failure
717 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
718 if(!gIsFinishSendData)
720 u_mutex_lock(gThreadMutex);
721 u_cond_wait(gThreadCond, gThreadMutex);
722 OIC_LOG(DEBUG, TAG, "unset wait");
723 u_mutex_unlock(gThreadMutex);
726 // start LE Scan again
728 CANativeLEStartScan();
734 jstring CANativeGetAddressFromGattObj(JNIEnv *env, jobject gatt)
738 OIC_LOG(DEBUG, TAG, "[BLE][Native] gatt is null");
742 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
743 if(!jni_cid_gattdevice_list)
745 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_gattdevice_list is null");
749 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
750 "()Landroid/bluetooth/BluetoothDevice;");
751 if(!jni_mid_getDevice)
753 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getDevice is null");
757 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
760 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_device is null");
764 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
767 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_address is null");
777 void CANativeGattClose(JNIEnv *env, jobject bluetoothGatt)
780 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT CLOSE");
782 // get BluetoothGatt class
783 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
784 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
785 if(!jni_cid_BluetoothGatt)
787 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
791 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close","()V");
792 if(!jni_mid_closeGatt)
794 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_closeGatt is null");
798 // call disconnect gatt method
799 OIC_LOG(DEBUG, TAG, "[BLE][Native] request close Gatt");
800 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
803 void CANativeLEStartScan()
807 OIC_LOG_V(DEBUG, TAG, "server is not started yet..scan will be passed");
811 jboolean isAttached = FALSE;
813 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
816 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
818 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
821 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
827 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeLEStartScan");
829 // scan gatt server with UUID
830 if(gLeScanCallback && gUUIDList)
832 CANativeLEStartScanWithUUIDImpl(env, gUUIDList, gLeScanCallback);
836 (*g_jvm)->DetachCurrentThread(g_jvm);
840 void CANativeLEStartScanImpl(JNIEnv *env, jobject callback)
842 if(!CALEIsEnableBTAdapter(env))
844 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
848 // get default bt adapter class
849 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
850 if(!jni_cid_BTAdapter)
852 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
856 // get remote bt adapter method
857 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
858 if(!jni_mid_getDefaultAdapter)
860 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
864 // get start le scan method
865 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
866 "(Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)Z");
867 if(!jni_mid_startLeScan)
869 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan: jni_mid_startLeScan is null");
873 // gat bt adapter object
874 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
875 if(!jni_obj_BTAdapter)
877 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
881 // call start le scan method
882 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_startLeScan, callback);
883 if(!jni_obj_startLeScan)
885 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan is failed");
890 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan is started");
894 jobject CANativeGetUUIDObject(JNIEnv *env, const char* uuid)
897 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
900 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_uuid is null");
904 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;");
905 if(!jni_mid_fromString)
907 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_fromString is null");
911 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
912 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString, jni_uuid);
915 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_uuid is null");
922 void CANativeLEStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
924 // get default bt adapter class
925 OIC_LOG(DEBUG, TAG, "[BLE][Native] get default bt adapter class");
927 if(!CALEIsEnableBTAdapter(env))
929 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
933 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
934 if(!jni_cid_BTAdapter)
936 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
940 // get remote bt adapter method
941 OIC_LOG(DEBUG, TAG, "[BLE][Native] get remote bt adapter method");
942 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
943 if(!jni_mid_getDefaultAdapter)
945 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
949 // get start le scan method
950 OIC_LOG(DEBUG, TAG, "[BLE][Native] get start le scan method");
951 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
952 "([Ljava/util/UUID;Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)Z");
953 if(!jni_mid_startLeScan)
955 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan: jni_mid_startLeScan is null");
959 // get bt adapter object
960 OIC_LOG(DEBUG, TAG, "[BLE][Native] get bt adapter object");
961 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
962 if(!jni_obj_BTAdapter)
964 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
968 // call start le scan method
969 OIC_LOG(DEBUG, TAG, "[BLE][Native] call start le scan service method");
970 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_startLeScan, uuids, callback);
971 if(!jni_obj_startLeScan)
973 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan With UUID is failed");
978 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan With UUID is started");
982 void CANativeLEStopScan()
984 jboolean isAttached = FALSE;
986 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
989 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
990 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
993 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
999 CANativeLEStopScanImpl(env, gLeScanCallback);
1002 (*g_jvm)->DetachCurrentThread(g_jvm);
1006 void CANativeLEStopScanImpl(JNIEnv *env, jobject callback)
1008 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeLEStopScan");
1010 if(!CALEIsEnableBTAdapter(env))
1012 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1016 // get default bt adapter class
1017 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
1018 if(!jni_cid_BTAdapter)
1020 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
1024 // get remote bt adapter method
1025 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
1026 if(!jni_mid_getDefaultAdapter)
1028 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
1032 // get start le scan method
1033 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1034 "(Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)V");
1035 if(!jni_mid_stopLeScan)
1037 OIC_LOG(DEBUG, TAG, "[BLE][Native] stopLeScan: jni_mid_stopLeScan is null");
1041 // gat bt adapter object
1042 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1043 if(!jni_obj_BTAdapter)
1045 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_BTAdapter is null");
1049 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to stop LE Scan");
1050 // call start le scan method
1051 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1054 int32_t CANativeLEConnect(JNIEnv *env, jobject bluetoothDevice, jobject context,
1055 jboolean autoconnect, jobject callback)
1057 if(!CALEIsEnableBTAdapter(env))
1059 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1063 jstring jni_address = CALEGetAddressFromBTDevice(env, bluetoothDevice);
1064 const char * addr = (*env)->GetStringUTFChars(env, jni_address, NULL);
1065 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] request connectGatt to %s", addr);
1069 // get BluetoothDevice class
1070 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothDevice class");
1071 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1072 if(!jni_cid_BluetoothDevice)
1074 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_cid_BluetoothDevice is null");
1078 // get connectGatt method
1079 OIC_LOG(DEBUG, TAG, "[BLE][Native] get connectGatt method");
1080 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice,
1082 "(Landroid/content/Context;ZLandroid/bluetooth/BluetoothGattCallback;)Landroid/bluetooth/BluetoothGatt;");
1083 if(!jni_mid_connectGatt)
1085 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_mid_connectGatt is null");
1089 OIC_LOG(DEBUG, TAG, "[BLE][Native] Call object method - connectGatt");
1090 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice,
1091 jni_mid_connectGatt, NULL, autoconnect, callback);
1092 if(!jni_obj_connectGatt)
1094 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - connectGatt was failed.obj will be removed");
1095 CANativeRemoveDevice(env, jni_address);
1096 CANativeupdateSendCnt(env);
1101 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - connecting..");
1106 void CANativeLEDisconnect(JNIEnv *env, jobject bluetoothGatt)
1108 if(!CALEIsEnableBTAdapter(env))
1110 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1115 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT DISCONNECT");
1117 // get BluetoothGatt class
1118 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt classjobject bluetoothGatt");
1119 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1120 if(!jni_cid_BluetoothGatt)
1122 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1126 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt disconnect method");
1127 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1128 "disconnect","()V");
1129 if(!jni_mid_disconnectGatt)
1131 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_disconnectGatt is null");
1135 // call disconnect gatt method
1136 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request disconnectGatt");
1137 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1141 void CANativeLEDisconnectAll(JNIEnv *env)
1143 OIC_LOG_V(DEBUG, TAG, "CANativeLEDisconnectAll");
1145 if(!gGattObjectList)
1147 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1152 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1154 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1157 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1160 CANativeLEDisconnect(env, jarrayObj);
1163 OICFree(gGattObjectList);
1164 gGattObjectList = NULL;
1168 void CANativeLEDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1170 if(!CALEIsEnableBTAdapter(env))
1172 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1176 // GATT SERVICE DISCOVERY
1177 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT SERVICE DISCOVERY");
1179 // get BluetoothGatt class
1180 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
1181 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1182 if(!jni_cid_BluetoothGatt)
1184 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1188 OIC_LOG(DEBUG, TAG, "[BLE][Native] discovery gatt services method");
1189 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1190 "discoverServices","()Z");
1191 if(!jni_mid_discoverServices)
1193 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_discoverServices is null");
1196 // call disconnect gatt method
1197 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request discovery gatt services");
1198 (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1201 jboolean CANativeLESendData(JNIEnv *env, jobject bluetoothGatt, jobject gattCharacteristic)
1203 if(!CALEIsEnableBTAdapter(env))
1205 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1209 // WRITE GATT CHARACTERISTIC
1210 OIC_LOG(DEBUG, TAG, "[BLE][Native] WRITE GATT CHARACTERISTIC");
1212 // get BluetoothGatt class
1213 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
1214 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1215 if(!jni_cid_BluetoothGatt)
1217 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1221 OIC_LOG(DEBUG, TAG, "[BLE][Native] write characteristic method");
1222 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1223 "writeCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
1224 if(!jni_mid_writeCharacteristic)
1226 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_writeCharacteristic is null");
1230 // call disconnect gatt method
1231 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to write gatt characteristic");
1232 jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeCharacteristic, gattCharacteristic);
1235 OIC_LOG(DEBUG, TAG, "[BLE][Native] writeCharacteristic is success");
1239 OIC_LOG(DEBUG, TAG, "[BLE][Native] writeCharacteristic is failed");
1245 void CANativeReadCharacteristic(JNIEnv *env, jobject bluetoothGatt)
1248 if(!CALEIsEnableBTAdapter(env))
1250 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1254 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1255 if(!jni_cid_BluetoothGatt)
1257 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1261 jstring jni_uuid = (*env)->NewStringUTF(env, IOTIVITY_GATT_RX_UUID);
1262 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1263 if(!jni_obj_GattCharacteristic)
1265 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1269 OIC_LOG(DEBUG, TAG, "[BLE][Native] read characteristic method");
1270 jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "readCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
1271 if(!jni_mid_readCharacteristic)
1273 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_readCharacteristic is null");
1277 // call disconnect gatt method
1278 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to read gatt characteristic");
1279 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic, jni_obj_GattCharacteristic);
1282 OIC_LOG(DEBUG, TAG, "[BLE][Native] readCharacteristic is success");
1286 OIC_LOG(DEBUG, TAG, "[BLE][Native] readCharacteristic is failed");
1290 jboolean CANativeSetCharacteristicNoti(JNIEnv *env, jobject bluetoothGatt, const char* uuid)
1292 if(!CALEIsEnableBTAdapter(env))
1294 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1298 // get BluetoothGatt class
1299 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeSetCharacteristicNoti");
1300 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1301 if(!jni_cid_BluetoothGatt)
1303 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1307 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1308 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1309 if(!jni_obj_GattCharacteristic)
1311 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1315 // set Characteristic Notification
1316 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "setCharacteristicNotification",
1317 "(Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z");
1318 if(!jni_mid_setNotification)
1320 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getService is null");
1324 jboolean enable = TRUE;
1325 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification, jni_obj_GattCharacteristic, enable);
1328 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - setCharacteristicNotification is success");
1333 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - setCharacteristicNotification is failed");
1338 jobject CANativeGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1340 if(!CALEIsEnableBTAdapter(env))
1342 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1346 // get BluetoothGatt class
1347 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeGetGattService");
1348 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1349 if(!jni_cid_BluetoothGatt)
1351 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1355 jmethodID jni_mid_getService = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "getService",
1356 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1357 if(!jni_mid_getService)
1359 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getService is null");
1363 jobject jni_obj_service_uuid = CANativeGetUUIDObject(env, IOTIVITY_GATT_SERVIE_UUID);
1364 if(!jni_obj_service_uuid)
1366 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_service_uuid is null");
1370 // get bluetooth gatt service
1371 OIC_LOG(DEBUG, TAG, "[BLE][Native] request to get service");
1372 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService, jni_obj_service_uuid);
1374 // get bluetooth gatt service class
1375 jclass jni_cid_BluetoothGattService = (*env)->FindClass(env, "android/bluetooth/BluetoothGattService");
1376 if(!jni_cid_BluetoothGattService)
1378 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGattService is null");
1382 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt getCharacteristic method");
1383 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService, "getCharacteristic",
1384 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattCharacteristic;");
1385 if(!jni_mid_getCharacteristic)
1387 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getCharacteristic is null");
1391 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1392 jobject jni_obj_tx_uuid = CANativeGetUUIDObject(env, uuid);
1393 if(!jni_obj_tx_uuid)
1395 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_tx_uuid is null");
1399 OIC_LOG(DEBUG, TAG, "[BLE][Native] request to get Characteristic");
1400 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService, jni_mid_getCharacteristic, jni_obj_tx_uuid);
1402 return jni_obj_GattCharacteristic;
1405 jobject CANativeCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data)
1407 if(!CALEIsEnableBTAdapter(env))
1409 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1413 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateGattCharacteristic");
1414 jstring jni_uuid = (*env)->NewStringUTF(env, IOTIVITY_GATT_TX_UUID);
1415 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1416 if(!jni_obj_GattCharacteristic)
1418 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1422 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/BluetoothGattCharacteristic");
1423 if(!jni_cid_BTGattCharacteristic)
1425 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTGattCharacteristic is null");
1429 OIC_LOG(DEBUG, TAG, "[BLE][Native] set value in Characteristic");
1430 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1432 if(!jni_mid_setValue)
1434 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_setValue is null");
1438 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue, data);
1441 OIC_LOG(DEBUG, TAG, "[BLE][Native] the locally stored value has been set");
1442 return jni_obj_GattCharacteristic;
1446 OIC_LOG(DEBUG, TAG, "[BLE][Native] the locally stored value hasn't been set");
1451 jbyteArray CANativeGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
1453 if(!CALEIsEnableBTAdapter(env))
1455 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1459 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/BluetoothGattCharacteristic");
1460 if(!jni_cid_BTGattCharacteristic)
1462 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTGattCharacteristic is null");
1466 OIC_LOG(DEBUG, TAG, "[BLE][Native] get value in Characteristic");
1467 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
1469 if(!jni_mid_getValue)
1471 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getValue is null");
1475 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic, jni_mid_getValue);
1476 return jni_obj_data_array;
1480 void CANativeCreateUUIDList()
1482 jboolean isAttached = FALSE;
1484 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
1487 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
1488 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1492 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
1498 // create new object array
1499 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1500 if(!jni_cid_uuid_list)
1502 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_uuid_list is null");
1506 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1, jni_cid_uuid_list, NULL);
1507 if(!jni_obj_uuid_list)
1509 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_uuid_list is null");
1513 // remove previous list and create list again
1514 CANativeRemoveAllDevices(env);
1515 CANativeCreateScanDeviceList(env);
1518 jobject jni_obj_uuid = CANativeGetUUIDObject(env, IOTIVITY_GATT_SERVIE_UUID);
1519 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
1521 gUUIDList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list);
1524 (*g_jvm)->DetachCurrentThread(g_jvm);
1527 void CANativeCreateScanDeviceList(JNIEnv *env)
1529 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateScanDeviceList");
1531 // create new object array
1532 if (gdeviceList == NULL)
1534 OIC_LOG_V(DEBUG, TAG, "Create device list");
1536 gdeviceList = u_arraylist_create();
1540 void CANativeAddScanDeviceToList(JNIEnv *env, jobject device)
1544 OIC_LOG(DEBUG, TAG, "[BLE][Native] device is null");
1550 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdevice_list is null");
1554 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
1555 if(!jni_remoteAddress)
1557 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1561 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1563 if(!CANativeIsDeviceInList(env, remoteAddress)) {
1564 jobject gdevice = (*env)->NewGlobalRef(env, device);
1565 u_arraylist_add(gdeviceList, gdevice);
1566 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1570 jboolean CANativeIsDeviceInList(JNIEnv *env, const char* remoteAddress){
1571 // get address value from device list
1574 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1576 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1579 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1583 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
1586 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1590 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1592 if(!strcmp(remoteAddress, setAddress))
1594 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1603 OIC_LOG_V(DEBUG, TAG, "there are no the device in list. we can add");
1607 void CANativeRemoveAllDevices(JNIEnv *env)
1609 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDevices");
1613 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
1618 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1620 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1623 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1626 (*env)->DeleteGlobalRef(env, jarrayObj);
1629 OICFree(gdeviceList);
1634 void CANativeRemoveDevice(JNIEnv *env, jstring address)
1636 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveDevice");
1640 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
1645 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1647 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1650 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1654 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
1657 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1660 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1661 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
1663 if(!strcmp(setAddress, remoteAddress))
1665 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
1666 (*env)->DeleteGlobalRef(env, jarrayObj);
1668 CAReorderingDeviceList(index);
1672 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
1676 void CAReorderingDeviceList(uint32_t index)
1678 if (index >= gdeviceList->length)
1683 if (index < gdeviceList->length - 1)
1685 memmove(&gdeviceList->data[index], &gdeviceList->data[index + 1],
1686 (gdeviceList->length - index - 1) * sizeof(void *));
1689 gdeviceList->size--;
1690 gdeviceList->length--;
1696 void CANativeCreateGattObjList(JNIEnv *env)
1698 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateGattObjList");
1700 // create new object array
1701 if (gGattObjectList == NULL)
1703 OIC_LOG_V(DEBUG, TAG, "Create Gatt object list");
1705 gGattObjectList = u_arraylist_create();
1709 void CANativeAddGattobjToList(JNIEnv *env, jobject gatt)
1711 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeAddGattobjToList");
1715 OIC_LOG(DEBUG, TAG, "[BLE][Native] gatt is null");
1719 if(!gGattObjectList)
1721 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1725 jstring jni_remoteAddress = CANativeGetAddressFromGattObj(env, gatt);
1726 if(!jni_remoteAddress)
1728 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1732 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1734 if(!CANativeIsGattObjInList(env, remoteAddress))
1736 jobject gGatt = (*env)->NewGlobalRef(env, gatt);
1737 u_arraylist_add(gGattObjectList, gGatt);
1738 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1742 jboolean CANativeIsGattObjInList(JNIEnv *env, const char* remoteAddress)
1744 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsGattObjInList");
1747 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1750 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1753 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1757 jstring jni_setAddress = CANativeGetAddressFromGattObj(env, jarrayObj);
1760 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1764 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1766 if(!strcmp(remoteAddress, setAddress))
1768 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1777 OIC_LOG_V(DEBUG, TAG, "there are no the gatt obejct in list. we can add");
1781 void CANativeRemoveAllGattObjsList(JNIEnv *env)
1783 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllGattObjsList");
1785 if(!gGattObjectList)
1787 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1792 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1794 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1797 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1800 (*env)->DeleteGlobalRef(env, jarrayObj);
1803 OICFree(gGattObjectList);
1804 gGattObjectList = NULL;
1808 void CANativeRemoveGattObj(JNIEnv *env, jobject gatt)
1810 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveGattObj");
1812 if(!gGattObjectList)
1814 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1819 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1821 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1824 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1828 jstring jni_setAddress = CANativeGetAddressFromGattObj(env, jarrayObj);
1831 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1834 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1836 jstring jni_remoteAddress = CANativeGetAddressFromGattObj(env, gatt);
1837 if(!jni_remoteAddress)
1839 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1842 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1844 if(!strcmp(setAddress, remoteAddress))
1846 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
1847 (*env)->DeleteGlobalRef(env, jarrayObj);
1849 CAReorderingGattList(index);
1853 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
1857 void CAReorderingGattList(uint32_t index)
1859 if (index >= gGattObjectList->length)
1864 if (index < gGattObjectList->length - 1)
1866 memmove(&gGattObjectList->data[index], &gGattObjectList->data[index + 1],
1867 (gGattObjectList->length - index - 1) * sizeof(void *));
1870 gGattObjectList->size--;
1871 gGattObjectList->length--;
1875 * Check Sent Count for remove gSendBuffer
1877 void CANativeupdateSendCnt(JNIEnv *env)
1880 u_mutex_lock(gThreadMutex);
1884 if(gTargetCnt <= gCurrentSentCnt)
1887 gCurrentSentCnt = 0;
1891 (*env)->DeleteGlobalRef(env, gSendBuffer);
1894 // notity the thread
1895 u_cond_signal(gThreadCond);
1896 gIsFinishSendData = TRUE;
1897 OIC_LOG(DEBUG, TAG, "set signal for send data");
1900 u_mutex_unlock(gThreadMutex);