CAResult_t CASetLEClientAutoConnectionDeviceInfo(const char* address)
{
    OIC_LOG(DEBUG, TAG, "CASetClientAutoConnectionDeviceInfo");
    VERIFY_NON_NULL(address, TAG, "address");

    bool isAttached = false;
    JNIEnv* env;
    jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
    if (JNI_OK != res)
    {
        OIC_LOG(DEBUG, TAG, "AttachCurrentThread will be called for JNIEnv pointer");
        res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);

        if (JNI_OK != res)
        {
            OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
            return CA_STATUS_FAILED;
        }
        isAttached = true;
    }

    OIC_LOG_V(DEBUG, TAG, "set [%s] for Auto Connection", address);
    jstring jni_leAddress = (*env)->NewStringUTF(env, address);

    if (!CAManagerCheckBTAddress(env, jni_leAddress))
    {
        OIC_LOG(ERROR, TAG, "this address is not BT address string format");
        if (isAttached)
        {
            (*g_jvm)->DetachCurrentThread(g_jvm);
        }
        return CA_STATUS_FAILED;
    }

    // if there is target address in SharedPreference. it will be reseted.
    if (CAManagerIsConnectedDeviceAddress(env, g_context,
                                          jni_leAddress,
                                          g_connectedDeviceSet))
    {
        if (!CAManagerRemoveConnectedDeviceAddress(env, g_context,
                                                   jni_leAddress,
                                                   g_connectedDeviceSet))
        {
            OIC_LOG(ERROR, TAG, "Preference - remove has failed");
        }
        else
        {
            OIC_LOG(INFO, TAG, "Preference - remove success");
        }
    }

    // it will be added new target address.
    if (!CAManagerAddConnectedDeviceAddress(env, g_context,
                                            jni_leAddress, g_connectedDeviceSet))
    {
        OIC_LOG(ERROR, TAG, "Preference - putting has failed");
    }
    else
    {
        OIC_LOG(INFO, TAG, "Preference - putting success");
    }

    if (isAttached)
    {
        (*g_jvm)->DetachCurrentThread(g_jvm);
    }

    return CA_STATUS_OK;
}
예제 #2
0
/*
 * Class:     org_iotivity_ca_jar_caleinterface
 * Method:    caManagerLeServicesDiscoveredCallback
 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
 */
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeClientInterface_caManagerLeServicesDiscoveredCallback(JNIEnv *env,
                                                                               jobject obj,
                                                                               jobject gatt,
                                                                               jint status)
{
    OIC_LOG_V(INFO, TAG, "caManagerLeServicesDiscoveredCallback - status %d", status);
    VERIFY_NON_NULL_VOID(env, TAG, "env");
    VERIFY_NON_NULL_VOID(obj, TAG, "obj");
    VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");

    if (GATT_SUCCESS == status)
    {
        if (!g_connectedDeviceSet)
        {
            OIC_LOG(ERROR, TAG, "g_connectedDeviceSet is null");
            return;
        }

        jstring jni_address = CALEGetAddressFromGatt(env, gatt);
        if (!jni_address)
        {
            OIC_LOG(ERROR, TAG, "CALEGetAddressFromGatt is null");
            return;
        }

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

        OIC_LOG_V(DEBUG, TAG, "ServicesDiscovered device : %s", address);

        if (CAManagerIsConnectedDeviceAddress(env, g_context, jni_address, g_connectedDeviceSet))
        {
            OIC_LOG(INFO, TAG, "AC list - the address will be added to ACData list");
            CAManagerAddACData(env, jni_address);
            CAManagerSetAutoConnectingFlag(env, jni_address, false);

            // next connection will be requested with JNI_TRUE on autoConnect flag
            // after first connection
            CALEClientSetFlagToState(env, jni_address, CA_LE_AUTO_CONNECT_FLAG, JNI_TRUE);
        }
        else
        {
            OIC_LOG(DEBUG, TAG, "AC list - the address is not set to AutoConnect");
        }

        (*env)->ReleaseStringUTFChars(env, jni_address, address);
        (*env)->DeleteLocalRef(env, jni_address);

        OIC_LOG(INFO, TAG, "ServicesDiscovery is successful");
    }
    else
    {
        OIC_LOG(ERROR, TAG, "ServicesDiscovery has failed");
    }
}