static jobject g_bluetoothGattServer = NULL;
static jobject g_bluetoothGattServerCallback = NULL;
static jobject g_leAdvertiseCallback = NULL;
+static jobject g_bluetoothManager = NULL;
static CAPacketReceiveCallback g_packetReceiveCallback = NULL;
static CABLEErrorHandleCallback g_serverErrorCallback;
static bool g_isInitializedServer = false;
static jbyteArray g_sendBuffer = NULL;
+static jobject g_obj_bluetoothDevice = NULL;
static CABLEDataReceivedCallback g_CABLEServerDataReceivedCallback = NULL;
static ca_mutex g_bleReqRespCbMutex = NULL;
return CA_STATUS_FAILED;
}
+/**
+ * get the current connection state of the gatt profile to the remote device.
+ * @param[in] env JNI interface pointer.
+ * @param[in] device bluetooth device object
+ * @return state of the profile connection.
+ */
+static jint CALEServerGetConnectionState(JNIEnv *env, jobject device)
+{
+ OIC_LOG(DEBUG, TAG, "CALEServerGetConnectionState");
+
+ VERIFY_NON_NULL_RET(env, TAG, "env", -1);
+ VERIFY_NON_NULL_RET(device, TAG, "device", -1);
+
+ jclass jni_cid_bluetoothManager = (*env)->FindClass(env, "android/bluetooth/BluetoothManager");
+ if (!jni_cid_bluetoothManager)
+ {
+ OIC_LOG(ERROR, TAG, "jni_cid_bluetoothManager is null");
+ return -1;
+ }
+
+ jmethodID jni_mid_getConnectionState = (*env)->GetMethodID(env, jni_cid_bluetoothManager,
+ "getConnectionState",
+ "(Landroid/bluetooth/BluetoothDevice"
+ ";I)I");
+ if (!jni_mid_getConnectionState)
+ {
+ OIC_LOG(ERROR, TAG, "jni_mid_getConnectionState is null");
+ return -1;
+ }
+
+ if (!g_bluetoothManager)
+ {
+ OIC_LOG(ERROR, TAG, "g_bluetoothManager is null");
+ return -1;
+ }
+
+ jint jni_state = (jint)(*env)->CallIntMethod(env, g_bluetoothManager,
+ jni_mid_getConnectionState,
+ device, GATT_PROFILE);
+ OIC_LOG_V(INFO, TAG, "connection state is %d", jni_state);
+ return jni_state;
+}
+
jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData)
{
OIC_LOG(DEBUG, TAG, "IN - CALEServerSetResponseData");
return CA_ADAPTER_NOT_ENABLED;
}
+ if (STATE_CONNECTED != CALEServerGetConnectionState(env, device))
+ {
+ OIC_LOG(ERROR, TAG, "it is not connected state");
+ return CA_STATUS_FAILED;
+ }
+
jclass jni_cid_bluetoothGattServer = (*env)->FindClass(env,
"android/bluetooth/BluetoothGattServer");
if (!jni_cid_bluetoothGattServer)
return NULL;
}
+ if (g_bluetoothManager)
+ {
+ (*env)->DeleteGlobalRef(env, g_bluetoothManager);
+ }
+ g_bluetoothManager = (*env)->NewGlobalRef(env, jni_obj_bluetoothManager);
+
jobject jni_obj_bluetoothGattServer = (*env)->CallObjectMethod(env, jni_obj_bluetoothManager,
jni_mid_openGattServer,
g_context,
g_sendBuffer = NULL;
}
+ if (g_bluetoothManager)
+ {
+ (*env)->DeleteGlobalRef(env, g_bluetoothManager);
+ g_bluetoothManager = NULL;
+ }
+
ca_cond_free(g_threadSendNotifyCond);
g_threadSendNotifyCond = NULL;
ca_mutex_lock(g_threadSendMutex);
- jobject jni_obj_bluetoothDevice = NULL;
uint32_t length = u_arraylist_length(g_connectedDeviceList);
for (uint32_t index = 0; index < length; index++)
{
if (!strcmp(setAddress, address))
{
OIC_LOG(DEBUG, TAG, "found the device");
- jni_obj_bluetoothDevice = jarrayObj;
+
+ if (g_obj_bluetoothDevice)
+ {
+ (*env)->DeleteGlobalRef(env, g_obj_bluetoothDevice);
+ }
+ g_obj_bluetoothDevice = (*env)->NewGlobalRef(env, jarrayObj);
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
break;
}
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
}
- if (jni_obj_bluetoothDevice)
+ if (g_obj_bluetoothDevice)
{
jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
(*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
- CAResult_t res = CALEServerSend(env, jni_obj_bluetoothDevice, g_sendBuffer);
+ CAResult_t res = CALEServerSend(env, g_obj_bluetoothDevice, g_sendBuffer);
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "send has failed");
+ if (g_obj_bluetoothDevice)
+ {
+ (*env)->DeleteGlobalRef(env, g_obj_bluetoothDevice);
+ g_obj_bluetoothDevice = NULL;
+ }
goto error_exit;
}
}
continue;
}
- CAResult_t res = CALEServerSend(env, jarrayObj, jni_bytearr_data);
+ if (g_obj_bluetoothDevice)
+ {
+ (*env)->DeleteGlobalRef(env, g_obj_bluetoothDevice);
+ }
+ g_obj_bluetoothDevice = (*env)->NewGlobalRef(env, jarrayObj);
+
+ CAResult_t res = CALEServerSend(env, g_obj_bluetoothDevice, jni_bytearr_data);
if (CA_STATUS_OK != res)
{
OIC_LOG_V(ERROR, TAG, "send has failed for the device[%s]", address);
(*env)->ReleaseStringUTFChars(env, jni_address, address);
+ if (g_obj_bluetoothDevice)
+ {
+ (*env)->DeleteGlobalRef(env, g_obj_bluetoothDevice);
+ g_obj_bluetoothDevice = NULL;
+ }
continue;
}
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "send has failed");
+
+ if (g_obj_bluetoothDevice)
+ {
+ (*env)->DeleteGlobalRef(env, g_obj_bluetoothDevice);
+ g_obj_bluetoothDevice = NULL;
+ }
+
ca_mutex_lock(g_threadSendNotifyMutex);
g_isSignalSetFlag = true;
ca_cond_signal(g_threadSendNotifyCond);
{
OIC_LOG(DEBUG, TAG, "notify success");
+ if (g_obj_bluetoothDevice)
+ {
+ (*env)->DeleteGlobalRef(env, g_obj_bluetoothDevice);
+ g_obj_bluetoothDevice = NULL;
+ }
+
// next data can be sent
ca_mutex_lock(g_threadSendNotifyMutex);
OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
(*env)->DeleteGlobalRef(env, g_bluetoothGattServerCallback);
}
+ if (g_obj_bluetoothDevice)
+ {
+ (*env)->DeleteGlobalRef(env, g_obj_bluetoothDevice);
+ g_obj_bluetoothDevice = NULL;
+ }
+
ca_mutex_lock(g_threadSendNotifyMutex);
ca_cond_signal(g_threadSendNotifyCond);
ca_mutex_unlock(g_threadSendNotifyMutex);