6 #include "caleserver.h"
8 #include "oic_malloc.h"
9 #include "uthreadpool.h" /* for thread pool */
11 #include "uarraylist.h"
12 #include "com_iotivity_jar_CALeInterface.h"
17 static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
18 static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
19 static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
20 static const char *CLASSPATH_BT_UUID = "java/util/UUID";
21 static const char *CLASSPATH_BT_GATT = "android/bluetooth/BluetoothGatt";
23 static const char *IOTIVITY_GATT_SERVIE_UUID = "713d0000-503e-4c75-ba94-3148f18d941e";
24 static const char *IOTIVITY_GATT_TX_UUID = "713d0003-503e-4c75-ba94-3148f18d941e";
25 static const char *IOTIVITY_GATT_RX_UUID = "713d0002-503e-4c75-ba94-3148f18d941e";
27 static const uint32_t STATE_CONNECTED = 2;
28 static const uint32_t STATE_DISCONNECTED = 0;
29 static const uint32_t GATT_SUCCESS = 0;
32 static u_arraylist_t *gdeviceList = NULL;
33 static u_arraylist_t *gGattObjectList = NULL;
34 static CAPacketReceiveCallback gPacketReceiveCallback = NULL;
35 static u_thread_pool_t gThreadPoolHandle = NULL;
36 static jobject gLeScanCallback;
37 static jobject gLeGattCallback;
38 static jobject gContext;
39 static jobjectArray gUUIDList;
40 static jboolean gIsStartServer;
41 static jboolean gIsFinishSendData;
43 static jbyteArray gSendBuffer;
44 static uint32_t gTargetCnt = 0;
45 static uint32_t gCurrentSentCnt = 0;
47 /** mutex for synchrnoization **/
48 static u_mutex gThreadMutex;
49 /** conditional mutex for synchrnoization **/
50 static u_cond gThreadCond;
52 ////////////////////////////////////////////////////////////////////////////////////////////////////
53 //FIXME getting context
54 void CALEClientJNISetContext(JNIEnv *env, jobject context)
56 OIC_LOG_V(DEBUG, TAG, "CALEClientJNISetContext");
60 OIC_LOG_V(DEBUG, TAG, "context is null");
64 gContext = (*env)->NewGlobalRef(env, context);
67 void CALeCreateJniInterfaceObject()
69 OIC_LOG_V(DEBUG, TAG, "CALeCreateJniInterfaceObject");
71 jboolean isAttached = FALSE;
73 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
76 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
77 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
81 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
87 jclass LeJniInterface = (*env)->FindClass(env, "com/iotivity/jar/CALeInterface");
90 OIC_LOG_V(DEBUG, TAG, "Could not get CALeInterface class");
94 jmethodID LeInterfaceConstructorMethod =
95 (*env)->GetMethodID(env, LeJniInterface, "<init>", "(Landroid/content/Context;)V");
96 if (!LeInterfaceConstructorMethod)
98 OIC_LOG_V(DEBUG, TAG, "Could not get CALeInterface constructor method");
102 (*env)->NewObject(env, LeJniInterface, LeInterfaceConstructorMethod, gContext);
103 OIC_LOG_V(DEBUG, TAG, "Create CALeInterface instance");
106 (*g_jvm)->DetachCurrentThread(g_jvm);
109 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
111 OIC_LOG_V(DEBUG, TAG, "JNI_OnLoad in calecore");
114 if((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
118 g_jvm = jvm; /* cache the JavaVM pointer */
120 //JVM required for WifiCore to work with JNI interface
122 CALeServerJniInit(env, jvm);
124 return JNI_VERSION_1_6;
127 void JNI_OnUnload(JavaVM *jvm, void *reserved)
129 OIC_LOG_V(DEBUG, TAG, "JNI_OnUnload in calecore");
132 if((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
140 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CARegisterLeScanCallback(JNIEnv *env,
141 jobject obj, jobject callback)
143 OIC_LOG_V(DEBUG, TAG, "CARegisterLeScanCallback");
145 gLeScanCallback = (*env)->NewGlobalRef(env, callback);
148 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CARegisterLeGattCallback(JNIEnv *env,
149 jobject obj, jobject callback)
151 OIC_LOG_V(DEBUG, TAG, "CARegisterLeGattCallback");
153 gLeGattCallback = (*env)->NewGlobalRef(env, callback);
156 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeScanCallback (JNIEnv *env,
157 jobject obj, jobject device, jint rssi, jbyteArray scanRecord)
159 CANativeAddScanDeviceToList(env, device);
163 * Class: com_iotivity_jar_CALeInterface
164 * Method: CALeGattConnectionStateChangeCallback
165 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
167 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattConnectionStateChangeCallback
168 (JNIEnv *env, jobject obj, jobject gatt, jint status, jint newstate)
170 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status, newstate);
172 if(GATT_SUCCESS == status && STATE_CONNECTED == newstate) // le connected
175 CANativeAddGattobjToList(env, gatt);
176 CANativeLEDiscoverServices(env, gatt);
179 else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate) // le disconnected
182 CANativeGattClose(env, gatt);
183 CANativeRemoveGattObj(env, gatt);
188 CANativeSendFinsih(env, gatt);
193 * Class: com_iotivity_jar_CALeInterface
194 * Method: CALeGattServicesDiscoveredCallback
195 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
197 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattServicesDiscoveredCallback
198 (JNIEnv *env, jobject obj, jobject gatt, jint status)
200 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
202 if(0 != status) // discovery error
204 CANativeSendFinsih(env, gatt);
208 // read Characteristic
209 // CANativeReadCharacteristic(env, gatt);
211 jboolean ret_rx = CANativeSetCharacteristicNoti(env, gatt, IOTIVITY_GATT_RX_UUID);
212 if(!ret_rx) // SetCharacteristicNoti is failed
214 CANativeSendFinsih(env, gatt);
218 // jstring data = (*env)->NewStringUTF(env, "HelloWorld");
219 jobject jni_obj_character = CANativeCreateGattCharacteristic(env, gatt, gSendBuffer);
220 if(!jni_obj_character)
222 CANativeSendFinsih(env, gatt);
227 jboolean ret = CANativeLESendData(env, gatt, jni_obj_character);
230 CANativeSendFinsih(env, gatt);
236 * Class: com_iotivity_jar_CALeInterface
237 * Method: CALeGattCharacteristicReadCallback
238 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
240 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicReadCallback
241 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data, jint status)
243 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicReadCallback - status : %d", status);
246 char* readData = (char*)(*env)->GetByteArrayElements(env, data, &isCopy);
248 jstring jni_address = CANativeGetAddressFromGattObj(env, gatt);
249 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
251 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicReadCallback - read data : %s", readData);
253 gPacketReceiveCallback(address, readData);
257 * Class: com_iotivity_jar_CALeInterface
258 * Method: CALeGattCharacteristicWritjclasseCallback
259 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
261 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicWriteCallback
262 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data, jint status)
264 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
267 char* writeData = (char*)(*env)->GetByteArrayElements(env, data, &isCopy);
269 jstring jni_address = CANativeGetAddressFromGattObj(env, gatt);
270 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
272 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", writeData);
275 CANativeSendFinsih(env, gatt);
280 CANativeSendFinsih(env, gatt);
285 * Class: com_iotivity_jar_CALeInterface
286 * Method: CALeGattCharacteristicChangedCallback
287 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
289 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicChangedCallback
290 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data)
292 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
295 char* NotificationData = (char*)(*env)->GetByteArrayElements(env, data, &isCopy);
297 jstring jni_address = CANativeGetAddressFromGattObj(env, gatt);
298 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
300 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data : %s", NotificationData);
302 gPacketReceiveCallback(address, NotificationData);
304 CANativeSendFinsih(env, gatt);
308 * Class: com_iotivity_jar_CALeInterface
309 * Method: CALeGattDescriptorReadCallback
310 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
312 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattDescriptorReadCallback
313 (JNIEnv *env, jobject obj, jobject gatt, jobject descriptor, jint status)
315 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorReadCallback - status %d: ", status);
319 * Class: com_iotivity_jar_CALeInterface
320 * Method: CALeGattDescriptorWriteCallback
321 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
323 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattDescriptorWriteCallback
324 (JNIEnv *env, jobject obj, jobject gatt, jobject descriptor, jint status)
326 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
330 * Class: com_iotivity_jar_CALeInterface
331 * Method: CALeGattReliableWriteCompletedCallback
332 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
334 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattReliableWriteCompletedCallback
335 (JNIEnv *env, jobject obj, jobject gatt, jint status)
337 OIC_LOG_V(DEBUG, TAG, "CALeGattReliableWriteCompletedCallback - status %d: ", status);
341 * Class: com_iotivity_jar_CALeInterface
342 * Method: CALeGattReadRemoteRssiCallback
343 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
345 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattReadRemoteRssiCallback
346 (JNIEnv *env, jobject obj, jobject gatt, jint rssi, jint status)
348 OIC_LOG_V(DEBUG, TAG, "CALeGattReadRemoteRssiCallback - rssi %d, status %d: ", rssi, status);
352 void CALEInitialize(u_thread_pool_t handle)
354 OIC_LOG(DEBUG, TAG, "CALEInitialize");
356 gThreadPoolHandle = handle;
358 // init mutex for send logic
359 gThreadMutex = u_mutex_new();
360 gThreadCond = u_cond_new();
362 CANativeCreateUUIDList();
364 CALeCreateJniInterfaceObject(); /* create java CALeInterface instance*/
369 OIC_LOG(DEBUG, TAG, "CALETerminate");
371 jboolean isAttached = FALSE;
373 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
376 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
377 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
381 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
387 CANativeLEDisconnectAll(env);
391 CANativeLEStopScanImpl(env, gLeScanCallback);
397 (*env)->DeleteGlobalRef(env, gLeScanCallback);
402 (*env)->DeleteGlobalRef(env, gLeGattCallback);
407 (*env)->DeleteGlobalRef(env, gContext);
412 (*env)->DeleteGlobalRef(env, gSendBuffer);
417 (*env)->DeleteGlobalRef(env, gUUIDList);
420 CANativeRemoveAllDevices(env);
421 CANativeRemoveAllGattObjsList(env);
422 gIsStartServer = FALSE;
423 gIsFinishSendData = FALSE;
426 (*g_jvm)->DetachCurrentThread(g_jvm);
428 // delete mutex object
429 u_mutex_free(gThreadMutex);
431 u_cond_free(gThreadCond);
434 void CANativeSendFinsih(JNIEnv *env, jobject gatt)
436 OIC_LOG_V(DEBUG, TAG, "CANativeSendFinsih");
440 CANativeLEDisconnect(env, gatt);
442 CANativeupdateSendCnt(env);
445 int32_t CALESendUnicastMessage(const char* address, const char* data, uint32_t dataLen)
447 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastMessage(%s, %s)", address, data);
449 CALESendUnicastMessageImpl(address, data, dataLen);
453 int32_t CALESendMulticastMessage(const char* data, uint32_t dataLen)
455 OIC_LOG_V(DEBUG, TAG, "CALESendMulticastMessage(%s)", data);
457 jboolean isAttached = FALSE;
459 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
462 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
463 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
467 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
473 CALESendMulticastMessageImpl(env, data, dataLen);
476 (*g_jvm)->DetachCurrentThread(g_jvm);
481 int32_t CALEStartUnicastServer(const char* address)
483 OIC_LOG_V(DEBUG, TAG, "CALEStartUnicastServer(%s)", address);
488 int32_t CALEStartMulticastServer()
490 OIC_LOG_V(DEBUG, TAG, "CALEStartMulticastServer");
494 OIC_LOG_V(DEBUG, TAG, "server is already started..it will be skipped");
498 jboolean isAttached = FALSE;
500 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
503 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
504 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
508 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
514 gIsStartServer = TRUE;
516 CANativeLEStartScan();
519 (*g_jvm)->DetachCurrentThread(g_jvm);
524 int32_t CALEStopUnicastServer(int32_t serverID)
526 OIC_LOG(DEBUG, TAG, "CALEStopUnicastServer");
531 int32_t CALEStopMulticastServer(int32_t serverID)
533 OIC_LOG(DEBUG, TAG, "CALEStopMulticastServer");
534 gIsStartServer = FALSE;
535 CANativeLEStopScan();
539 void CALESetCallback(CAPacketReceiveCallback callback)
541 gPacketReceiveCallback = callback;
544 CAResult_t CALEGetInterfaceInfo(char **address)
546 CALEGetLocalAddress(address);
550 void CALEGetLocalAddress(char** address)
552 jboolean isAttached = FALSE;
554 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
557 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
558 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
561 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
567 jstring jni_address = CANativeGetLocalDeviceAddress(env);
570 const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
571 *address = (char*)OICMalloc(strlen(localAddress) + 1);
572 memcpy(*address, localAddress, strlen(localAddress));
575 OIC_LOG_V(DEBUG, TAG, "Local Address : %s", *address);
577 (*g_jvm)->DetachCurrentThread(g_jvm);
580 int32_t CALESendUnicastMessageImpl(const char* address, const char* data, uint32_t dataLen)
582 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastMessageImpl, address: %s, data: %s", address, data);
584 jboolean isAttached = FALSE;
586 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
589 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
590 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
593 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
599 OIC_LOG(DEBUG, TAG, "[BLE][Native] set byteArray for data");
602 (*env)->DeleteGlobalRef(env, gSendBuffer);
604 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
605 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*)data);
606 gSendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
608 // connect to gatt server
609 CANativeLEStopScanImpl(env, gLeScanCallback);
612 jobject jni_obj_bluetoothDevice = NULL;
613 if(gContext && gdeviceList)
616 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
618 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
621 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
625 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
628 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
631 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
633 if(!strcmp(setAddress, address))
635 jni_obj_bluetoothDevice = jarrayObj;
640 if(jni_obj_bluetoothDevice)
642 jboolean autoConnect = FALSE;
643 CANativeLEConnect(env, jni_obj_bluetoothDevice, gContext, autoConnect, gLeGattCallback);
648 (*g_jvm)->DetachCurrentThread(g_jvm);
653 int32_t CALESendMulticastMessageImpl(JNIEnv *env, const char* data, uint32_t dataLen)
655 OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s, %d", data, dataLen);
659 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
662 OIC_LOG(DEBUG, TAG, "set wait");
665 gIsFinishSendData = FALSE;
667 OIC_LOG(DEBUG, TAG, "[BLE][Native] set byteArray for data");
670 (*env)->DeleteGlobalRef(env, gSendBuffer);
672 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
673 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*)data);
674 gSendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
676 // connect to gatt server
677 CANativeLEStopScanImpl(env, gLeScanCallback);
681 CANativeRemoveAllGattObjsList(env);
682 CANativeCreateGattObjList(env);
684 gTargetCnt = u_arraylist_length(gdeviceList);
688 while (index < u_arraylist_length(gdeviceList)) {
689 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
692 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
696 if(0 <= CANativeLEConnect(env, jarrayObj, gContext, FALSE, gLeGattCallback))
698 // connection failure
704 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
705 if(!gIsFinishSendData)
707 u_mutex_lock(gThreadMutex);
708 u_cond_wait(gThreadCond, gThreadMutex);
709 OIC_LOG(DEBUG, TAG, "unset wait");
710 u_mutex_unlock(gThreadMutex);
713 // start LE Scan again
715 CANativeLEStartScan();
724 jstring CANativeGetLocalDeviceAddress(JNIEnv* env)
726 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
727 if(!jni_cid_BTAdapter)
729 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_cid_BTAdapter is null");
733 jmethodID jni_mid_getDefaultAdapter =
734 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
735 if(!jni_mid_getDefaultAdapter)
737 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_mid_getDefaultAdapter is null");
741 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getAddress", METHODID_STRINGNONPARAM);
742 if(!jni_mid_getAddress)
744 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_mid_getAddress is null");
748 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
749 if(!jni_obj_BTAdapter)
751 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_obj_BTAdapter is null");
755 jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getAddress);
758 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_str_address is null");
762 return jni_str_address;
765 jobjectArray CANativeBondedDevices(JNIEnv *env)
767 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
768 if(!jni_cid_BTAdapter)
770 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_cid_BTAdapter is null");
774 jmethodID jni_mid_getDefaultAdapter =
775 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
777 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
778 if(!jni_obj_BTAdapter)
780 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: bluetooth adapter is null");
784 // Get a list of currently paired devices
785 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
786 "getBondedDevices", "()Ljava/util/Set;");
787 if(!jni_mid_getBondedDevices)
789 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
793 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getBondedDevices);
794 if(!jni_obj_setPairedDevices)
796 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_obj_setPairedDevices is null");
800 // Convert the set to an object array
801 // object[] array = Set<BluetoothDevice>.toArray();
802 jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
803 jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray", "()[Ljava/lang/Object;");
807 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_mid_toArray is null");
811 jobjectArray jni_arrayPairedDevices = (jobjectArray)((*env)->CallObjectMethod(env,
812 jni_obj_setPairedDevices, jni_mid_toArray));
813 if(!jni_arrayPairedDevices)
815 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_arrayPairedDevices is null");
819 return jni_arrayPairedDevices;
822 jint CANativeGetBTStateOnInfo(JNIEnv *env)
824 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
825 if(!jni_cid_BTAdapter)
827 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
831 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
832 if (jni_fid_stateon == 0)
834 OIC_LOG(DEBUG, TAG, "[BLE][Native] get_field_state is 0");
837 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
839 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] bluetooth STATE_ON state integer value : %d", jni_int_val);
844 jboolean CANativeIsEnableBTAdapter(JNIEnv *env)
846 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
847 if(!jni_cid_BTAdapter)
849 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
853 jmethodID jni_mid_getDefaultAdapter =
854 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
855 if(!jni_mid_getDefaultAdapter)
857 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getDefaultAdapter is null");
861 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
862 if(!jni_obj_BTAdapter)
864 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_BTAdapter is null");
869 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled",
871 if(!jni_mid_isEnable)
873 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_isEnable is null");
877 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
878 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] adapter state is %d", jni_isEnable);
883 jstring CANativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
885 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
886 if(!jni_cid_device_list)
888 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_device_list is null");
892 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
893 "()Ljava/lang/String;");
894 if(!jni_mid_getAddress)
896 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getAddress is null");
900 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice, jni_mid_getAddress);
903 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_address is null");
909 jstring CANativeGetAddressFromGattObj(JNIEnv *env, jobject gatt)
913 OIC_LOG(DEBUG, TAG, "[BLE][Native] gatt is null");
917 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
918 if(!jni_cid_gattdevice_list)
920 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_gattdevice_list is null");
924 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
925 "()Landroid/bluetooth/BluetoothDevice;");
926 if(!jni_mid_getDevice)
928 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getDevice is null");
932 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
935 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_device is null");
939 jstring jni_address = CANativeGetAddressFromBTDevice(env, jni_obj_device);
942 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_address is null");
952 void CANativeGattClose(JNIEnv *env, jobject bluetoothGatt)
955 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT CLOSE");
957 // get BluetoothGatt class
958 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
959 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
960 if(!jni_cid_BluetoothGatt)
962 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
966 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close","()V");
967 if(!jni_mid_closeGatt)
969 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_closeGatt is null");
973 // call disconnect gatt method
974 OIC_LOG(DEBUG, TAG, "[BLE][Native] request close Gatt");
975 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
978 void CANativeLEStartScan()
982 OIC_LOG_V(DEBUG, TAG, "server is not started yet..scan will be passed");
986 jboolean isAttached = FALSE;
988 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
991 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
993 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
996 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
1002 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeLEStartScan");
1004 // scan gatt server with UUID
1005 if(gLeScanCallback && gUUIDList)
1007 CANativeLEStartScanWithUUIDImpl(env, gUUIDList, gLeScanCallback);
1011 (*g_jvm)->DetachCurrentThread(g_jvm);
1015 void CANativeLEStartScanImpl(JNIEnv *env, jobject callback)
1017 if(!CANativeIsEnableBTAdapter(env))
1019 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1023 // get default bt adapter class
1024 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
1025 if(!jni_cid_BTAdapter)
1027 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
1031 // get remote bt adapter method
1032 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
1033 if(!jni_mid_getDefaultAdapter)
1035 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
1039 // get start le scan method
1040 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1041 "(Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)Z");
1042 if(!jni_mid_startLeScan)
1044 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan: jni_mid_startLeScan is null");
1048 // gat bt adapter object
1049 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1050 if(!jni_obj_BTAdapter)
1052 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
1056 // call start le scan method
1057 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_startLeScan, callback);
1058 if(!jni_obj_startLeScan)
1060 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan is failed");
1065 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan is started");
1069 jobject CANativeGetUUIDObject(JNIEnv *env, const char* uuid)
1072 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1075 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_uuid is null");
1079 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;");
1080 if(!jni_mid_fromString)
1082 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_fromString is null");
1086 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1087 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString, jni_uuid);
1090 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_uuid is null");
1094 return jni_obj_uuid;
1097 void CANativeLEStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
1099 // get default bt adapter class
1100 OIC_LOG(DEBUG, TAG, "[BLE][Native] get default bt adapter class");
1102 if(!CANativeIsEnableBTAdapter(env))
1104 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1108 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
1109 if(!jni_cid_BTAdapter)
1111 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
1115 // get remote bt adapter method
1116 OIC_LOG(DEBUG, TAG, "[BLE][Native] get remote bt adapter method");
1117 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
1118 if(!jni_mid_getDefaultAdapter)
1120 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
1124 // get start le scan method
1125 OIC_LOG(DEBUG, TAG, "[BLE][Native] get start le scan method");
1126 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1127 "([Ljava/util/UUID;Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)Z");
1128 if(!jni_mid_startLeScan)
1130 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan: jni_mid_startLeScan is null");
1134 // get bt adapter object
1135 OIC_LOG(DEBUG, TAG, "[BLE][Native] get bt adapter object");
1136 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1137 if(!jni_obj_BTAdapter)
1139 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
1143 // call start le scan method
1144 OIC_LOG(DEBUG, TAG, "[BLE][Native] call start le scan service method");
1145 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_startLeScan, uuids, callback);
1146 if(!jni_obj_startLeScan)
1148 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan With UUID is failed");
1153 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan With UUID is started");
1157 void CANativeLEStopScan()
1159 jboolean isAttached = FALSE;
1161 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
1164 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
1165 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1168 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
1174 CANativeLEStopScanImpl(env, gLeScanCallback);
1177 (*g_jvm)->DetachCurrentThread(g_jvm);
1181 void CANativeLEStopScanImpl(JNIEnv *env, jobject callback)
1183 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeLEStopScan");
1185 if(!CANativeIsEnableBTAdapter(env))
1187 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1191 // get default bt adapter class
1192 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
1193 if(!jni_cid_BTAdapter)
1195 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
1199 // get remote bt adapter method
1200 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
1201 if(!jni_mid_getDefaultAdapter)
1203 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
1207 // get start le scan method
1208 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1209 "(Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)V");
1210 if(!jni_mid_stopLeScan)
1212 OIC_LOG(DEBUG, TAG, "[BLE][Native] stopLeScan: jni_mid_stopLeScan is null");
1216 // gat bt adapter object
1217 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1218 if(!jni_obj_BTAdapter)
1220 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_BTAdapter is null");
1224 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to stop LE Scan");
1225 // call start le scan method
1226 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1229 int32_t CANativeLEConnect(JNIEnv *env, jobject bluetoothDevice, jobject context,
1230 jboolean autoconnect, jobject callback)
1232 if(!CANativeIsEnableBTAdapter(env))
1234 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1238 jstring jni_address = CANativeGetAddressFromBTDevice(env, bluetoothDevice);
1239 const char * addr = (*env)->GetStringUTFChars(env, jni_address, NULL);
1240 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] request connectGatt to %s", addr);
1244 // get BluetoothDevice class
1245 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothDevice class");
1246 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1247 if(!jni_cid_BluetoothDevice)
1249 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_cid_BluetoothDevice is null");
1253 // get connectGatt method
1254 OIC_LOG(DEBUG, TAG, "[BLE][Native] get connectGatt method");
1255 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice,
1257 "(Landroid/content/Context;ZLandroid/bluetooth/BluetoothGattCallback;)Landroid/bluetooth/BluetoothGatt;");
1258 if(!jni_mid_connectGatt)
1260 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_mid_connectGatt is null");
1264 OIC_LOG(DEBUG, TAG, "[BLE][Native] Call object method - connectGatt");
1265 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice,
1266 jni_mid_connectGatt, NULL, autoconnect, callback);
1267 if(!jni_obj_connectGatt)
1269 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - connectGatt was failed.obj will be removed");
1270 CANativeRemoveDevice(env, jni_address);
1271 CANativeupdateSendCnt(env);
1276 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - connecting..");
1281 void CANativeLEDisconnect(JNIEnv *env, jobject bluetoothGatt)
1283 if(!CANativeIsEnableBTAdapter(env))
1285 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1290 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT DISCONNECT");
1292 // get BluetoothGatt class
1293 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt classjobject bluetoothGatt");
1294 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1295 if(!jni_cid_BluetoothGatt)
1297 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1301 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt disconnect method");
1302 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1303 "disconnect","()V");
1304 if(!jni_mid_disconnectGatt)
1306 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_disconnectGatt is null");
1310 // call disconnect gatt method
1311 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request disconnectGatt");
1312 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1316 void CANativeLEDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1318 if(!CANativeIsEnableBTAdapter(env))
1320 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1324 // GATT SERVICE DISCOVERY
1325 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT SERVICE DISCOVERY");
1327 // get BluetoothGatt class
1328 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
1329 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1330 if(!jni_cid_BluetoothGatt)
1332 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1336 OIC_LOG(DEBUG, TAG, "[BLE][Native] discovery gatt services method");
1337 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1338 "discoverServices","()Z");
1339 if(!jni_mid_discoverServices)
1341 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_discoverServices is null");
1344 // call disconnect gatt method
1345 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request discovery gatt services");
1346 (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1349 jboolean CANativeLESendData(JNIEnv *env, jobject bluetoothGatt, jobject gattCharacteristic)
1351 if(!CANativeIsEnableBTAdapter(env))
1353 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1357 // WRITE GATT CHARACTERISTIC
1358 OIC_LOG(DEBUG, TAG, "[BLE][Native] WRITE GATT CHARACTERISTIC");
1360 // get BluetoothGatt class
1361 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
1362 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1363 if(!jni_cid_BluetoothGatt)
1365 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1369 OIC_LOG(DEBUG, TAG, "[BLE][Native] write characteristic method");
1370 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1371 "writeCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
1372 if(!jni_mid_writeCharacteristic)
1374 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_writeCharacteristic is null");
1378 // call disconnect gatt method
1379 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to write gatt characteristic");
1380 jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeCharacteristic, gattCharacteristic);
1383 OIC_LOG(DEBUG, TAG, "[BLE][Native] writeCharacteristic is success");
1387 OIC_LOG(DEBUG, TAG, "[BLE][Native] writeCharacteristic is failed");
1393 void CANativeReadCharacteristic(JNIEnv *env, jobject bluetoothGatt)
1396 if(!CANativeIsEnableBTAdapter(env))
1398 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1402 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1403 if(!jni_cid_BluetoothGatt)
1405 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1409 jstring jni_uuid = (*env)->NewStringUTF(env, IOTIVITY_GATT_RX_UUID);
1410 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1411 if(!jni_obj_GattCharacteristic)
1413 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1417 OIC_LOG(DEBUG, TAG, "[BLE][Native] read characteristic method");
1418 jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "readCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
1419 if(!jni_mid_readCharacteristic)
1421 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_readCharacteristic is null");
1425 // call disconnect gatt method
1426 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to read gatt characteristic");
1427 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic, jni_obj_GattCharacteristic);
1430 OIC_LOG(DEBUG, TAG, "[BLE][Native] readCharacteristic is success");
1434 OIC_LOG(DEBUG, TAG, "[BLE][Native] readCharacteristic is failed");
1438 jboolean CANativeSetCharacteristicNoti(JNIEnv *env, jobject bluetoothGatt, const char* uuid)
1440 if(!CANativeIsEnableBTAdapter(env))
1442 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1446 // get BluetoothGatt class
1447 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeSetCharacteristicNoti");
1448 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1449 if(!jni_cid_BluetoothGatt)
1451 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1455 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1456 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1457 if(!jni_obj_GattCharacteristic)
1459 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1463 // set Characteristic Notification
1464 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "setCharacteristicNotification",
1465 "(Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z");
1466 if(!jni_mid_setNotification)
1468 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getService is null");
1472 jboolean enable = TRUE;
1473 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification, jni_obj_GattCharacteristic, enable);
1476 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - setCharacteristicNotification is success");
1481 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - setCharacteristicNotification is failed");
1486 jobject CANativeGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1488 if(!CANativeIsEnableBTAdapter(env))
1490 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1494 // get BluetoothGatt class
1495 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeGetGattService");
1496 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1497 if(!jni_cid_BluetoothGatt)
1499 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1503 jmethodID jni_mid_getService = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "getService",
1504 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1505 if(!jni_mid_getService)
1507 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getService is null");
1511 jobject jni_obj_service_uuid = CANativeGetUUIDObject(env, IOTIVITY_GATT_SERVIE_UUID);
1512 if(!jni_obj_service_uuid)
1514 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_service_uuid is null");
1518 // get bluetooth gatt service
1519 OIC_LOG(DEBUG, TAG, "[BLE][Native] request to get service");
1520 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService, jni_obj_service_uuid);
1522 // get bluetooth gatt service class
1523 jclass jni_cid_BluetoothGattService = (*env)->FindClass(env, "android/bluetooth/BluetoothGattService");
1524 if(!jni_cid_BluetoothGattService)
1526 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGattService is null");
1530 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt getCharacteristic method");
1531 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService, "getCharacteristic",
1532 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattCharacteristic;");
1533 if(!jni_mid_getCharacteristic)
1535 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getCharacteristic is null");
1539 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1540 jobject jni_obj_tx_uuid = CANativeGetUUIDObject(env, uuid);
1541 if(!jni_obj_tx_uuid)
1543 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_tx_uuid is null");
1547 OIC_LOG(DEBUG, TAG, "[BLE][Native] request to get Characteristic");
1548 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService, jni_mid_getCharacteristic, jni_obj_tx_uuid);
1550 return jni_obj_GattCharacteristic;
1553 jobject CANativeCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data)
1555 if(!CANativeIsEnableBTAdapter(env))
1557 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1561 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateGattCharacteristic");
1562 jstring jni_uuid = (*env)->NewStringUTF(env, IOTIVITY_GATT_TX_UUID);
1563 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1564 if(!jni_obj_GattCharacteristic)
1566 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1570 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/BluetoothGattCharacteristic");
1571 if(!jni_cid_BTGattCharacteristic)
1573 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTGattCharacteristic is null");
1577 OIC_LOG(DEBUG, TAG, "[BLE][Native] set value in Characteristic");
1578 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1580 if(!jni_mid_setValue)
1582 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_setValue is null");
1586 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue, data);
1589 OIC_LOG(DEBUG, TAG, "[BLE][Native] the locally stored value has been set");
1590 return jni_obj_GattCharacteristic;
1594 OIC_LOG(DEBUG, TAG, "[BLE][Native] the locally stored value hasn't been set");
1599 jbyteArray CANativeGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
1601 if(!CANativeIsEnableBTAdapter(env))
1603 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1607 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/BluetoothGattCharacteristic");
1608 if(!jni_cid_BTGattCharacteristic)
1610 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTGattCharacteristic is null");
1614 OIC_LOG(DEBUG, TAG, "[BLE][Native] get value in Characteristic");
1615 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
1617 if(!jni_mid_getValue)
1619 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getValue is null");
1623 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic, jni_mid_getValue);
1624 return jni_obj_data_array;
1628 void CANativeCreateUUIDList()
1630 jboolean isAttached = FALSE;
1632 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
1635 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
1636 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1640 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
1646 // create new object array
1647 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1648 if(!jni_cid_uuid_list)
1650 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_uuid_list is null");
1654 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1, jni_cid_uuid_list, NULL);
1655 if(!jni_obj_uuid_list)
1657 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_uuid_list is null");
1661 // remove previous list and create list again
1662 CANativeRemoveAllDevices(env);
1663 CANativeCreateScanDeviceList(env);
1666 jobject jni_obj_uuid = CANativeGetUUIDObject(env, IOTIVITY_GATT_SERVIE_UUID);
1667 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
1669 gUUIDList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list);
1672 (*g_jvm)->DetachCurrentThread(g_jvm);
1675 void CANativeCreateScanDeviceList(JNIEnv *env)
1677 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateScanDeviceList");
1679 // create new object array
1680 if (gdeviceList == NULL)
1682 OIC_LOG_V(DEBUG, TAG, "Create device list");
1684 gdeviceList = u_arraylist_create();
1688 void CANativeAddScanDeviceToList(JNIEnv *env, jobject device)
1692 OIC_LOG(DEBUG, TAG, "[BLE][Native] device is null");
1698 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdevice_list is null");
1702 jstring jni_remoteAddress = CANativeGetAddressFromBTDevice(env, device);
1703 if(!jni_remoteAddress)
1705 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1709 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1711 if(!CANativeIsDeviceInList(env, remoteAddress)) {
1712 jobject gdevice = (*env)->NewGlobalRef(env, device);
1713 u_arraylist_add(gdeviceList, gdevice);
1714 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1718 jboolean CANativeIsDeviceInList(JNIEnv *env, const char* remoteAddress){
1719 // get address value from device list
1722 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1724 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1727 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1731 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
1734 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1738 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1740 if(!strcmp(remoteAddress, setAddress))
1742 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1751 OIC_LOG_V(DEBUG, TAG, "there are no the device in list. we can add");
1755 void CANativeRemoveAllDevices(JNIEnv *env)
1757 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDevices");
1761 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
1766 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1768 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1771 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1774 (*env)->DeleteGlobalRef(env, jarrayObj);
1777 OICFree(gdeviceList);
1782 void CANativeRemoveDevice(JNIEnv *env, jstring address)
1784 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveDevice");
1788 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
1793 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1795 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1798 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1802 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
1805 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1808 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1809 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
1811 if(!strcmp(setAddress, remoteAddress))
1813 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
1814 (*env)->DeleteGlobalRef(env, jarrayObj);
1816 CAReorderingDeviceList(index);
1820 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
1824 void CAReorderingDeviceList(uint32_t index)
1826 if (index >= gdeviceList->length)
1831 if (index < gdeviceList->length - 1)
1833 memmove(&gdeviceList->data[index], &gdeviceList->data[index + 1],
1834 (gdeviceList->length - index - 1) * sizeof(void *));
1837 gdeviceList->size--;
1838 gdeviceList->length--;
1844 void CANativeCreateGattObjList(JNIEnv *env)
1846 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateGattObjList");
1848 // create new object array
1849 if (gGattObjectList == NULL)
1851 OIC_LOG_V(DEBUG, TAG, "Create Gatt object list");
1853 gGattObjectList = u_arraylist_create();
1857 void CANativeAddGattobjToList(JNIEnv *env, jobject gatt)
1859 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeAddGattobjToList");
1863 OIC_LOG(DEBUG, TAG, "[BLE][Native] gatt is null");
1867 if(!gGattObjectList)
1869 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1873 jstring jni_remoteAddress = CANativeGetAddressFromGattObj(env, gatt);
1874 if(!jni_remoteAddress)
1876 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1880 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1882 if(!CANativeIsGattObjInList(env, remoteAddress))
1884 jobject gGatt = (*env)->NewGlobalRef(env, gatt);
1885 u_arraylist_add(gGattObjectList, gGatt);
1886 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1890 jboolean CANativeIsGattObjInList(JNIEnv *env, const char* remoteAddress)
1892 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsGattObjInList");
1895 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1898 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1901 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1905 jstring jni_setAddress = CANativeGetAddressFromGattObj(env, jarrayObj);
1908 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1912 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1914 if(!strcmp(remoteAddress, setAddress))
1916 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1925 OIC_LOG_V(DEBUG, TAG, "there are no the gatt obejct in list. we can add");
1929 void CANativeRemoveAllGattObjsList(JNIEnv *env)
1931 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllGattObjsList");
1933 if(!gGattObjectList)
1935 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1940 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1942 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1945 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1948 (*env)->DeleteGlobalRef(env, jarrayObj);
1951 OICFree(gGattObjectList);
1952 gGattObjectList = NULL;
1956 void CANativeLEDisconnectAll(JNIEnv *env)
1958 OIC_LOG_V(DEBUG, TAG, "CANativeLEDisconnectAll");
1960 if(!gGattObjectList)
1962 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1967 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1969 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1972 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1975 CANativeLEDisconnect(env, jarrayObj);
1978 OICFree(gGattObjectList);
1979 gGattObjectList = NULL;
1983 void CANativeRemoveGattObj(JNIEnv *env, jobject gatt)
1985 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveGattObj");
1987 if(!gGattObjectList)
1989 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1994 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1996 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1999 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
2003 jstring jni_setAddress = CANativeGetAddressFromGattObj(env, jarrayObj);
2006 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
2009 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2011 jstring jni_remoteAddress = CANativeGetAddressFromGattObj(env, gatt);
2012 if(!jni_remoteAddress)
2014 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
2017 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2019 if(!strcmp(setAddress, remoteAddress))
2021 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
2022 (*env)->DeleteGlobalRef(env, jarrayObj);
2024 CAReorderingGattList(index);
2028 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
2032 void CAReorderingGattList(uint32_t index)
2034 if (index >= gGattObjectList->length)
2039 if (index < gGattObjectList->length - 1)
2041 memmove(&gGattObjectList->data[index], &gGattObjectList->data[index + 1],
2042 (gGattObjectList->length - index - 1) * sizeof(void *));
2045 gGattObjectList->size--;
2046 gGattObjectList->length--;
2049 void CANativeupdateSendCnt(JNIEnv *env)
2052 u_mutex_lock(gThreadMutex);
2056 if(gTargetCnt <= gCurrentSentCnt)
2059 gCurrentSentCnt = 0;
2063 (*env)->DeleteGlobalRef(env, gSendBuffer);
2066 // notity the thread
2067 u_cond_signal(gThreadCond);
2068 gIsFinishSendData = TRUE;
2069 OIC_LOG(DEBUG, TAG, "set signal for send data");
2072 u_mutex_unlock(gThreadMutex);