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 if (*address == NULL)
576 memcpy(*address, localAddress, strlen(localAddress));
579 OIC_LOG_V(DEBUG, TAG, "Local Address : %s", *address);
581 (*g_jvm)->DetachCurrentThread(g_jvm);
584 int32_t CALESendUnicastMessageImpl(const char* address, const char* data, uint32_t dataLen)
586 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastMessageImpl, address: %s, data: %s", address, data);
588 jboolean isAttached = FALSE;
590 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
593 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
594 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
597 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
603 OIC_LOG(DEBUG, TAG, "[BLE][Native] set byteArray for data");
606 (*env)->DeleteGlobalRef(env, gSendBuffer);
608 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
609 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*)data);
610 gSendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
612 // connect to gatt server
613 CANativeLEStopScanImpl(env, gLeScanCallback);
616 jobject jni_obj_bluetoothDevice = NULL;
617 if(gContext && gdeviceList)
620 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
622 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
625 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
629 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
632 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
635 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
637 if(!strcmp(setAddress, address))
639 jni_obj_bluetoothDevice = jarrayObj;
644 if(jni_obj_bluetoothDevice)
646 jboolean autoConnect = FALSE;
647 CANativeLEConnect(env, jni_obj_bluetoothDevice, gContext, autoConnect, gLeGattCallback);
652 (*g_jvm)->DetachCurrentThread(g_jvm);
657 int32_t CALESendMulticastMessageImpl(JNIEnv *env, const char* data, uint32_t dataLen)
659 OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s, %d", data, dataLen);
663 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
666 OIC_LOG(DEBUG, TAG, "set wait");
669 gIsFinishSendData = FALSE;
671 OIC_LOG(DEBUG, TAG, "[BLE][Native] set byteArray for data");
674 (*env)->DeleteGlobalRef(env, gSendBuffer);
676 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
677 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*)data);
678 gSendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
680 // connect to gatt server
681 CANativeLEStopScanImpl(env, gLeScanCallback);
685 CANativeRemoveAllGattObjsList(env);
686 CANativeCreateGattObjList(env);
688 gTargetCnt = u_arraylist_length(gdeviceList);
692 while (index < u_arraylist_length(gdeviceList)) {
693 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
696 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
700 if(0 <= CANativeLEConnect(env, jarrayObj, gContext, FALSE, gLeGattCallback))
702 // connection failure
708 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
709 if(!gIsFinishSendData)
711 u_mutex_lock(gThreadMutex);
712 u_cond_wait(gThreadCond, gThreadMutex);
713 OIC_LOG(DEBUG, TAG, "unset wait");
714 u_mutex_unlock(gThreadMutex);
717 // start LE Scan again
719 CANativeLEStartScan();
728 jstring CANativeGetLocalDeviceAddress(JNIEnv* env)
730 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
731 if(!jni_cid_BTAdapter)
733 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_cid_BTAdapter is null");
737 jmethodID jni_mid_getDefaultAdapter =
738 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
739 if(!jni_mid_getDefaultAdapter)
741 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_mid_getDefaultAdapter is null");
745 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getAddress", METHODID_STRINGNONPARAM);
746 if(!jni_mid_getAddress)
748 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_mid_getAddress is null");
752 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
753 if(!jni_obj_BTAdapter)
755 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_obj_BTAdapter is null");
759 jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getAddress);
762 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_str_address is null");
766 return jni_str_address;
769 jobjectArray CANativeBondedDevices(JNIEnv *env)
771 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
772 if(!jni_cid_BTAdapter)
774 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_cid_BTAdapter is null");
778 jmethodID jni_mid_getDefaultAdapter =
779 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
781 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
782 if(!jni_obj_BTAdapter)
784 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: bluetooth adapter is null");
788 // Get a list of currently paired devices
789 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
790 "getBondedDevices", "()Ljava/util/Set;");
791 if(!jni_mid_getBondedDevices)
793 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
797 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getBondedDevices);
798 if(!jni_obj_setPairedDevices)
800 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_obj_setPairedDevices is null");
804 // Convert the set to an object array
805 // object[] array = Set<BluetoothDevice>.toArray();
806 jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
807 jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray", "()[Ljava/lang/Object;");
811 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_mid_toArray is null");
815 jobjectArray jni_arrayPairedDevices = (jobjectArray)((*env)->CallObjectMethod(env,
816 jni_obj_setPairedDevices, jni_mid_toArray));
817 if(!jni_arrayPairedDevices)
819 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_arrayPairedDevices is null");
823 return jni_arrayPairedDevices;
826 jint CANativeGetBTStateOnInfo(JNIEnv *env)
828 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
829 if(!jni_cid_BTAdapter)
831 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
835 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
836 if (jni_fid_stateon == 0)
838 OIC_LOG(DEBUG, TAG, "[BLE][Native] get_field_state is 0");
841 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
843 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] bluetooth STATE_ON state integer value : %d", jni_int_val);
848 jboolean CANativeIsEnableBTAdapter(JNIEnv *env)
850 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
851 if(!jni_cid_BTAdapter)
853 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
857 jmethodID jni_mid_getDefaultAdapter =
858 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
859 if(!jni_mid_getDefaultAdapter)
861 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getDefaultAdapter is null");
865 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
866 if(!jni_obj_BTAdapter)
868 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_BTAdapter is null");
873 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled",
875 if(!jni_mid_isEnable)
877 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_isEnable is null");
881 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
882 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] adapter state is %d", jni_isEnable);
887 jstring CANativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
889 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
890 if(!jni_cid_device_list)
892 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_device_list is null");
896 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
897 "()Ljava/lang/String;");
898 if(!jni_mid_getAddress)
900 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getAddress is null");
904 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice, jni_mid_getAddress);
907 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_address is null");
913 jstring CANativeGetAddressFromGattObj(JNIEnv *env, jobject gatt)
917 OIC_LOG(DEBUG, TAG, "[BLE][Native] gatt is null");
921 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
922 if(!jni_cid_gattdevice_list)
924 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_gattdevice_list is null");
928 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
929 "()Landroid/bluetooth/BluetoothDevice;");
930 if(!jni_mid_getDevice)
932 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getDevice is null");
936 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
939 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_device is null");
943 jstring jni_address = CANativeGetAddressFromBTDevice(env, jni_obj_device);
946 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_address is null");
956 void CANativeGattClose(JNIEnv *env, jobject bluetoothGatt)
959 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT CLOSE");
961 // get BluetoothGatt class
962 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
963 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
964 if(!jni_cid_BluetoothGatt)
966 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
970 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close","()V");
971 if(!jni_mid_closeGatt)
973 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_closeGatt is null");
977 // call disconnect gatt method
978 OIC_LOG(DEBUG, TAG, "[BLE][Native] request close Gatt");
979 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
982 void CANativeLEStartScan()
986 OIC_LOG_V(DEBUG, TAG, "server is not started yet..scan will be passed");
990 jboolean isAttached = FALSE;
992 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
995 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
997 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1000 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
1006 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeLEStartScan");
1008 // scan gatt server with UUID
1009 if(gLeScanCallback && gUUIDList)
1011 CANativeLEStartScanWithUUIDImpl(env, gUUIDList, gLeScanCallback);
1015 (*g_jvm)->DetachCurrentThread(g_jvm);
1019 void CANativeLEStartScanImpl(JNIEnv *env, jobject callback)
1021 if(!CANativeIsEnableBTAdapter(env))
1023 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1027 // get default bt adapter class
1028 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
1029 if(!jni_cid_BTAdapter)
1031 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
1035 // get remote bt adapter method
1036 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
1037 if(!jni_mid_getDefaultAdapter)
1039 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
1043 // get start le scan method
1044 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1045 "(Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)Z");
1046 if(!jni_mid_startLeScan)
1048 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan: jni_mid_startLeScan is null");
1052 // gat bt adapter object
1053 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1054 if(!jni_obj_BTAdapter)
1056 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
1060 // call start le scan method
1061 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_startLeScan, callback);
1062 if(!jni_obj_startLeScan)
1064 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan is failed");
1069 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan is started");
1073 jobject CANativeGetUUIDObject(JNIEnv *env, const char* uuid)
1076 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1079 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_uuid is null");
1083 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;");
1084 if(!jni_mid_fromString)
1086 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_fromString is null");
1090 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1091 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString, jni_uuid);
1094 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_uuid is null");
1098 return jni_obj_uuid;
1101 void CANativeLEStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
1103 // get default bt adapter class
1104 OIC_LOG(DEBUG, TAG, "[BLE][Native] get default bt adapter class");
1106 if(!CANativeIsEnableBTAdapter(env))
1108 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1112 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
1113 if(!jni_cid_BTAdapter)
1115 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
1119 // get remote bt adapter method
1120 OIC_LOG(DEBUG, TAG, "[BLE][Native] get remote bt adapter method");
1121 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
1122 if(!jni_mid_getDefaultAdapter)
1124 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
1128 // get start le scan method
1129 OIC_LOG(DEBUG, TAG, "[BLE][Native] get start le scan method");
1130 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1131 "([Ljava/util/UUID;Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)Z");
1132 if(!jni_mid_startLeScan)
1134 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan: jni_mid_startLeScan is null");
1138 // get bt adapter object
1139 OIC_LOG(DEBUG, TAG, "[BLE][Native] get bt adapter object");
1140 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1141 if(!jni_obj_BTAdapter)
1143 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
1147 // call start le scan method
1148 OIC_LOG(DEBUG, TAG, "[BLE][Native] call start le scan service method");
1149 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_startLeScan, uuids, callback);
1150 if(!jni_obj_startLeScan)
1152 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan With UUID is failed");
1157 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan With UUID is started");
1161 void CANativeLEStopScan()
1163 jboolean isAttached = FALSE;
1165 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
1168 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
1169 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1172 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
1178 CANativeLEStopScanImpl(env, gLeScanCallback);
1181 (*g_jvm)->DetachCurrentThread(g_jvm);
1185 void CANativeLEStopScanImpl(JNIEnv *env, jobject callback)
1187 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeLEStopScan");
1189 if(!CANativeIsEnableBTAdapter(env))
1191 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1195 // get default bt adapter class
1196 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
1197 if(!jni_cid_BTAdapter)
1199 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
1203 // get remote bt adapter method
1204 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
1205 if(!jni_mid_getDefaultAdapter)
1207 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
1211 // get start le scan method
1212 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1213 "(Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)V");
1214 if(!jni_mid_stopLeScan)
1216 OIC_LOG(DEBUG, TAG, "[BLE][Native] stopLeScan: jni_mid_stopLeScan is null");
1220 // gat bt adapter object
1221 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1222 if(!jni_obj_BTAdapter)
1224 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_BTAdapter is null");
1228 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to stop LE Scan");
1229 // call start le scan method
1230 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1233 int32_t CANativeLEConnect(JNIEnv *env, jobject bluetoothDevice, jobject context,
1234 jboolean autoconnect, jobject callback)
1236 if(!CANativeIsEnableBTAdapter(env))
1238 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1242 jstring jni_address = CANativeGetAddressFromBTDevice(env, bluetoothDevice);
1243 const char * addr = (*env)->GetStringUTFChars(env, jni_address, NULL);
1244 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] request connectGatt to %s", addr);
1248 // get BluetoothDevice class
1249 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothDevice class");
1250 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1251 if(!jni_cid_BluetoothDevice)
1253 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_cid_BluetoothDevice is null");
1257 // get connectGatt method
1258 OIC_LOG(DEBUG, TAG, "[BLE][Native] get connectGatt method");
1259 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice,
1261 "(Landroid/content/Context;ZLandroid/bluetooth/BluetoothGattCallback;)Landroid/bluetooth/BluetoothGatt;");
1262 if(!jni_mid_connectGatt)
1264 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_mid_connectGatt is null");
1268 OIC_LOG(DEBUG, TAG, "[BLE][Native] Call object method - connectGatt");
1269 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice,
1270 jni_mid_connectGatt, NULL, autoconnect, callback);
1271 if(!jni_obj_connectGatt)
1273 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - connectGatt was failed.obj will be removed");
1274 CANativeRemoveDevice(env, jni_address);
1275 CANativeupdateSendCnt(env);
1280 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - connecting..");
1285 void CANativeLEDisconnect(JNIEnv *env, jobject bluetoothGatt)
1287 if(!CANativeIsEnableBTAdapter(env))
1289 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1294 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT DISCONNECT");
1296 // get BluetoothGatt class
1297 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt classjobject bluetoothGatt");
1298 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1299 if(!jni_cid_BluetoothGatt)
1301 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1305 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt disconnect method");
1306 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1307 "disconnect","()V");
1308 if(!jni_mid_disconnectGatt)
1310 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_disconnectGatt is null");
1314 // call disconnect gatt method
1315 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request disconnectGatt");
1316 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1320 void CANativeLEDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1322 if(!CANativeIsEnableBTAdapter(env))
1324 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1328 // GATT SERVICE DISCOVERY
1329 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT SERVICE DISCOVERY");
1331 // get BluetoothGatt class
1332 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
1333 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1334 if(!jni_cid_BluetoothGatt)
1336 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1340 OIC_LOG(DEBUG, TAG, "[BLE][Native] discovery gatt services method");
1341 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1342 "discoverServices","()Z");
1343 if(!jni_mid_discoverServices)
1345 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_discoverServices is null");
1348 // call disconnect gatt method
1349 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request discovery gatt services");
1350 (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1353 jboolean CANativeLESendData(JNIEnv *env, jobject bluetoothGatt, jobject gattCharacteristic)
1355 if(!CANativeIsEnableBTAdapter(env))
1357 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1361 // WRITE GATT CHARACTERISTIC
1362 OIC_LOG(DEBUG, TAG, "[BLE][Native] WRITE GATT CHARACTERISTIC");
1364 // get BluetoothGatt class
1365 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
1366 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1367 if(!jni_cid_BluetoothGatt)
1369 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1373 OIC_LOG(DEBUG, TAG, "[BLE][Native] write characteristic method");
1374 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1375 "writeCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
1376 if(!jni_mid_writeCharacteristic)
1378 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_writeCharacteristic is null");
1382 // call disconnect gatt method
1383 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to write gatt characteristic");
1384 jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeCharacteristic, gattCharacteristic);
1387 OIC_LOG(DEBUG, TAG, "[BLE][Native] writeCharacteristic is success");
1391 OIC_LOG(DEBUG, TAG, "[BLE][Native] writeCharacteristic is failed");
1397 void CANativeReadCharacteristic(JNIEnv *env, jobject bluetoothGatt)
1400 if(!CANativeIsEnableBTAdapter(env))
1402 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1406 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1407 if(!jni_cid_BluetoothGatt)
1409 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1413 jstring jni_uuid = (*env)->NewStringUTF(env, IOTIVITY_GATT_RX_UUID);
1414 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1415 if(!jni_obj_GattCharacteristic)
1417 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1421 OIC_LOG(DEBUG, TAG, "[BLE][Native] read characteristic method");
1422 jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "readCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
1423 if(!jni_mid_readCharacteristic)
1425 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_readCharacteristic is null");
1429 // call disconnect gatt method
1430 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - request to read gatt characteristic");
1431 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic, jni_obj_GattCharacteristic);
1434 OIC_LOG(DEBUG, TAG, "[BLE][Native] readCharacteristic is success");
1438 OIC_LOG(DEBUG, TAG, "[BLE][Native] readCharacteristic is failed");
1442 jboolean CANativeSetCharacteristicNoti(JNIEnv *env, jobject bluetoothGatt, const char* uuid)
1444 if(!CANativeIsEnableBTAdapter(env))
1446 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1450 // get BluetoothGatt class
1451 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeSetCharacteristicNoti");
1452 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1453 if(!jni_cid_BluetoothGatt)
1455 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1459 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1460 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1461 if(!jni_obj_GattCharacteristic)
1463 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1467 // set Characteristic Notification
1468 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "setCharacteristicNotification",
1469 "(Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z");
1470 if(!jni_mid_setNotification)
1472 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getService is null");
1476 jboolean enable = TRUE;
1477 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification, jni_obj_GattCharacteristic, enable);
1480 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - setCharacteristicNotification is success");
1485 OIC_LOG(DEBUG, TAG, "[BLE][Native] CALL API - setCharacteristicNotification is failed");
1490 jobject CANativeGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1492 if(!CANativeIsEnableBTAdapter(env))
1494 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1498 // get BluetoothGatt class
1499 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeGetGattService");
1500 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1501 if(!jni_cid_BluetoothGatt)
1503 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1507 jmethodID jni_mid_getService = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "getService",
1508 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1509 if(!jni_mid_getService)
1511 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getService is null");
1515 jobject jni_obj_service_uuid = CANativeGetUUIDObject(env, IOTIVITY_GATT_SERVIE_UUID);
1516 if(!jni_obj_service_uuid)
1518 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_service_uuid is null");
1522 // get bluetooth gatt service
1523 OIC_LOG(DEBUG, TAG, "[BLE][Native] request to get service");
1524 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService, jni_obj_service_uuid);
1526 // get bluetooth gatt service class
1527 jclass jni_cid_BluetoothGattService = (*env)->FindClass(env, "android/bluetooth/BluetoothGattService");
1528 if(!jni_cid_BluetoothGattService)
1530 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGattService is null");
1534 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt getCharacteristic method");
1535 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService, "getCharacteristic",
1536 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattCharacteristic;");
1537 if(!jni_mid_getCharacteristic)
1539 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getCharacteristic is null");
1543 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1544 jobject jni_obj_tx_uuid = CANativeGetUUIDObject(env, uuid);
1545 if(!jni_obj_tx_uuid)
1547 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_tx_uuid is null");
1551 OIC_LOG(DEBUG, TAG, "[BLE][Native] request to get Characteristic");
1552 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService, jni_mid_getCharacteristic, jni_obj_tx_uuid);
1554 return jni_obj_GattCharacteristic;
1557 jobject CANativeCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data)
1559 if(!CANativeIsEnableBTAdapter(env))
1561 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1565 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateGattCharacteristic");
1566 jstring jni_uuid = (*env)->NewStringUTF(env, IOTIVITY_GATT_TX_UUID);
1567 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, jni_uuid);
1568 if(!jni_obj_GattCharacteristic)
1570 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1574 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/BluetoothGattCharacteristic");
1575 if(!jni_cid_BTGattCharacteristic)
1577 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTGattCharacteristic is null");
1581 OIC_LOG(DEBUG, TAG, "[BLE][Native] set value in Characteristic");
1582 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1584 if(!jni_mid_setValue)
1586 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_setValue is null");
1590 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue, data);
1593 OIC_LOG(DEBUG, TAG, "[BLE][Native] the locally stored value has been set");
1594 return jni_obj_GattCharacteristic;
1598 OIC_LOG(DEBUG, TAG, "[BLE][Native] the locally stored value hasn't been set");
1603 jbyteArray CANativeGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
1605 if(!CANativeIsEnableBTAdapter(env))
1607 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1611 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/BluetoothGattCharacteristic");
1612 if(!jni_cid_BTGattCharacteristic)
1614 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTGattCharacteristic is null");
1618 OIC_LOG(DEBUG, TAG, "[BLE][Native] get value in Characteristic");
1619 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
1621 if(!jni_mid_getValue)
1623 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getValue is null");
1627 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic, jni_mid_getValue);
1628 return jni_obj_data_array;
1632 void CANativeCreateUUIDList()
1634 jboolean isAttached = FALSE;
1636 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
1639 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
1640 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1644 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
1650 // create new object array
1651 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1652 if(!jni_cid_uuid_list)
1654 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_uuid_list is null");
1658 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1, jni_cid_uuid_list, NULL);
1659 if(!jni_obj_uuid_list)
1661 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_uuid_list is null");
1665 // remove previous list and create list again
1666 CANativeRemoveAllDevices(env);
1667 CANativeCreateScanDeviceList(env);
1670 jobject jni_obj_uuid = CANativeGetUUIDObject(env, IOTIVITY_GATT_SERVIE_UUID);
1671 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
1673 gUUIDList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list);
1676 (*g_jvm)->DetachCurrentThread(g_jvm);
1679 void CANativeCreateScanDeviceList(JNIEnv *env)
1681 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateScanDeviceList");
1683 // create new object array
1684 if (gdeviceList == NULL)
1686 OIC_LOG_V(DEBUG, TAG, "Create device list");
1688 gdeviceList = u_arraylist_create();
1692 void CANativeAddScanDeviceToList(JNIEnv *env, jobject device)
1696 OIC_LOG(DEBUG, TAG, "[BLE][Native] device is null");
1702 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdevice_list is null");
1706 jstring jni_remoteAddress = CANativeGetAddressFromBTDevice(env, device);
1707 if(!jni_remoteAddress)
1709 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1713 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1715 if(!CANativeIsDeviceInList(env, remoteAddress)) {
1716 jobject gdevice = (*env)->NewGlobalRef(env, device);
1717 u_arraylist_add(gdeviceList, gdevice);
1718 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1722 jboolean CANativeIsDeviceInList(JNIEnv *env, const char* remoteAddress){
1723 // get address value from device list
1726 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1728 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1731 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1735 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
1738 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1742 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1744 if(!strcmp(remoteAddress, setAddress))
1746 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1755 OIC_LOG_V(DEBUG, TAG, "there are no the device in list. we can add");
1759 void CANativeRemoveAllDevices(JNIEnv *env)
1761 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDevices");
1765 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
1770 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1772 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1775 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1778 (*env)->DeleteGlobalRef(env, jarrayObj);
1781 OICFree(gdeviceList);
1786 void CANativeRemoveDevice(JNIEnv *env, jstring address)
1788 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveDevice");
1792 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
1797 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1799 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1802 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1806 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
1809 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1812 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1813 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
1815 if(!strcmp(setAddress, remoteAddress))
1817 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
1818 (*env)->DeleteGlobalRef(env, jarrayObj);
1820 CAReorderingDeviceList(index);
1824 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
1828 void CAReorderingDeviceList(uint32_t index)
1830 if (index >= gdeviceList->length)
1835 if (index < gdeviceList->length - 1)
1837 memmove(&gdeviceList->data[index], &gdeviceList->data[index + 1],
1838 (gdeviceList->length - index - 1) * sizeof(void *));
1841 gdeviceList->size--;
1842 gdeviceList->length--;
1848 void CANativeCreateGattObjList(JNIEnv *env)
1850 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateGattObjList");
1852 // create new object array
1853 if (gGattObjectList == NULL)
1855 OIC_LOG_V(DEBUG, TAG, "Create Gatt object list");
1857 gGattObjectList = u_arraylist_create();
1861 void CANativeAddGattobjToList(JNIEnv *env, jobject gatt)
1863 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeAddGattobjToList");
1867 OIC_LOG(DEBUG, TAG, "[BLE][Native] gatt is null");
1871 if(!gGattObjectList)
1873 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1877 jstring jni_remoteAddress = CANativeGetAddressFromGattObj(env, gatt);
1878 if(!jni_remoteAddress)
1880 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1884 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1886 if(!CANativeIsGattObjInList(env, remoteAddress))
1888 jobject gGatt = (*env)->NewGlobalRef(env, gatt);
1889 u_arraylist_add(gGattObjectList, gGatt);
1890 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1894 jboolean CANativeIsGattObjInList(JNIEnv *env, const char* remoteAddress)
1896 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsGattObjInList");
1899 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1902 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1905 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1909 jstring jni_setAddress = CANativeGetAddressFromGattObj(env, jarrayObj);
1912 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1916 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1918 if(!strcmp(remoteAddress, setAddress))
1920 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1929 OIC_LOG_V(DEBUG, TAG, "there are no the gatt obejct in list. we can add");
1933 void CANativeRemoveAllGattObjsList(JNIEnv *env)
1935 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllGattObjsList");
1937 if(!gGattObjectList)
1939 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1944 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1946 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1949 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1952 (*env)->DeleteGlobalRef(env, jarrayObj);
1955 OICFree(gGattObjectList);
1956 gGattObjectList = NULL;
1960 void CANativeLEDisconnectAll(JNIEnv *env)
1962 OIC_LOG_V(DEBUG, TAG, "CANativeLEDisconnectAll");
1964 if(!gGattObjectList)
1966 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1971 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1973 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1976 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1979 CANativeLEDisconnect(env, jarrayObj);
1982 OICFree(gGattObjectList);
1983 gGattObjectList = NULL;
1987 void CANativeRemoveGattObj(JNIEnv *env, jobject gatt)
1989 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveGattObj");
1991 if(!gGattObjectList)
1993 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1998 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
2000 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
2003 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
2007 jstring jni_setAddress = CANativeGetAddressFromGattObj(env, jarrayObj);
2010 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
2013 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2015 jstring jni_remoteAddress = CANativeGetAddressFromGattObj(env, gatt);
2016 if(!jni_remoteAddress)
2018 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
2021 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2023 if(!strcmp(setAddress, remoteAddress))
2025 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
2026 (*env)->DeleteGlobalRef(env, jarrayObj);
2028 CAReorderingGattList(index);
2032 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
2036 void CAReorderingGattList(uint32_t index)
2038 if (index >= gGattObjectList->length)
2043 if (index < gGattObjectList->length - 1)
2045 memmove(&gGattObjectList->data[index], &gGattObjectList->data[index + 1],
2046 (gGattObjectList->length - index - 1) * sizeof(void *));
2049 gGattObjectList->size--;
2050 gGattObjectList->length--;
2053 void CANativeupdateSendCnt(JNIEnv *env)
2056 u_mutex_lock(gThreadMutex);
2060 if(gTargetCnt <= gCurrentSentCnt)
2063 gCurrentSentCnt = 0;
2067 (*env)->DeleteGlobalRef(env, gSendBuffer);
2070 // notity the thread
2071 u_cond_signal(gThreadCond);
2072 gIsFinishSendData = TRUE;
2073 OIC_LOG(DEBUG, TAG, "set signal for send data");
2076 u_mutex_unlock(gThreadMutex);