Example #1
0
jobject CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address)
{
    VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
    VERIFY_NON_NULL_RET(remote_le_address, TAG, "remote_le_address", NULL);

    OIC_LOG(DEBUG, TAG, "IN - CAManagerConnectGatt");

    jobject jni_bluetooth = CAManagerGetRemoteDevice(env, remote_le_address);
    if (!jni_bluetooth)
    {
        OIC_LOG(ERROR, TAG, "jni_bluetooth is null");
        return NULL;
    }

    if (!CAManagerIsDeviceBonded(env, jni_bluetooth))
    {
        OIC_LOG(INFO, TAG, "device is BONDED_NONE");
    }

    // request to connection with AutoConnection Flag
    OIC_LOG(INFO, TAG, "request gatt connection by CM auto connector");
    jobject newGatt = (jobject)CALEClientConnect(env, jni_bluetooth, JNI_TRUE);
    if (NULL == newGatt)
    {
        OIC_LOG(INFO, TAG, "re-connection will be started");
        return NULL;
    }

    // set flag auto connection is requested.
    CAManagerSetAutoConnectingFlag(env, remote_le_address, true);

    OIC_LOG(DEBUG, TAG, "OUT - CAManagerConnectGatt");
    return newGatt;
}
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeClientInterface_caManagerLeGattConnectionStateChangeCB(
        JNIEnv *env, jobject obj, jobject gatt, jint status, jint newState)
{
    OIC_LOG_V(INFO, TAG, "caManagerLeGattConnectionStateChangeCB - status %d, newState %d",
              status, newState);

    VERIFY_NON_NULL_VOID(env, TAG, "env");
    VERIFY_NON_NULL_VOID(obj, TAG, "obj");
    VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");

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

    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, "caManagerLeGattConnectionStateChangeCB - address [%s]", address);

    if (GATT_SUCCESS == status && state_connected == newState) // le connected
    {
        OIC_LOG(DEBUG, TAG, "LE is connected");

        CAResult_t res = CAManagerReadRemoteRssi(env, gatt);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, TAG, "CAManagerReadRemoteRssi has failed");
            goto exit;
        }
    }
    else if (state_disconnected == newState)// le disconnected
    {
        OIC_LOG(DEBUG, TAG, "LE is disconnected");

        if (LINK_LOSS == status || REMOTE_DISCONNECT == status)
        {
            if (!CAManagerIsInACDataList(env, jni_address))
            {
                OIC_LOG_V(DEBUG, TAG, "this[%s] is not target address for Auto Connection",
                          address);
                goto exit;
            }

            CAManagerSetAutoConnectingFlag(env, jni_address, false);

            CAResult_t res = CAManagerStartAutoConnection(env, jni_address);
            if (CA_STATUS_OK != res)
            {
                OIC_LOG(ERROR, TAG, "CAManagerStartAutoConnection has failed");
                goto exit;
            }
        }
        else if (ACCEPT_TIMEOUT_EXCEPTION == status)
        {
            CAManagerProcessRecovery(env, START_RECOVERY);
        }
    }

exit:
    (*env)->ReleaseStringUTFChars(env, jni_address, address);
    (*env)->DeleteLocalRef(env, jni_address);
}
/*
 * 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");
    }
}
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeClientInterface_caManagerAdapterStateChangedCallback(
        JNIEnv *env, jobject obj, jint state)
{
    OIC_LOG_V(INFO, TAG, "caManagerAdapterStateChangedCallback - state %d", state);
    VERIFY_NON_NULL_VOID(env, TAG, "env");
    VERIFY_NON_NULL_VOID(obj, TAG, "obj");

    jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
    jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
    jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");

    if (state_on == state)
    {
        OIC_LOG(DEBUG, TAG, "AdapterStateChangedCallback : state_on");

        // when BT state is on. recovery flag has to be reset.
        CAManagerSetBTRecovery(false);

        // find target device for autoconnect
        size_t length = CAManagerGetACDataLength();
        OIC_LOG_V(DEBUG, TAG, "length of ACDataList : %d", length);
        for (size_t idx = 0; idx < length; idx++)
        {
            jstring leAddress = CAManagerGetLEAddressFromACData(env, idx);
            if (leAddress)
            {
                CAResult_t res = CAManagerStartAutoConnection(env, leAddress);
                if (CA_STATUS_OK != res)
                {
                    OIC_LOG(ERROR, TAG, "CAManagerStartAutoConnection has failed");
                    return;
                }
            }
        }
    }
    else if (state_off == state)
    {
        OIC_LOG(DEBUG, TAG, "AdapterStateChangedCallback : state_off");

        // reset isAutoConnecting flag for all target devices
        size_t length = CAManagerGetACDataLength();
        OIC_LOG_V(DEBUG, TAG, "length of ACDataList : %d", length);
        for (size_t idx = 0; idx < length; idx++)
        {
            jstring address = CAManagerGetLEAddressFromACData(env, idx);
            if (address)
            {
                CAManagerSetAutoConnectingFlag(env, address, false);
            }
        }

        // check whether BT recovery is needed or not
        if (CAManagerIsRecoveryFlagSet())
        {
            CAManagerProcessRecovery(env, STATE_OFF);
        }
    }
    else if (state_turning_off == state)
    {
        OIC_LOG(DEBUG, TAG, "AdapterStateChangedCallback : state_turning_off");
    }
    else
    {
        OIC_LOG(INFO, TAG, "AdapterStateChangedCallback state is not available");
    }
}