3 #include <android/log.h>
4 #include "caedrutils.h"
6 #include "oic_malloc.h"
7 #include "uthreadpool.h"
8 #include "uarraylist.h"
10 #define TAG PCF("CA_EDR_UTILS")
12 static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
13 static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
14 static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
16 static u_arraylist_t *gdeviceStateList = NULL;
17 static u_arraylist_t *gdeviceObjectList = NULL;
19 // get address from bluetooth socket
20 jstring CAEDRNativeGetAddressFromDeviceSocket(JNIEnv *env, jobject bluetoothSocketObj)
22 if(!bluetoothSocketObj)
27 jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
30 OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_cid_BTSocket is null");
34 jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTSocket, "getRemoteDevice",
35 "()Landroid/bluetooth/BluetoothDevice;");
36 if(!jni_mid_getRemoteDevice)
38 OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_mid_getRemoteDevice is null");
42 jobject jni_obj_remoteBTDevice = (*env)->CallObjectMethod(env, bluetoothSocketObj, jni_mid_getRemoteDevice);
43 if(!jni_obj_remoteBTDevice)
45 OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_obj_remoteBTDevice is null");
49 jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
50 jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress", "()Ljava/lang/String;");
52 jstring j_str_address = (*env)->CallObjectMethod(env, jni_obj_remoteBTDevice, j_mid_getAddress);
55 const char * address = (*env)->GetStringUTFChars(env, j_str_address, NULL);
56 OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: ~~ remote device address is %s", address);
57 // (*env)->ReleaseStringUTFChars(env, j_str_address, address);
64 jstring CAEDRNativeGetLocalDeviceAddress(JNIEnv* env)
66 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
67 if(!jni_cid_BTAdapter)
69 OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_cid_BTAdapter is null");
73 jmethodID jni_mid_getDefaultAdapter =
74 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
75 if(!jni_mid_getDefaultAdapter)
77 OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_mid_getDefaultAdapter is null");
81 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getAddress", METHODID_STRINGNONPARAM);
82 if(!jni_mid_getAddress)
84 OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_mid_getAddress is null");
88 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
89 if(!jni_obj_BTAdapter)
91 OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_obj_BTAdapter is null");
95 jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getAddress);
98 OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_str_address is null");
102 return jni_str_address;
105 jobjectArray CAEDRNativeGetBondedDevices(JNIEnv *env)
107 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
108 if(!jni_cid_BTAdapter)
110 OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_cid_BTAdapter is null");
114 jmethodID jni_mid_getDefaultAdapter =
115 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
117 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
118 if(!jni_obj_BTAdapter)
120 OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: bluetooth adapter is null");
124 // Get a list of currently paired devices
125 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
126 "getBondedDevices", "()Ljava/util/Set;");
127 if(!jni_mid_getBondedDevices)
129 OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
133 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getBondedDevices);
134 if(!jni_obj_setPairedDevices)
136 OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_obj_setPairedDevices is null");
140 // Convert the set to an object array
141 // object[] array = Set<BluetoothDevice>.toArray();
142 jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
143 jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray", "()[Ljava/lang/Object;");
147 OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_mid_toArray is null");
151 jobjectArray jni_arrayPairedDevices = (jobjectArray)((*env)->CallObjectMethod(env,
152 jni_obj_setPairedDevices, jni_mid_toArray));
153 if(!jni_arrayPairedDevices)
155 OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_arrayPairedDevices is null");
159 return jni_arrayPairedDevices;
162 jint CAEDRNativeGetBTStateOnInfo(JNIEnv *env)
164 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
165 if(!jni_cid_BTAdapter)
167 OIC_LOG(DEBUG, TAG, "[EDR][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
171 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
172 if (jni_fid_stateon == 0)
174 OIC_LOG(DEBUG, TAG, "[EDR][Native] get_field_state is 0");
177 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
179 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] bluetooth STATE_ON state integer value : %d", jni_int_val);
184 jboolean CAEDRNativeIsEnableBTAdapter(JNIEnv *env)
186 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
187 if(!jni_cid_BTAdapter)
189 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
193 jmethodID jni_mid_getDefaultAdapter =
194 (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
195 if(!jni_mid_getDefaultAdapter)
197 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_getDefaultAdapter is null");
201 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
202 if(!jni_obj_BTAdapter)
204 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_obj_BTAdapter is null");
209 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled",
211 if(!jni_mid_isEnable)
213 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_isEnable is null");
217 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
218 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] adapter state is %d", jni_isEnable);
223 jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
225 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
226 if(!jni_cid_device_list)
228 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_cid_device_list is null");
232 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
233 "()Ljava/lang/String;");
234 if(!jni_mid_getAddress)
236 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_getAddress is null");
240 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice, jni_mid_getAddress);
243 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_address is null");
253 void CAEDRNativeCreateDeviceStateList()
255 OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceStateList");
257 // create new object array
258 if (gdeviceStateList == NULL)
260 OIC_LOG_V(DEBUG, TAG, "Create device list");
262 gdeviceStateList = u_arraylist_create();
266 void CAEDRUpdateDeviceState(uint32_t state, const char* address)
268 state_t *newstate = (state_t*) OICMalloc( sizeof(state_t) );
269 memset(newstate->address, 0, CA_MACADDR_SIZE);
270 strcpy(newstate->address, address);
271 newstate->state = state;
273 CAEDRNativeAddDeviceStateToList(newstate);
276 void CAEDRNativeAddDeviceStateToList(state_t* state)
280 OIC_LOG(DEBUG, TAG, "[EDR][Native] device is null");
284 if(!gdeviceStateList)
286 OIC_LOG(DEBUG, TAG, "[EDR][Native] gdevice_list is null");
290 if(CAEDRNativeIsDeviceInList(state->address)) {
291 CAEDRNativeRemoveDevice(state->address); // delete previous state for update new state
293 u_arraylist_add(gdeviceStateList, state); // update new state
294 OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d", state->state);
297 jboolean CAEDRNativeIsDeviceInList(const char* remoteAddress){
300 for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
302 state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
305 OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
309 if(!strcmp(remoteAddress, state->address))
311 OIC_LOG_V(DEBUG, TAG, "the device is already set");
320 OIC_LOG_V(DEBUG, TAG, "there are no the device in list.");
324 void CAEDRNativeRemoveAllDeviceState()
326 OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
328 if(!gdeviceStateList)
330 OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
335 for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
337 state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
340 OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
346 OICFree(gdeviceStateList);
347 gdeviceStateList = NULL;
351 void CAEDRNativeRemoveDevice(const char* remoteAddress)
353 OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceforStateList");
355 if(!gdeviceStateList)
357 OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
362 for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
364 state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
367 OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
371 if(!strcmp(state->address, remoteAddress))
373 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove state : %s", remoteAddress);
376 CAEDRReorderingDeviceList(index);
383 jboolean CAEDRIsConnectedDevice(const char* remoteAddress)
385 OIC_LOG_V(DEBUG, TAG, "CAEDRIsConnectedDevice");
387 if(!gdeviceStateList)
389 OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
394 for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
396 state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
399 OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
403 if(!strcmp(state->address, remoteAddress))
405 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] check whether it is connected or not");
413 void CAEDRReorderingDeviceList(uint32_t index)
415 if (index >= gdeviceStateList->length)
420 if (index < gdeviceStateList->length - 1)
422 memmove(&gdeviceStateList->data[index], &gdeviceStateList->data[index + 1],
423 (gdeviceStateList->length - index - 1) * sizeof(void *));
426 gdeviceStateList->size--;
427 gdeviceStateList->length--;
431 * Device Socket Object List
433 void CAEDRNativeCreateDeviceSocketList()
435 OIC_LOG(DEBUG, TAG, "[BLE][Native] CAEDRNativeCreateDeviceSocketList");
437 // create new object array
438 if (gdeviceObjectList == NULL)
440 OIC_LOG_V(DEBUG, TAG, "Create Device object list");
442 gdeviceObjectList = u_arraylist_create();
446 void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
448 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeAddDeviceobjToList");
452 OIC_LOG(DEBUG, TAG, "[BLE][Native] Device is null");
456 if(!gdeviceObjectList)
458 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
462 jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
463 if(!jni_remoteAddress)
465 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
469 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
471 if(!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
473 jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
474 u_arraylist_add(gdeviceObjectList, gDeviceSocker);
475 OIC_LOG_V(DEBUG, TAG, "Set Socket Object to Array");
479 jboolean CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
481 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsDeviceObjInList");
484 for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
487 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
490 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
494 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
497 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
501 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
503 if(!strcmp(remoteAddress, setAddress))
505 OIC_LOG_V(DEBUG, TAG, "the device is already set");
514 OIC_LOG_V(DEBUG, TAG, "there are no the Device obejct in list. we can add");
518 void CAEDRNativeSocketCloseToAll(JNIEnv *env)
520 OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeSocketCloseToAll");
522 if(!gdeviceObjectList)
524 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
528 jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
529 if(!jni_cid_BTSocket)
531 OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
535 jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
538 OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_mid_close is null");
543 for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
545 jobject jni_obj_socket = (jobject) u_arraylist_get(gdeviceObjectList, index);
548 OIC_LOG(DEBUG, TAG, "[BLE][Native] socket obj is null");
552 (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
554 if((*env)->ExceptionCheck(env))
556 OIC_LOG(DEBUG, TAG, "[EDR][Native] close: close is Failed!!!");
557 (*env)->ExceptionDescribe(env);
558 (*env)->ExceptionClear(env);
564 void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
566 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
568 if(!gdeviceObjectList)
570 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
575 for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
577 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
580 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
583 (*env)->DeleteGlobalRef(env, jarrayObj);
586 OICFree(gdeviceObjectList);
587 gdeviceObjectList = NULL;
591 void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
593 OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
595 if(!gdeviceObjectList)
597 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
602 for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
604 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
607 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
611 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
614 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
617 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
619 jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
620 if(!jni_remoteAddress)
622 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
625 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
627 if(!strcmp(setAddress, remoteAddress))
629 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
630 (*env)->DeleteGlobalRef(env, jarrayObj);
632 CAEDRReorderingDeviceSocketList(index);
637 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
641 void CAEDRNativeRemoveDeviceSocketBaseAddr(JNIEnv *env, jstring address)
643 OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
645 if(!gdeviceObjectList)
647 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
652 for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
654 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
657 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
661 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
664 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
667 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
668 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
670 if(!strcmp(setAddress, remoteAddress))
672 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
673 (*env)->DeleteGlobalRef(env, jarrayObj);
675 CAEDRReorderingDeviceSocketList(index);
680 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
684 jobject CAEDRNativeGetDeviceSocket(uint32_t idx)
686 OIC_LOG_V(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
688 if(!gdeviceObjectList)
690 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
694 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, idx);
697 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is not available");
703 uint32_t CAEDRGetSocketListLength()
705 if(!gdeviceObjectList)
707 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
711 uint32_t length = u_arraylist_length(gdeviceObjectList);
716 void CAEDRReorderingDeviceSocketList(uint32_t index)
718 if (index >= gdeviceObjectList->length)
723 if (index < gdeviceObjectList->length - 1)
725 memmove(&gdeviceObjectList->data[index], &gdeviceObjectList->data[index + 1],
726 (gdeviceObjectList->length - index - 1) * sizeof(void *));
729 gdeviceObjectList->size--;
730 gdeviceObjectList->length--;