Commit 48417668 authored by Don Gagne's avatar Don Gagne

Merge pull request #2034 from DonLakeFlyer/AndroidSerial

Fix native function setting in android serial port
parents 736a861f 0c251d79
......@@ -79,7 +79,6 @@ public class UsbDeviceJNI extends QtActivity
}
};
// NATIVE C++ FUNCTION THAT WILL BE CALLED IF THE DEVICE IS UNPLUGGED
private static native void nativeDeviceHasDisconnected(int userDataA);
private static native void nativeDeviceException(int userDataA, String messageA);
......
......@@ -1370,6 +1370,12 @@ void QSerialPort::setError(QSerialPort::SerialPortError serialPortError, const Q
emit error(serialPortError);
}
void QSerialPort::setNativeMethods(void)
{
QSerialPortPrivate::setNativeMethods();
}
#include "moc_qserialport.cpp"
QT_END_NAMESPACE
......@@ -247,6 +247,8 @@ public:
Handle handle() const;
static void setNativeMethods(void);
Q_SIGNALS:
void baudRateChanged(qint32 baudRate, QSerialPort::Directions directions);
void dataBitsChanged(QSerialPort::DataBits dataBits);
......
......@@ -106,7 +106,6 @@ QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q)
, isCustomBaudRateSupported(false)
, emittedBytesWritten(false)
, pendingBytesWritten(0)
, hasRegisteredFunctions(false)
, jniDataBits(8)
, jniStopBits(1)
, jniParity(0)
......@@ -115,6 +114,43 @@ QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q)
{
}
void QSerialPortPrivate::setNativeMethods(void)
{
__android_log_print(ANDROID_LOG_INFO, kJTag, "Registering Native Functions");
// REGISTER THE C++ FUNCTION WITH JNI
JNINativeMethod javaMethods[] {
{"nativeDeviceHasDisconnected", "(I)V", reinterpret_cast<void *>(jniDeviceHasDisconnected)},
{"nativeDeviceNewData", "(I[B)V", reinterpret_cast<void *>(jniDeviceNewData)},
{"nativeDeviceException", "(ILjava/lang/String;)V", reinterpret_cast<void *>(jniDeviceException)}
};
QAndroidJniEnvironment jniEnv;
if (jniEnv->ExceptionCheck()) {
jniEnv->ExceptionDescribe();
jniEnv->ExceptionClear();
}
jclass objectClass = jniEnv->FindClass(kJniClassName);
if(!objectClass) {
__android_log_print(ANDROID_LOG_ERROR, kJTag, "Couldn't find class: %s", kJniClassName);
return;
}
jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0]));
__android_log_print(ANDROID_LOG_INFO, kJTag, "Native Functions Registered");
if (jniEnv->ExceptionCheck()) {
jniEnv->ExceptionDescribe();
jniEnv->ExceptionClear();
}
if (val < 0) {
__android_log_print(ANDROID_LOG_ERROR, kJTag, "Error registering methods");
}
}
bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
{
rwMode = mode;
......@@ -140,45 +176,6 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
return false;
}
if (!hasRegisteredFunctions)
{
__android_log_print(ANDROID_LOG_INFO, kJTag, "Registering Native Functions");
// REGISTER THE C++ FUNCTION WITH JNI
JNINativeMethod javaMethods[] {
{"nativeDeviceHasDisconnected", "(I)V", reinterpret_cast<void *>(jniDeviceHasDisconnected)},
{"nativeDeviceNewData", "(I[B)V", reinterpret_cast<void *>(jniDeviceNewData)},
{"nativeDeviceException", "(ILjava/lang/String;)V", reinterpret_cast<void *>(jniDeviceException)}
};
QAndroidJniEnvironment jniEnv;
if (jniEnv->ExceptionCheck()) {
jniEnv->ExceptionDescribe();
jniEnv->ExceptionClear();
}
QAndroidJniObject javaClass(kJniClassName);
if(!javaClass.isValid()) {
__android_log_print(ANDROID_LOG_ERROR, kJTag, "Java class %s not valid", kJniClassName);
return false;
}
jclass objectClass = jniEnv->GetObjectClass(javaClass.object<jobject>());
jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0]));
jniEnv->DeleteLocalRef(objectClass);
hasRegisteredFunctions = true;
__android_log_print(ANDROID_LOG_INFO, kJTag, "Native Functions Registered");
if (jniEnv->ExceptionCheck()) {
jniEnv->ExceptionDescribe();
jniEnv->ExceptionClear();
}
if(val < 0) {
__android_log_print(ANDROID_LOG_ERROR, kJTag, "Error registering methods");
q_ptr->setError(QSerialPort::OpenError);
return false;
}
}
__android_log_print(ANDROID_LOG_INFO, kJTag, "Calling Java getDeviceHandle");
cleanJavaException();
descriptor = QAndroidJniObject::callStaticMethod<jint>(
......
......@@ -108,10 +108,11 @@ public:
qint64 pendingBytesWritten;
static void setNativeMethods(void);
private:
QIODevice::OpenMode rwMode;
int deviceId;
bool hasRegisteredFunctions;
int jniDataBits;
int jniStopBits;
int jniParity;
......
......@@ -86,6 +86,25 @@ int WindowsCrtReportHook(int reportType, char* message, int* returnValue)
#endif
#ifdef __android__
#include <jni.h>
#include "qserialport.h"
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
Q_UNUSED(reserved);
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return -1;
}
QSerialPort::setNativeMethods();
return JNI_VERSION_1_6;
}
#endif
/**
* @brief Starts the application
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment