6 #include "oic_malloc.h"
7 #include "uthreadpool.h" /* for thread pool */
8 #include "uarraylist.h"
9 #include "com_iotivity_jar_CALeInterface.h"
13 #define METHODID_OBJECTNONPARAM "()Landroid/bluetooth/BluetoothAdapter;"
14 #define METHODID_INTNONPARAM "()I"
15 #define METHODID_STRINGNONPARAM "()Ljava/lang/String;"
16 #define METHODID_OBJECT_STRINGUUIDPARAM "(Ljava/lang/String;Ljava/util/UUID;)Ljava/lang/Object;"
17 #define METHODID_ONRESPONSE_PARAM "(Ljava/lang/String;)V"
18 #define CLASSPATH_BT_ADPATER "android/bluetooth/BluetoothAdapter"
19 #define CLASSPATH_BT_UUID "java/util/UUID"
21 static const uint32_t STATE_CONNECTED = 2;
22 static const uint32_t STATE_DISCONNECTED = 0;
23 static const uint32_t GATT_SUCCESS = 0;
26 static u_arraylist_t *gdeviceList = NULL;
27 static u_arraylist_t *gGattObjectList = NULL;
28 static CAPacketReceiveCallback gPacketReceiveCallback = NULL;
29 static u_thread_pool_t gThreadPoolHandle = NULL;
30 static jobject gLeScanCallback;
31 static jobject gLeGattCallback;
32 static jobject gContext;
33 static jboolean gIsStartServer;
35 ////////////////////////////////////////////////////////////////////////////////////////////////////
36 void CALeCreateJniInterfaceObject()
38 OIC_LOG_V(DEBUG, TAG, "CALeCreateJniInterfaceObject");
40 jboolean isAttached = FALSE;
42 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
45 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
46 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
50 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
56 jclass LeJniInterface = (*env)->FindClass(env, "com/iotivity/jar/CALeInterface");
59 OIC_LOG_V(DEBUG, TAG, "Could not get CALeInterface class");
63 jmethodID LeInterfaceConstructorMethod =
64 (*env)->GetMethodID(env, LeJniInterface, "<init>", "()V");
65 if (!LeInterfaceConstructorMethod)
67 OIC_LOG_V(DEBUG, TAG, "Could not get CALeInterface constructor method");
71 jobject jni_instance = (*env)->NewObject(env, LeJniInterface, LeInterfaceConstructorMethod);
72 gContext = (*env)->NewGlobalRef(env, jni_instance);
73 OIC_LOG_V(DEBUG, TAG, "Create CALeInterface instance");
76 (*g_jvm)->DetachCurrentThread(g_jvm);
79 JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
81 OIC_LOG_V(DEBUG, TAG, "JNI_OnLoad in calecore");
84 if((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
88 g_jvm = jvm; /* cache the JavaVM pointer */
90 //JVM required for WifiCore to work with JNI interface
93 return JNI_VERSION_1_6;
96 void JNI_OnUnload(JavaVM *jvm, void *reserved)
98 OIC_LOG_V(DEBUG, TAG, "JNI_OnUnload in calecore");
101 if((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
109 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CARegisterLeScanCallback(JNIEnv *env,
110 jobject obj, jobject callback)
112 OIC_LOG_V(DEBUG, TAG, "CARegisterLeScanCallback");
114 gLeScanCallback = (*env)->NewGlobalRef(env, callback);
117 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CARegisterLeGattCallback(JNIEnv *env,
118 jobject obj, jobject callback)
120 OIC_LOG_V(DEBUG, TAG, "CARegisterLeGattCallback");
122 gLeGattCallback = (*env)->NewGlobalRef(env, callback);
125 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeScanCallback (JNIEnv *env,
126 jobject obj, jobject device, jint rssi, jbyteArray scanRecord)
128 CANativeAddScanDeviceToList(env, device);
132 * Class: com_iotivity_jar_CALeInterface
133 * Method: CALeGattConnectionStateChangeCallback
134 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
136 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattConnectionStateChangeCallback
137 (JNIEnv *env, jobject obj, jobject gatt, jint status, jint newstate)
139 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status, newstate);
141 if(GATT_SUCCESS == status && STATE_CONNECTED == newstate)
144 CANativeAddGattobjToList(env, gatt);
145 CANativeLEDiscoverServices(env, gatt);
148 else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate)
151 CANativeRemoveGattObj(env, gatt);
157 * Class: com_iotivity_jar_CALeInterface
158 * Method: CALeGattServicesDiscoveredCallback
159 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
161 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattServicesDiscoveredCallback
162 (JNIEnv *env, jobject obj, jobject gatt, jint status)
164 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
168 jboolean ret = CANativeSetCharacteristicNoti(env, gatt);
171 jstring data = (*env)->NewStringUTF(env, "HelloWorld");
172 jobject jni_obj_character = CANativeCreateGattCharacteristic(env, gatt, data);
173 if(jni_obj_character)
175 CANativeLESendData(env, gatt, jni_obj_character);
182 * Class: com_iotivity_jar_CALeInterface
183 * Method: CALeGattCharacteristicReadCallback
184 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
186 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicReadCallback
187 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jstring data, jint status)
189 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicReadCallback - status : %d", status);
191 const char* readData = (*env)->GetStringUTFChars(env, data, NULL);
192 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicReadCallback - read data : %s", readData);
196 * Class: com_iotivity_jar_CALeInterface
197 * Method: CALeGattCharacteristicWritjclasseCallback
198 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
200 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicWriteCallback
201 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jstring data, jint status)
203 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
205 const char* writeData = (*env)->GetStringUTFChars(env, data, NULL);
206 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", writeData);
208 // jobjectArray jni_obj_data_array = CANativeGetValueFromCharacteristic(env, characteristic);
209 // if(!jni_obj_data_array)
211 // OIC_LOG_V(DEBUG, TAG, "jni_obj_data_array is null");
217 * Class: com_iotivity_jar_CALeInterface
218 * Method: CALeGattCharacteristicChangedCallback
219 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
221 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattCharacteristicChangedCallback
222 (JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jstring data)
224 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
226 const char* changedData = (*env)->GetStringUTFChars(env, data, NULL);
227 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data : %s", changedData);
229 CANativeLEDisconnect(env, gatt);
233 * Class: com_iotivity_jar_CALeInterface
234 * Method: CALeGattDescriptorReadCallback
235 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
237 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattDescriptorReadCallback
238 (JNIEnv *env, jobject obj, jobject gatt, jobject descriptor, jint status)
240 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorReadCallback - status %d: ", status);
244 * Class: com_iotivity_jar_CALeInterface
245 * Method: CALeGattDescriptorWriteCallback
246 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
248 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattDescriptorWriteCallback
249 (JNIEnv *env, jobject obj, jobject gatt, jobject descriptor, jint status)
251 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
255 * Class: com_iotivity_jar_CALeInterface
256 * Method: CALeGattReliableWriteCompletedCallback
257 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
259 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattReliableWriteCompletedCallback
260 (JNIEnv *env, jobject obj, jobject gatt, jint status)
262 OIC_LOG_V(DEBUG, TAG, "CALeGattReliableWriteCompletedCallback - status %d: ", status);
266 * Class: com_iotivity_jar_CALeInterface
267 * Method: CALeGattReadRemoteRssiCallback
268 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
270 JNIEXPORT void JNICALL Java_com_iotivity_jar_CALeInterface_CALeGattReadRemoteRssiCallback
271 (JNIEnv *env, jobject obj, jobject gatt, jint rssi, jint status)
273 OIC_LOG_V(DEBUG, TAG, "CALeGattReadRemoteRssiCallback - rssi %d, status %d: ", rssi, status);
277 void CALEInitialize(u_thread_pool_t handle)
279 OIC_LOG(DEBUG, TAG, "CALEInitialize");
281 gThreadPoolHandle = handle;
283 CALeCreateJniInterfaceObject(); /* create java CALeInterface instance*/
288 OIC_LOG(DEBUG, TAG, "CALETerminate");
290 jboolean isAttached = FALSE;
292 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
295 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
296 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
300 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
306 CANativeLEDisconnectAll(env);
310 CANativeLEStopScanImpl(env, gLeScanCallback);
315 (*env)->DeleteGlobalRef(env, gLeScanCallback);
320 (*env)->DeleteGlobalRef(env, gLeGattCallback);
325 (*env)->DeleteGlobalRef(env, gContext);
328 CANativeRemoveAllDevices(env);
329 CANativeRemoveAllGattObjsList(env);
330 gIsStartServer = FALSE;
333 (*g_jvm)->DetachCurrentThread(g_jvm);
337 int32_t CALESendUnicastMessage(const char* address, const char* data, uint32_t dataLen)
339 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastMessage(%s, %s)", address, data);
341 CALESendUnicastMessageImpl(address, data, dataLen);
345 int32_t CALESendMulticastMessage(const char* data, uint32_t dataLen)
347 OIC_LOG_V(DEBUG, TAG, "CALESendMulticastMessage(%s)", data);
349 jboolean isAttached = FALSE;
351 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
354 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
355 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
359 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
365 CALESendMulticastMessageImpl(env, data, dataLen);
368 (*g_jvm)->DetachCurrentThread(g_jvm);
373 int32_t CALEStartUnicastServer(const char* address)
375 OIC_LOG_V(DEBUG, TAG, "CALEStartUnicastServer(%s)", address);
380 int32_t CALEStartMulticastServer()
382 OIC_LOG_V(DEBUG, TAG, "CALEStartMulticastServer");
386 OIC_LOG_V(DEBUG, TAG, "server is already started..it will be skipped");
390 jboolean isAttached = FALSE;
392 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
395 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
396 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
400 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
406 gIsStartServer = TRUE;
407 CANativeLEStartScan();
410 (*g_jvm)->DetachCurrentThread(g_jvm);
415 int32_t CALEStopUnicastServer(int32_t serverID)
417 OIC_LOG(DEBUG, TAG, "CALEStopUnicastServer");
422 int32_t CALEStopMulticastServer(int32_t serverID)
424 OIC_LOG(DEBUG, TAG, "CALEStopMulticastServer");
425 gIsStartServer = FALSE;
426 CANativeLEStopScan();
430 void CALESetCallback(CAPacketReceiveCallback callback)
432 gPacketReceiveCallback = callback;
435 void CALEGetInterfaceInfo(CALocalConnectivity_t **info, uint32_t* size)
440 void CALEGetLocalAddress(char* address)
442 jboolean isAttached = FALSE;
444 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
447 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
448 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
451 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
457 jstring jni_address = CANativeGetLocalDeviceAddress(env);
458 const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
459 memcpy(address, localAddress, strlen(localAddress));
461 OIC_LOG_V(DEBUG, TAG, "Local Address : %s", address);
463 (*g_jvm)->DetachCurrentThread(g_jvm);
466 int32_t CALESendUnicastMessageImpl(const char* address, const char* data, uint32_t dataLen)
468 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastMessageImpl, address: %s, data: %s", address, data);
470 jboolean isAttached = FALSE;
472 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
475 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
476 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
479 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
485 // connect to gatt server
486 CANativeLEStopScanImpl(env, gLeScanCallback);
488 jobject jni_obj_bluetoothDevice = NULL;
489 if(gContext && gdeviceList)
492 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
494 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
497 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
501 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
504 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
507 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
509 if(!strcmp(setAddress, address))
511 jni_obj_bluetoothDevice = jarrayObj;
516 if(jni_obj_bluetoothDevice)
518 jboolean autoConnect = FALSE;
519 CANativeLEConnect(env, jni_obj_bluetoothDevice, gContext, autoConnect, gLeGattCallback);
524 (*g_jvm)->DetachCurrentThread(g_jvm);
529 int32_t CALESendMulticastMessageImpl(JNIEnv *env, const char* data, uint32_t dataLen)
531 OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s", data);
535 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
539 // connect to gatt server
540 CANativeLEStopScanImpl(env, gLeScanCallback);
543 CANativeRemoveAllGattObjsList(env);
544 CANativeCreateGattObjList(env);
547 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
549 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
552 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
556 CANativeLEConnect(env, jarrayObj, gContext, FALSE, gLeGattCallback);
559 // CANativeLEStartScan();
567 jstring CANativeGetLocalDeviceAddress(JNIEnv* env)
569 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
570 if(!jni_cid_BTAdapter)
572 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_cid_BTAdapter is null");
576 jmethodID jni_mid_getDefaultAdapter =
577 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
578 if(!jni_mid_getDefaultAdapter)
580 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_mid_getDefaultAdapter is null");
584 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getAddress", METHODID_STRINGNONPARAM);
585 if(!jni_mid_getAddress)
587 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_mid_getAddress is null");
591 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
592 if(!jni_obj_BTAdapter)
594 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_obj_BTAdapter is null");
598 jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getAddress);
601 OIC_LOG(DEBUG, TAG, "[BLE][Native] getAddress: jni_str_address is null");
605 return jni_str_address;
608 jobjectArray CANativeBondedDevices(JNIEnv *env)
610 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
611 if(!jni_cid_BTAdapter)
613 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_cid_BTAdapter is null");
617 jmethodID jni_mid_getDefaultAdapter =
618 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
620 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
621 if(!jni_obj_BTAdapter)
623 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: bluetooth adapter is null");
627 // Get a list of currently paired devices
628 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
629 "getBondedDevices", "()Ljava/util/Set;");
630 if(!jni_mid_getBondedDevices)
632 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
636 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getBondedDevices);
637 if(!jni_obj_setPairedDevices)
639 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_obj_setPairedDevices is null");
643 // Convert the set to an object array
644 // object[] array = Set<BluetoothDevice>.toArray();
645 jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
646 jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray", "()[Ljava/lang/Object;");
650 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_mid_toArray is null");
654 jobjectArray jni_arrayPairedDevices = (jobjectArray)((*env)->CallObjectMethod(env,
655 jni_obj_setPairedDevices, jni_mid_toArray));
656 if(!jni_arrayPairedDevices)
658 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBondedDevices: jni_arrayPairedDevices is null");
662 return jni_arrayPairedDevices;
665 jint CANativeGetBTStateOnInfo(JNIEnv *env)
667 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
668 if(!jni_cid_BTAdapter)
670 OIC_LOG(DEBUG, TAG, "[BLE][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
674 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
675 if (jni_fid_stateon == 0)
677 OIC_LOG(DEBUG, TAG, "[BLE][Native] get_field_state is 0");
680 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
682 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] bluetooth STATE_ON state integer value : %d", jni_int_val);
687 jboolean CANativeIsEnableBTAdapter(JNIEnv *env)
689 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
690 if(!jni_cid_BTAdapter)
692 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
696 jmethodID jni_mid_getDefaultAdapter =
697 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
698 if(!jni_mid_getDefaultAdapter)
700 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getDefaultAdapter is null");
704 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
705 if(!jni_obj_BTAdapter)
707 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_BTAdapter is null");
712 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled",
714 if(!jni_mid_isEnable)
716 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_isEnable is null");
720 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
721 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] adapter state is %d", jni_isEnable);
726 jstring CANativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
728 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
729 if(!jni_cid_device_list)
731 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_device_list is null");
735 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
736 "()Ljava/lang/String;");
737 if(!jni_mid_getAddress)
739 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getAddress is null");
743 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice, jni_mid_getAddress);
746 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_address is null");
752 jstring CANativeGetAddressFromGattObj(JNIEnv *env, jobject gatt)
756 OIC_LOG(DEBUG, TAG, "[BLE][Native] gatt is null");
760 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
761 if(!jni_cid_gattdevice_list)
763 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_gattdevice_list is null");
767 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
768 "()Landroid/bluetooth/BluetoothDevice;");
769 if(!jni_mid_getDevice)
771 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getDevice is null");
775 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
778 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_device is null");
782 jstring jni_address = CANativeGetAddressFromBTDevice(env, jni_obj_device);
785 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_address is null");
795 void CANativeGattClose(JNIEnv *env, jobject bluetoothGatt)
798 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT CLOSE");
800 // get BluetoothGatt class
801 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
802 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
803 if(!jni_cid_BluetoothGatt)
805 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
809 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt disconnect method");
810 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close","()V");
811 if(!jni_mid_closeGatt)
813 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_closeGatt is null");
817 // call disconnect gatt method
818 OIC_LOG(DEBUG, TAG, "[BLE][Native] request close Gatt");
819 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
822 void CANativeLEStartScan()
826 OIC_LOG_V(DEBUG, TAG, "server is not started yet..scan will be passed");
830 jboolean isAttached = FALSE;
832 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
835 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
837 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
840 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
846 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeLEStartScan");
847 // create new object array
848 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
849 if(!jni_cid_uuid_list)
851 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_uuid_list is null");
855 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1, jni_cid_uuid_list, NULL);
856 if(!jni_obj_uuid_list)
858 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_uuid_list is null");
862 // remove previous list and create list again
863 CANativeRemoveAllDevices(env);
864 CANativeCreateScanDeviceList(env);
867 jobject jni_obj_uuid = CANativeGetUUIDObject(env, "713d0000-503e-4c75-ba94-3148f18d941e");
868 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
870 // scan gatt server with UUID
871 if(gLeScanCallback && jni_obj_uuid_list)
873 // CANativeLEStartScanWithUUIDImpl(env, jni_obj_uuid_list, gLeScanCallback);
874 CANativeLEStartScanImpl(env, gLeScanCallback);
878 (*g_jvm)->DetachCurrentThread(g_jvm);
882 void CANativeLEStartScanImpl(JNIEnv *env, jobject callback)
884 if(!CANativeIsEnableBTAdapter(env))
886 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
890 // get default bt adapter class
891 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
892 if(!jni_cid_BTAdapter)
894 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
898 // get remote bt adapter method
899 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
900 if(!jni_mid_getDefaultAdapter)
902 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
906 // get start le scan method
907 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
908 "(Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)Z");
909 if(!jni_mid_startLeScan)
911 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan: jni_mid_startLeScan is null");
915 // gat bt adapter object
916 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
917 if(!jni_obj_BTAdapter)
919 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
923 // call start le scan method
924 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_startLeScan, callback);
925 if(!jni_obj_startLeScan)
927 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_obj_startLeScan is null");
932 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan..");
936 jobject CANativeGetUUIDObject(JNIEnv *env, const char* uuid)
939 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
942 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_uuid is null");
946 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;");
947 if(!jni_mid_fromString)
949 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_fromString is null");
953 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
954 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString, jni_uuid);
957 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_uuid is null");
964 void CANativeLEStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
966 // get default bt adapter class
967 OIC_LOG(DEBUG, TAG, "[BLE][Native] get default bt adapter class");
969 if(!CANativeIsEnableBTAdapter(env))
971 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
975 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
976 if(!jni_cid_BTAdapter)
978 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
982 // get remote bt adapter method
983 OIC_LOG(DEBUG, TAG, "[BLE][Native] get remote bt adapter method");
984 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
985 if(!jni_mid_getDefaultAdapter)
987 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
991 // get start le scan method
992 OIC_LOG(DEBUG, TAG, "[BLE][Native] get start le scan method");
993 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
994 "([Ljava/util/UUID;Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)Z");
995 if(!jni_mid_startLeScan)
997 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan: jni_mid_startLeScan is null");
1001 // get bt adapter object
1002 OIC_LOG(DEBUG, TAG, "[BLE][Native] get bt adapter object");
1003 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1004 if(!jni_obj_BTAdapter)
1006 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
1010 // call start le scan method
1011 OIC_LOG(DEBUG, TAG, "[BLE][Native] call start le scan service method");
1012 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_startLeScan, uuids, callback);
1013 if(!jni_obj_startLeScan)
1015 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_obj_startLeScan is null");
1020 OIC_LOG(DEBUG, TAG, "[BLE][Native] startLeScan..");
1024 void CANativeLEStopScan()
1026 jboolean isAttached = FALSE;
1028 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
1031 OIC_LOG_V(DEBUG, TAG, "Could not get JNIEnv pointer");
1032 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1035 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
1041 CANativeLEStopScanImpl(env, gLeScanCallback);
1044 (*g_jvm)->DetachCurrentThread(g_jvm);
1048 void CANativeLEStopScanImpl(JNIEnv *env, jobject callback)
1050 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeLEStopScan");
1052 if(!CANativeIsEnableBTAdapter(env))
1054 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1058 // get default bt adapter class
1059 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
1060 if(!jni_cid_BTAdapter)
1062 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_cid_BTAdapter is null");
1066 // get remote bt adapter method
1067 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
1068 if(!jni_mid_getDefaultAdapter)
1070 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_mid_getDefaultAdapter is null");
1074 // get start le scan method
1075 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1076 "(Landroid/bluetooth/BluetoothAdapter$LeScanCallback;)V");
1077 if(!jni_mid_stopLeScan)
1079 OIC_LOG(DEBUG, TAG, "[BLE][Native] stopLeScan: jni_mid_stopLeScan is null");
1083 // gat bt adapter object
1084 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
1085 if(!jni_obj_BTAdapter)
1087 OIC_LOG(DEBUG, TAG, "[BLE][Native] getState From BTAdapter: jni_obj_BTAdapter is null");
1091 // call start le scan method
1092 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1095 void CANativeLEConnect(JNIEnv *env, jobject bluetoothDevice, jobject context, jboolean autoconnect, jobject callback)
1097 if(!CANativeIsEnableBTAdapter(env))
1099 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1103 jstring jni_address = CANativeGetAddressFromBTDevice(env, bluetoothDevice);
1104 const char * addr = (*env)->GetStringUTFChars(env, jni_address, NULL);
1105 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] request connectGatt to %s", addr);
1109 // get BluetoothDevice class
1110 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothDevice class");
1111 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1112 if(!jni_cid_BluetoothDevice)
1114 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_cid_BluetoothDevice is null");
1118 // get connectGatt method
1119 OIC_LOG(DEBUG, TAG, "[BLE][Native] get connectGatt method");
1120 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice, "connectGatt",
1121 "(Landroid/content/Context;ZLandroid/bluetooth/BluetoothGattCallback;)Landroid/bluetooth/BluetoothGatt;");
1122 if(!jni_mid_connectGatt)
1124 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: jni_mid_connectGatt is null");
1128 OIC_LOG(DEBUG, TAG, "[BLE][Native] Call object method - connectGatt");
1129 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice, jni_mid_connectGatt, context, autoconnect, callback);
1130 if(!jni_obj_connectGatt)
1132 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: connectGatt was failed..obj will be removed");
1133 CANativeRemoveDevice(env, jni_address);
1138 OIC_LOG(DEBUG, TAG, "[BLE][Native] bleConnect: connecting..");
1142 void CANativeLEDisconnect(JNIEnv *env, jobject bluetoothGatt)
1144 if(!CANativeIsEnableBTAdapter(env))
1146 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1151 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT DISCONNECT");
1153 // get BluetoothGatt class
1154 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt classjobject bluetoothGatt");
1155 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
1156 if(!jni_cid_BluetoothGatt)
1158 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1162 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt disconnect method");
1163 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "disconnect","()V");
1164 if(!jni_mid_disconnectGatt)
1166 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_disconnectGatt is null");
1170 // call disconnect gatt method
1171 OIC_LOG(DEBUG, TAG, "[BLE][Native] request disconnectGatt");
1172 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1176 void CANativeLEDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1178 if(!CANativeIsEnableBTAdapter(env))
1180 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1184 // GATT SERVICE DISCOVERY
1185 OIC_LOG(DEBUG, TAG, "[BLE][Native] GATT SERVICE DISCOVERY");
1187 // get BluetoothGatt class
1188 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
1189 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
1190 if(!jni_cid_BluetoothGatt)
1192 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1196 OIC_LOG(DEBUG, TAG, "[BLE][Native] discovery gatt services method");
1197 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "discoverServices","()Z");
1198 if(!jni_mid_discoverServices)
1200 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_discoverServices is null");
1203 // call disconnect gatt method
1204 OIC_LOG(DEBUG, TAG, "[BLE][Native] request discovery gatt services");
1205 (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1208 void CANativeLESendData(JNIEnv *env, jobject bluetoothGatt, jobject gattCharacteristic)
1210 if(!CANativeIsEnableBTAdapter(env))
1212 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1216 // WRITE GATT CHARACTERISTIC
1217 OIC_LOG(DEBUG, TAG, "[BLE][Native] WRITE GATT CHARACTERISTIC");
1219 // get BluetoothGatt class
1220 OIC_LOG(DEBUG, TAG, "[BLE][Native] get BluetoothGatt class");
1221 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
1222 if(!jni_cid_BluetoothGatt)
1224 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1228 OIC_LOG(DEBUG, TAG, "[BLE][Native] write characteristic method");
1229 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "writeCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
1230 if(!jni_mid_writeCharacteristic)
1232 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_writeCharacteristic is null");
1236 // call disconnect gatt method
1237 OIC_LOG(DEBUG, TAG, "[BLE][Native] request write gatt characteristic");
1238 (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeCharacteristic, gattCharacteristic);
1242 jboolean CANativeSetCharacteristicNoti(JNIEnv *env, jobject bluetoothGatt)
1244 if(!CANativeIsEnableBTAdapter(env))
1246 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1250 // get BluetoothGatt class
1251 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeSetCharacteristicNoti");
1252 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
1253 if(!jni_cid_BluetoothGatt)
1255 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1259 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, "713d0002-503e-4c75-ba94-3148f18d941e");
1260 if(!jni_obj_GattCharacteristic)
1262 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1266 // set Characteristic Notification
1267 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt disconnect method");
1268 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "setCharacteristicNotification",
1269 "(Landroid/bluetooth/BluetoothGattCharacteristic;Z)Z");
1270 if(!jni_mid_setNotification)
1272 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getService is null");
1276 jboolean enable = TRUE;
1277 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification, jni_obj_GattCharacteristic, enable);
1280 OIC_LOG(DEBUG, TAG, "[BLE][Native] setCharacteristicNotification is success");
1285 OIC_LOG(DEBUG, TAG, "[BLE][Native] setCharacteristicNotification is failed");
1290 jobject CANativeGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1292 if(!CANativeIsEnableBTAdapter(env))
1294 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1298 // get BluetoothGatt class
1299 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeGetGattService");
1300 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
1301 if(!jni_cid_BluetoothGatt)
1303 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGatt is null");
1307 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt disconnect method");
1308 jmethodID jni_mid_getService = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "getService",
1309 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1310 if(!jni_mid_getService)
1312 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getService is null");
1316 jobject jni_obj_service_uuid = CANativeGetUUIDObject(env, "713d0000-503e-4c75-ba94-3148f18d941e");
1317 if(!jni_obj_service_uuid)
1319 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_service_uuid is null");
1323 // get bluetooth gatt service
1324 OIC_LOG(DEBUG, TAG, "[BLE][Native] request to get service");
1325 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService, jni_obj_service_uuid);
1327 // get bluetooth gatt service class
1328 jclass jni_cid_BluetoothGattService = (*env)->FindClass(env, "android/bluetooth/BluetoothGattService");
1329 if(!jni_cid_BluetoothGattService)
1331 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BluetoothGattService is null");
1335 OIC_LOG(DEBUG, TAG, "[BLE][Native] get gatt getCharacteristic method");
1336 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService, "getCharacteristic",
1337 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattCharacteristic;");
1338 if(!jni_mid_getCharacteristic)
1340 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getCharacteristic is null");
1344 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1345 jobject jni_obj_tx_uuid = CANativeGetUUIDObject(env, uuid);
1346 if(!jni_obj_tx_uuid)
1348 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_tx_uuid is null");
1353 OIC_LOG(DEBUG, TAG, "[BLE][Native] request to get Characteristic");
1354 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService, jni_mid_getCharacteristic, jni_obj_tx_uuid);
1356 return jni_obj_GattCharacteristic;
1359 jobject CANativeCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jstring data)
1361 if(!CANativeIsEnableBTAdapter(env))
1363 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1367 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateGattCharacteristic");
1368 jobject jni_obj_GattCharacteristic = CANativeGetGattService(env, bluetoothGatt, "713d0003-503e-4c75-ba94-3148f18d941e");
1369 if(!jni_obj_GattCharacteristic)
1371 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_obj_GattCharacteristic is null");
1375 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/BluetoothGattCharacteristic");
1376 if(!jni_cid_BTGattCharacteristic)
1378 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTGattCharacteristic is null");
1382 OIC_LOG(DEBUG, TAG, "[BLE][Native] set value in Characteristic");
1383 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1384 "(Ljava/lang/String;)Z");
1385 if(!jni_mid_setValue)
1387 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_setValue is null");
1391 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue, data);
1394 OIC_LOG(DEBUG, TAG, "[BLE][Native] the locally stored value has been set");
1395 return jni_obj_GattCharacteristic;
1399 OIC_LOG(DEBUG, TAG, "[BLE][Native] the locally stored value hasn't been set");
1404 jbyteArray CANativeGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
1406 if(!CANativeIsEnableBTAdapter(env))
1408 OIC_LOG(DEBUG, TAG, "[BLE][Native] BT adpater is not enable");
1412 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/BluetoothGattCharacteristic");
1413 if(!jni_cid_BTGattCharacteristic)
1415 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_cid_BTGattCharacteristic is null");
1419 OIC_LOG(DEBUG, TAG, "[BLE][Native] get value in Characteristic");
1420 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
1422 if(!jni_mid_getValue)
1424 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_mid_getValue is null");
1428 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic, jni_mid_getValue);
1429 return jni_obj_data_array;
1432 void CANativeCreateScanDeviceList(JNIEnv *env)
1434 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateScanDeviceList");
1436 // create new object array
1437 if (gdeviceList == NULL)
1439 OIC_LOG_V(DEBUG, TAG, "Create device list");
1441 gdeviceList = u_arraylist_create();
1445 void CANativeAddScanDeviceToList(JNIEnv *env, jobject device)
1449 OIC_LOG(DEBUG, TAG, "[BLE][Native] device is null");
1455 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdevice_list is null");
1459 jstring jni_remoteAddress = CANativeGetAddressFromBTDevice(env, device);
1460 if(!jni_remoteAddress)
1462 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1466 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1468 if(!CANativeIsDeviceInList(env, remoteAddress)) {
1469 jobject gdevice = (*env)->NewGlobalRef(env, device);
1470 u_arraylist_add(gdeviceList, gdevice);
1471 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1475 jboolean CANativeIsDeviceInList(JNIEnv *env, const char* remoteAddress){
1476 // get address value from device list
1479 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1481 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1484 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1488 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
1491 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1495 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1497 if(!strcmp(remoteAddress, setAddress))
1499 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1508 OIC_LOG_V(DEBUG, TAG, "there are no the device in list. we can add");
1512 void CANativeRemoveAllDevices(JNIEnv *env)
1514 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDevices");
1518 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
1523 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1525 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1528 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1531 (*env)->DeleteGlobalRef(env, jarrayObj);
1534 OICFree(gdeviceList);
1539 void CANativeRemoveDevice(JNIEnv *env, jstring address)
1541 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveDevice");
1545 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceList is null");
1550 for (index = 0; index < u_arraylist_length(gdeviceList); index++)
1552 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceList, index);
1555 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1559 jstring jni_setAddress = CANativeGetAddressFromBTDevice(env, jarrayObj);
1562 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1565 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1566 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
1568 if(!strcmp(setAddress, remoteAddress))
1570 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
1571 (*env)->DeleteGlobalRef(env, jarrayObj);
1573 CAReorderingDeviceList(index);
1577 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
1581 void CAReorderingDeviceList(uint32_t index)
1583 if (index >= gdeviceList->length)
1588 if (index < gdeviceList->length - 1)
1590 memmove(&gdeviceList->data[index], &gdeviceList->data[index + 1],
1591 (gdeviceList->length - index - 1) * sizeof(void *));
1594 gdeviceList->size--;
1595 gdeviceList->length--;
1601 void CANativeCreateGattObjList(JNIEnv *env)
1603 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeCreateGattObjList");
1605 // create new object array
1606 if (gGattObjectList == NULL)
1608 OIC_LOG_V(DEBUG, TAG, "Create Gatt object list");
1610 gGattObjectList = u_arraylist_create();
1614 void CANativeAddGattobjToList(JNIEnv *env, jobject gatt)
1616 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeAddGattobjToList");
1620 OIC_LOG(DEBUG, TAG, "[BLE][Native] gatt is null");
1624 if(!gGattObjectList)
1626 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1630 jstring jni_remoteAddress = CANativeGetAddressFromGattObj(env, gatt);
1631 if(!jni_remoteAddress)
1633 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1637 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1639 if(!CANativeIsGattObjInList(env, remoteAddress))
1641 jobject gGatt = (*env)->NewGlobalRef(env, gatt);
1642 u_arraylist_add(gGattObjectList, gGatt);
1643 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1647 jboolean CANativeIsGattObjInList(JNIEnv *env, const char* remoteAddress)
1649 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsGattObjInList");
1652 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1655 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1658 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1662 jstring jni_setAddress = CANativeGetAddressFromGattObj(env, jarrayObj);
1665 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1669 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1671 if(!strcmp(remoteAddress, setAddress))
1673 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1682 OIC_LOG_V(DEBUG, TAG, "there are no the gatt obejct in list. we can add");
1686 void CANativeRemoveAllGattObjsList(JNIEnv *env)
1688 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllGattObjsList");
1690 if(!gGattObjectList)
1692 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1697 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1699 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1702 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1705 (*env)->DeleteGlobalRef(env, jarrayObj);
1708 OICFree(gGattObjectList);
1709 gGattObjectList = NULL;
1713 void CANativeLEDisconnectAll(JNIEnv *env)
1715 OIC_LOG_V(DEBUG, TAG, "CANativeLEDisconnectAll");
1717 if(!gGattObjectList)
1719 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1724 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1726 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1729 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1732 CANativeLEDisconnect(env, jarrayObj);
1735 OICFree(gGattObjectList);
1736 gGattObjectList = NULL;
1740 void CANativeRemoveGattObj(JNIEnv *env, jobject gatt)
1742 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveGattObj");
1744 if(!gGattObjectList)
1746 OIC_LOG(DEBUG, TAG, "[BLE][Native] gGattObjectList is null");
1751 for (index = 0; index < u_arraylist_length(gGattObjectList); index++)
1753 jobject jarrayObj = (jobject) u_arraylist_get(gGattObjectList, index);
1756 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1760 jstring jni_setAddress = CANativeGetAddressFromGattObj(env, jarrayObj);
1763 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1766 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1768 jstring jni_remoteAddress = CANativeGetAddressFromGattObj(env, gatt);
1769 if(!jni_remoteAddress)
1771 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1774 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1776 if(!strcmp(setAddress, remoteAddress))
1778 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
1779 (*env)->DeleteGlobalRef(env, jarrayObj);
1781 CAReorderingGattList(index);
1785 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
1789 void CAReorderingGattList(uint32_t index)
1791 if (index >= gGattObjectList->length)
1796 if (index < gGattObjectList->length - 1)
1798 memmove(&gGattObjectList->data[index], &gGattObjectList->data[index + 1],
1799 (gGattObjectList->length - index - 1) * sizeof(void *));
1802 gGattObjectList->size--;
1803 gGattObjectList->length--;