Example #1
0
CAResult_t CAEDRSendMulticastMessageImpl(JNIEnv *env, const uint8_t* data, uint32_t dataLen)
{
    VERIFY_NON_NULL(data, TAG, "data is null");
    OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s, %d", data, dataLen);

    // get bonded device list
    jobjectArray jni_arrayPairedDevices = CAEDRNativeGetBondedDevices(env);
    if (!jni_arrayPairedDevices)
    {
        OIC_LOG(ERROR, TAG, "[EDR][Native] jni_arrayPairedDevices is empty");
        return CA_STATUS_INVALID_PARAM;
    }
    // Get information from array of devices
    jclass jni_cid_BTDevice = (*env)->FindClass(env, CLASSPATH_BT_DEVICE);
    jmethodID j_mid_getName = (*env)->GetMethodID(env, jni_cid_BTDevice, "getName",
                                                  METHODID_STRINGNONPARAM);
    jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress",
                                                     METHODID_STRINGNONPARAM);

    jsize length = (*env)->GetArrayLength(env, jni_arrayPairedDevices);
    for (jsize i = 0; i < length; i++)
    {
        // get name, address from BT device
        jobject j_obj_device = (*env)->GetObjectArrayElement(env, jni_arrayPairedDevices, i);
        jstring j_str_name = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getName);

        if (j_str_name)
        {
            const char * name = (*env)->GetStringUTFChars(env, j_str_name, NULL);
            OIC_LOG_V(DEBUG, TAG, "[EDR][Native] getBondedDevices: ~~device name is %s", name);
            (*env)->ReleaseStringUTFChars(env, j_str_name, name);
            (*env)->DeleteLocalRef(env, j_str_name);
        }

        jstring j_str_address = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getAddress);
        const char * remoteAddress = (*env)->GetStringUTFChars(env, j_str_address, NULL);
        (*env)->DeleteLocalRef(env, j_obj_device);
        OIC_LOG_V(DEBUG, TAG,
                  "[EDR][Native] getBondedDevices: ~~device address is %s", remoteAddress);

        // find address
        CAResult_t res = CAEDRNativeSendData(env, remoteAddress, data, dataLen);
        (*env)->ReleaseStringUTFChars(env, j_str_address, remoteAddress);
        (*env)->DeleteLocalRef(env, j_str_address);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG_V(ERROR, TAG, "CASendMulticastMessageImpl, failed to send message to : %s",
                      remoteAddress);
            g_edrErrorHandler(remoteAddress, data, dataLen, res);
            continue;
        }
    }

    (*env)->DeleteLocalRef(env, jni_arrayPairedDevices);
    (*env)->DeleteLocalRef(env, jni_cid_BTDevice);

    return CA_STATUS_OK;
}
Example #2
0
CAResult_t CAEDRSendMulticastMessageImpl(JNIEnv *env, const char* data, uint32_t dataLen)
{
    OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s, %d", data, dataLen);

    // get bonded device list
    jobjectArray jni_arrayPairedDevices = CAEDRNativeGetBondedDevices(env);
    if(!jni_arrayPairedDevices)
    {
        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_arrayPairedDevices is empty");
        return CA_STATUS_INVALID_PARAM;
    }
    // Get information from array of devices
    jsize length = (*env)->GetArrayLength(env, jni_arrayPairedDevices);
    jsize i;
    for( i = 0 ; i < length ; i++ )
    {
        // get name, address from BT device
        jobject j_obj_device = (*env)->GetObjectArrayElement(env, jni_arrayPairedDevices, i);

        jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
        jmethodID j_mid_getName = (*env)->GetMethodID(env, jni_cid_BTDevice,
                "getName", "()Ljava/lang/String;");
        jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice,
                "getAddress", "()Ljava/lang/String;");

        jstring j_str_name = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getName);

        if(j_str_name)
        {
            const char * name = (*env)->GetStringUTFChars(env, j_str_name, NULL);
            OIC_LOG_V(DEBUG, TAG, "[EDR][Native] getBondedDevices: ~~device name is %s", name);
            (*env)->ReleaseStringUTFChars(env, j_str_name, name);
        }

        jstring j_str_address = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getAddress);
        const char * remoteAddress = (*env)->GetStringUTFChars(env, j_str_address, NULL);
        OIC_LOG_V(DEBUG, TAG,
                "[EDR][Native] getBondedDevices: ~~device address is %s", remoteAddress);

        // find address
        CAEDRNativeSendData(env, remoteAddress, data, i);
    }

    return CA_STATUS_OK;
}
Example #3
0
CAResult_t CAEDRSendUnicastMessageImpl(const char* address, const uint8_t* data, uint32_t dataLen)
{
    VERIFY_NON_NULL(address, TAG, "address is null");
    VERIFY_NON_NULL(data, TAG, "data is null");

    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, "CAEDRSendUnicastMessageImpl - Could not get JNIEnv pointer");
        res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
        if (JNI_OK != res)
        {
            OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
            return CA_STATUS_INVALID_PARAM;
        }
        isAttached = true;
    }

    OIC_LOG(DEBUG, TAG, "set byteArray for data");

    // get bonded device list
    jobjectArray jni_arrayPairedDevices = CAEDRNativeGetBondedDevices(env);
    if (!jni_arrayPairedDevices)
    {
        OIC_LOG(ERROR, TAG, "jni_arrayPairedDevices is empty");
        if (isAttached)
        {
            (*g_jvm)->DetachCurrentThread(g_jvm);
        }
        return CA_STATUS_INVALID_PARAM;
    }
    // Get information from array of devices
    jclass jni_cid_BTDevice = (*env)->FindClass(env, CLASSPATH_BT_DEVICE);
    jmethodID j_mid_getName = (*env)->GetMethodID(env, jni_cid_BTDevice, "getName",
                                                  METHODID_STRINGNONPARAM);
    jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress",
                                                     METHODID_STRINGNONPARAM);

    jsize length = (*env)->GetArrayLength(env, jni_arrayPairedDevices);
    for (jsize i = 0; i < length; i++)
    {
        OIC_LOG(DEBUG, TAG, "start to check device");
        // get name, address from BT device
        jobject j_obj_device = (*env)->GetObjectArrayElement(env, jni_arrayPairedDevices, i);
        jstring j_str_name = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getName);

        if (j_str_name)
        {
            const char * name = (*env)->GetStringUTFChars(env, j_str_name, NULL);
            OIC_LOG_V(DEBUG, TAG, "device name is %s", name);
            (*env)->ReleaseStringUTFChars(env, j_str_name, name);
            (*env)->DeleteLocalRef(env, j_str_name);
        }

        jstring j_str_address = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getAddress);
        const char * remoteAddress = (*env)->GetStringUTFChars(env, j_str_address, NULL);
        (*env)->DeleteLocalRef(env, j_obj_device);
        if (!remoteAddress)
        {
            OIC_LOG(ERROR, TAG, "remoteAddress is null");
            if (isAttached)
            {
                (*g_jvm)->DetachCurrentThread(g_jvm);
            }

            (*env)->DeleteLocalRef(env, j_str_address);
            (*env)->DeleteLocalRef(env, jni_arrayPairedDevices);
            (*env)->DeleteLocalRef(env, jni_cid_BTDevice);
            return CA_STATUS_INVALID_PARAM;
        }
        OIC_LOG_V(DEBUG, TAG, "device address is %s", remoteAddress);

        // find address
        if (!strcmp(remoteAddress, address))
        {
            CAResult_t res = CAEDRNativeSendData(env, remoteAddress, data, dataLen);
            (*env)->ReleaseStringUTFChars(env, j_str_address, remoteAddress);
            (*env)->DeleteLocalRef(env, j_str_address);
            if (CA_STATUS_OK != res)
            {
                (*env)->DeleteLocalRef(env, jni_arrayPairedDevices);
                (*env)->DeleteLocalRef(env, jni_cid_BTDevice);
                return res;
            }
            break;
        }
        (*env)->ReleaseStringUTFChars(env, j_str_address, remoteAddress);
        (*env)->DeleteLocalRef(env, j_str_address);
    }

    (*env)->DeleteLocalRef(env, jni_arrayPairedDevices);
    (*env)->DeleteLocalRef(env, jni_cid_BTDevice);

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

    return CA_STATUS_OK;
}
Example #4
0
CAResult_t CAEDRSendUnicastMessageImpl(const char* address, const char* data,
    uint32_t dataLen)
{
    OIC_LOG_V(DEBUG, TAG, "CAEDRSendUnicastMessageImpl, address: %s, data: %s", address, data);

    jboolean isAttached = JNI_FALSE;
    JNIEnv* env;
    jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
    if(res != JNI_OK)
    {
        OIC_LOG(DEBUG, TAG, "CAEDRSendUnicastMessageImpl - Could not get JNIEnv pointer");
        res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
        if(res != JNI_OK)
        {
            OIC_LOG(DEBUG, TAG, "AttachCurrentThread failed");
            return CA_STATUS_INVALID_PARAM;
        }
        isAttached = TRUE;
    }

    OIC_LOG(DEBUG, TAG, "[EDR][Native] set byteArray for data");
    if(g_sendBuffer)
    {
        (*env)->DeleteGlobalRef(env, g_sendBuffer);
    }
    jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
    (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*)data);
    g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);

    // get bonded device list
    jobjectArray jni_arrayPairedDevices = CAEDRNativeGetBondedDevices(env);
    if(!jni_arrayPairedDevices)
    {
        OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_arrayPairedDevices is empty");
        if (isAttached)
            (*g_jvm)->DetachCurrentThread(g_jvm);
        return CA_STATUS_INVALID_PARAM;
    }
    // Get information from array of devices
    jsize length = (*env)->GetArrayLength(env, jni_arrayPairedDevices);
    jsize i;
    for( i = 0 ; i < length ; i++ )
    {
        OIC_LOG(DEBUG, TAG, "[EDR][Native] start to check device");
        // get name, address from BT device
        jobject j_obj_device = (*env)->GetObjectArrayElement(env, jni_arrayPairedDevices, i);

        jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
        jmethodID j_mid_getName = (*env)->GetMethodID(env, jni_cid_BTDevice,
                "getName", "()Ljava/lang/String;");
        jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice,
                "getAddress", "()Ljava/lang/String;");

        jstring j_str_name = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getName);

        if(j_str_name)
        {
            const char * name = (*env)->GetStringUTFChars(env, j_str_name, NULL);
            OIC_LOG_V(DEBUG, TAG, "[EDR][Native] getBondedDevices: ~~device name is %s", name);
            (*env)->ReleaseStringUTFChars(env, j_str_name, name);
        }

        jstring j_str_address = (*env)->CallObjectMethod(env, j_obj_device, j_mid_getAddress);
        const char * remoteAddress = (*env)->GetStringUTFChars(env, j_str_address, NULL);
        OIC_LOG_V(DEBUG, TAG,
                "[EDR][Native] getBondedDevices: ~~device address is %s", remoteAddress);

        if (!remoteAddress) {
            OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
            if (isAttached)
                (*g_jvm)->DetachCurrentThread(g_jvm);
            return CA_STATUS_INVALID_PARAM;
        }
        if (!address) {
            OIC_LOG(ERROR, TAG, "[EDR][Native] address is null");
            if (isAttached)
                (*g_jvm)->DetachCurrentThread(g_jvm);
            return CA_STATUS_INVALID_PARAM;
        }
        // find address
        if(!strcmp(remoteAddress, address))
        {
            CAEDRNativeSendData(env, remoteAddress, data, i);
        }
    }

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

    return CA_STATUS_OK;
}