Exemple #1
0
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCallback(
        JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
{
    OIC_LOG_V(DEBUG, TAG, "caLeGattServerNWConnectionStateChangeCallback - status %d, newstate %d",
              status, newState);
    VERIFY_NON_NULL_VOID(env, TAG, "env");
    VERIFY_NON_NULL_VOID(obj, TAG, "obj");
    VERIFY_NON_NULL_VOID(device, TAG, "device");

    if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
    {
        if (g_bleConnectionStateChangedCallback)
        {
            jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
            if (!jni_remoteAddress)
            {
                OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
                return;
            }

            const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
            if (!address)
            {
                OIC_LOG(ERROR, TAG, "address is null");
                return;
            }

            // STATE_DISCONNECTED
            jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
                                                            "STATE_DISCONNECTED");

            // STATE_CONNECTED
            jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
                                                         "STATE_CONNECTED");

            if (state_disconnected == newState)
            {
                OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
                g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
            }
            else if (state_connected == newState)
            {
                OIC_LOG_V(DEBUG, TAG, "LE Connected state is %d, %s", newState, address);
                g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
            }
            else
            {
                OIC_LOG_V(DEBUG, TAG, "Unknown state : %d, %s", newState, address);
            }

            (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address);
        }
    }
}
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCallback(
        JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
{
    OIC_LOG(DEBUG, TAG, " Gatt Server NWConnectionStateChange Callback");

    VERIFY_NON_NULL_VOID(env, TAG, "env");
    VERIFY_NON_NULL_VOID(obj, TAG, "obj");
    VERIFY_NON_NULL_VOID(device, TAG, "device");
    (void)status;

    // STATE_DISCONNECTED
    jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
                                                    "STATE_DISCONNECTED");
    if (state_disconnected == newState)
    {
        jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
        if (!jni_remoteAddress)
        {
            OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
            return;
        }

        const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
        if (!address)
        {
            OIC_LOG(ERROR, TAG, "address is null");
            return;
        }

        if (g_bleConnectionStateChangedCallback)
        {
            g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
        }

        (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address);
    }
}