Beispiel #1
0
/**
 * EDR Method
 */
CAResult_t CAEDRNativeSendData(JNIEnv *env, const char *address, const char *data,
                               uint32_t dataLength, uint32_t id)
{
    OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btSendData logic start : %s, %d", data, dataLength);

    if (!CAEDRNativeIsEnableBTAdapter(env))
    {
        OIC_LOG(ERROR, TAG, "BT adpater is not enable");
        return CA_STATUS_INVALID_PARAM;
    }

    if (STATE_DISCONNECTED == CAEDRIsConnectedDevice(address))
    {
        // connect before send data
        OIC_LOG(DEBUG, TAG, "[EDR][Native] connect before send data");

        if (NULL == address)
        {
            OIC_LOG(ERROR, TAG, "[EDR][Native] remote address is empty");
            return CA_STATUS_INVALID_PARAM;
        }
        else
        {
            CAResult_t res = CAEDRNativeConnect(env, address, id);
            if (CA_STATUS_OK != res)
            {
                return res;
            }
        }
    }

    if (STATE_CONNECTED == CAEDRIsConnectedDevice(address))
    {
        if (!((*env)->ExceptionCheck(env)))
        {
            jclass jni_cid_BTsocket = (*env)->FindClass(env, CLASSPATH_BT_SOCKET);
            if (!jni_cid_BTsocket)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_cid_BTsocket is null");
                return CA_STATUS_FAILED;
            }

            jmethodID jni_mid_getOutputStream = (*env)->GetMethodID(env, jni_cid_BTsocket,
                                                                    "getOutputStream",
                                                                    METHODID_OUTPUTNONPARAM);
            if (!jni_mid_getOutputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_mid_getOutputStream is null");
                return CA_STATUS_FAILED;
            }

            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: Get MethodID for i/o stream");

            jobject jni_obj_socket = CAEDRNativeGetDeviceSocketBaseAddr(env, address);
            if (!jni_obj_socket)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_socket is not available");
                return CA_STATUS_FAILED;
            }

            jobject jni_obj_outputStream = (*env)->CallObjectMethod(env, jni_obj_socket,
                                                                    jni_mid_getOutputStream);
            if (!jni_obj_outputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_obj_outputStream is null");
                return CA_STATUS_FAILED;
            }

            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: ready outputStream..");

            jclass jni_cid_OutputStream = (*env)->FindClass(env, CLASSPATH_OUTPUT);
            if (!jni_cid_OutputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_cid_OutputStream is null");
                return CA_STATUS_FAILED;
            }

            jmethodID jni_mid_write = (*env)->GetMethodID(env, jni_cid_OutputStream, "write",
                                                          "([BII)V");
            if (!jni_mid_write)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_mid_write is null");
                return CA_STATUS_FAILED;
            }

            jbyteArray jbuf;
            jbuf = (*env)->NewByteArray(env, dataLength);
            (*env)->SetByteArrayRegion(env, jbuf, 0, dataLength, (jbyte*) data);

            (*env)->CallVoidMethod(env, jni_obj_outputStream, jni_mid_write, jbuf, (jint) 0,
                                   (jint) dataLength);

            if ((*env)->ExceptionCheck(env))
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: Write Error!!!");
                (*env)->ExceptionDescribe(env);
                (*env)->ExceptionClear(env);
                return CA_STATUS_FAILED;
            }

            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: Write Success");
        }
        else
        {
            (*env)->ExceptionDescribe(env);
            (*env)->ExceptionClear(env);
            OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: error!!");
            return CA_STATUS_FAILED;
        }
    }
    else
    {
        OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: BT connection is not completed!!");
    }

    return CA_STATUS_OK;
}
Beispiel #2
0
CAResult_t CAEDRNativeSendData(JNIEnv *env, const char *address, const uint8_t *data,
                               uint32_t dataLength)
{
    VERIFY_NON_NULL(env, TAG, "env is null");
    VERIFY_NON_NULL(address, TAG, "address is null");
    VERIFY_NON_NULL(data, TAG, "data is null");

    if (!CAEDRNativeIsEnableBTAdapter(env))
    {
        OIC_LOG(INFO, TAG, "BT adapter is not enabled");
        return CA_ADAPTER_NOT_ENABLED;
    }

    if (STATE_DISCONNECTED == CAEDRIsConnectedDevice(address))
    {
        // connect before send data
        OIC_LOG_V(DEBUG, TAG, "try to connect with [%s] before sending data", address);

        CAResult_t res = CAEDRNativeConnect(env, address);
        if (CA_STATUS_OK != res)
        {
            return res;
        }
    }

    if (STATE_CONNECTED == CAEDRIsConnectedDevice(address))
    {
        if (!((*env)->ExceptionCheck(env)))
        {
            jclass jni_cid_BTsocket = (*env)->FindClass(env, CLASSPATH_BT_SOCKET);
            if (!jni_cid_BTsocket)
            {
                OIC_LOG(ERROR, TAG, "jni_cid_BTsocket is null");
                return CA_STATUS_FAILED;
            }

            jmethodID jni_mid_getOutputStream = (*env)->GetMethodID(env, jni_cid_BTsocket,
                                                                    "getOutputStream",
                                                                    METHODID_OUTPUTNONPARAM);
            if (!jni_mid_getOutputStream)
            {
                OIC_LOG(ERROR, TAG, "jni_mid_getOutputStream is null");
                (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
                return CA_STATUS_FAILED;
            }

            OIC_LOG(DEBUG, TAG, "Get MethodID for i/o stream");

            jobject jni_obj_socket = CAEDRNativeGetDeviceSocketBaseAddr(env, address);
            if (!jni_obj_socket)
            {
                OIC_LOG(ERROR, TAG, "jni_socket is not available");
                (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
                return CA_STATUS_FAILED;
            }

            jobject jni_obj_outputStream = (*env)->CallObjectMethod(env, jni_obj_socket,
                                                                    jni_mid_getOutputStream);
            if (!jni_obj_outputStream)
            {
                OIC_LOG(ERROR, TAG, "jni_obj_outputStream is null");
                (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
                return CA_STATUS_FAILED;
            }

            OIC_LOG(DEBUG, TAG, "ready outputStream..");

            jclass jni_cid_OutputStream = (*env)->FindClass(env, CLASSPATH_OUTPUT);
            if (!jni_cid_OutputStream)
            {
                OIC_LOG(ERROR, TAG, "jni_cid_OutputStream is null");
                (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
                (*env)->DeleteLocalRef(env, jni_obj_outputStream);
                return CA_STATUS_FAILED;
            }

            jmethodID jni_mid_write = (*env)->GetMethodID(env, jni_cid_OutputStream, "write",
                                                          "([BII)V");
            if (!jni_mid_write)
            {
                OIC_LOG(ERROR, TAG, "jni_mid_write is null");
                (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
                (*env)->DeleteLocalRef(env, jni_obj_outputStream);
                (*env)->DeleteLocalRef(env, jni_cid_OutputStream);
                return CA_STATUS_FAILED;
            }

            jbyteArray jbuf = (*env)->NewByteArray(env, dataLength);
            (*env)->SetByteArrayRegion(env, jbuf, 0, dataLength, (jbyte*) data);

            (*env)->CallVoidMethod(env, jni_obj_outputStream, jni_mid_write, jbuf, (jint) 0,
                                   (jint) dataLength);

            (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
            (*env)->DeleteLocalRef(env, jni_obj_outputStream);
            (*env)->DeleteLocalRef(env, jni_cid_OutputStream);
            (*env)->DeleteLocalRef(env, jbuf);

            if ((*env)->ExceptionCheck(env))
            {
                OIC_LOG(ERROR, TAG, "Failed to write data in outputStram");
                (*env)->ExceptionDescribe(env);
                (*env)->ExceptionClear(env);
                return CA_STATUS_FAILED;
            }

            OIC_LOG_V(INFO, TAG, "EDR sendTo is successful: %u bytes, to %s",
                      dataLength, address);
        }
        else
        {
            (*env)->ExceptionDescribe(env);
            (*env)->ExceptionClear(env);
            OIC_LOG(ERROR, TAG, "error!!");
            return CA_STATUS_FAILED;
        }
    }
    else
    {
        OIC_LOG(DEBUG, TAG, "BT connection is not completed!!");
    }

    return CA_STATUS_OK;
}
Beispiel #3
0
/**
 * EDR Method
 */
void CAEDRNativeSendData(JNIEnv *env, const char *address, const char *data, uint32_t id)
{
    OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData logic start");

    if(STATE_DISCONNECTED == CAEDRIsConnectedDevice(address))
    {
        // connect before send data
        OIC_LOG(DEBUG, TAG, "[EDR][Native] connect before send data");

        if(NULL == address)
        {
            OIC_LOG(ERROR, TAG, "[EDR][Native] remote address is empty");
            return;
        }
        else
        {
            CAEDRNativeConnect(env, address, id);
        }
    }

    if(STATE_CONNECTED == CAEDRIsConnectedDevice(address))
    {
        if(!((*env)->ExceptionCheck(env)))
        {
            jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
            if(!jni_cid_BTsocket)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_cid_BTsocket is null");
                return;
            }

            jmethodID jni_mid_getOutputStream = (*env)->GetMethodID(env,
                    jni_cid_BTsocket, "getOutputStream",
                    "()Ljava/io/OutputStream;");
            if(!jni_mid_getOutputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_mid_getOutputStream is null");
                return;
            }
            OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btSendData: Get MethodID for i/o stream..%d", id);

            jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
            if(!jni_obj_socket)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_socket is not available");
                return;
            }

            jobject jni_obj_outputStream = (*env)->CallObjectMethod(env,
                    jni_obj_socket, jni_mid_getOutputStream);
            if(!jni_obj_outputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_obj_outputStream is null");
                return;
            }

            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: ready outputStream..");

            jclass jni_cid_OutputStream = (*env)->FindClass(env, "java/io/OutputStream");
            if(!jni_cid_OutputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_cid_OutputStream is null");
                return;
            }

            jmethodID jni_mid_write = (*env)->GetMethodID(env,
                    jni_cid_OutputStream, "write", "([BII)V");
            if(!jni_mid_write)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_mid_write is null");
                return;
            }

            jbyteArray jbuf;
            int length = strlen(data);
            jbuf = (*env)->NewByteArray(env, length);
            (*env)->SetByteArrayRegion(env, jbuf, 0, length, (jbyte*)data);

            (*env)->CallVoidMethod(env, jni_obj_outputStream, jni_mid_write,
                    jbuf, (jint) 0, (jint) length);

            if((*env)->ExceptionCheck(env))
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: Write Error!!!");
                (*env)->ExceptionDescribe(env);
                (*env)->ExceptionClear(env);
                return;
            }

            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: Write Success");

            // remove socket to list
            CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);

            // update state
            CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
        }
        else
        {
            (*env)->ExceptionDescribe(env);
            (*env)->ExceptionClear(env);
            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: error!!");
            return;
        }
    }
    else
    {
        OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: BT connection is not completed!!");
    }
}