Commit 0c251d79 authored by Don Gagne's avatar Don Gagne

Fix native function setting

parent 9fcdda7b
...@@ -79,7 +79,6 @@ public class UsbDeviceJNI extends QtActivity ...@@ -79,7 +79,6 @@ public class UsbDeviceJNI extends QtActivity
} }
}; };
// NATIVE C++ FUNCTION THAT WILL BE CALLED IF THE DEVICE IS UNPLUGGED // NATIVE C++ FUNCTION THAT WILL BE CALLED IF THE DEVICE IS UNPLUGGED
private static native void nativeDeviceHasDisconnected(int userDataA); private static native void nativeDeviceHasDisconnected(int userDataA);
private static native void nativeDeviceException(int userDataA, String messageA); private static native void nativeDeviceException(int userDataA, String messageA);
......
...@@ -1370,6 +1370,12 @@ void QSerialPort::setError(QSerialPort::SerialPortError serialPortError, const Q ...@@ -1370,6 +1370,12 @@ void QSerialPort::setError(QSerialPort::SerialPortError serialPortError, const Q
emit error(serialPortError); emit error(serialPortError);
} }
void QSerialPort::setNativeMethods(void)
{
QSerialPortPrivate::setNativeMethods();
}
#include "moc_qserialport.cpp" #include "moc_qserialport.cpp"
QT_END_NAMESPACE QT_END_NAMESPACE
...@@ -247,6 +247,8 @@ public: ...@@ -247,6 +247,8 @@ public:
Handle handle() const; Handle handle() const;
static void setNativeMethods(void);
Q_SIGNALS: Q_SIGNALS:
void baudRateChanged(qint32 baudRate, QSerialPort::Directions directions); void baudRateChanged(qint32 baudRate, QSerialPort::Directions directions);
void dataBitsChanged(QSerialPort::DataBits dataBits); void dataBitsChanged(QSerialPort::DataBits dataBits);
......
...@@ -106,7 +106,6 @@ QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q) ...@@ -106,7 +106,6 @@ QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q)
, isCustomBaudRateSupported(false) , isCustomBaudRateSupported(false)
, emittedBytesWritten(false) , emittedBytesWritten(false)
, pendingBytesWritten(0) , pendingBytesWritten(0)
, hasRegisteredFunctions(false)
, jniDataBits(8) , jniDataBits(8)
, jniStopBits(1) , jniStopBits(1)
, jniParity(0) , jniParity(0)
...@@ -115,34 +114,10 @@ QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q) ...@@ -115,34 +114,10 @@ QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q)
{ {
} }
bool QSerialPortPrivate::open(QIODevice::OpenMode mode) void QSerialPortPrivate::setNativeMethods(void)
{ {
rwMode = mode;
__android_log_print(ANDROID_LOG_INFO, kJTag, "Opening %s", systemLocation.toLatin1().data());
__android_log_print(ANDROID_LOG_INFO, kJTag, "Calling Java Open");
QAndroidJniObject jnameL = QAndroidJniObject::fromString(systemLocation);
cleanJavaException();
deviceId = QAndroidJniObject::callStaticMethod<jint>(
kJniClassName,
"open",
"(Ljava/lang/String;I)I",
jnameL.object<jstring>(),
(jint)this);
cleanJavaException();
isReadStopped = false;
if (deviceId == BAD_PORT)
{
__android_log_print(ANDROID_LOG_ERROR, kJTag, "Error opening %s", systemLocation.toLatin1().data());
q_ptr->setError(QSerialPort::DeviceNotFoundError);
return false;
}
if (!hasRegisteredFunctions)
{
__android_log_print(ANDROID_LOG_INFO, kJTag, "Registering Native Functions"); __android_log_print(ANDROID_LOG_INFO, kJTag, "Registering Native Functions");
// REGISTER THE C++ FUNCTION WITH JNI // REGISTER THE C++ FUNCTION WITH JNI
JNINativeMethod javaMethods[] { JNINativeMethod javaMethods[] {
{"nativeDeviceHasDisconnected", "(I)V", reinterpret_cast<void *>(jniDeviceHasDisconnected)}, {"nativeDeviceHasDisconnected", "(I)V", reinterpret_cast<void *>(jniDeviceHasDisconnected)},
...@@ -156,15 +131,14 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) ...@@ -156,15 +131,14 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
jniEnv->ExceptionClear(); jniEnv->ExceptionClear();
} }
QAndroidJniObject javaClass(kJniClassName); jclass objectClass = jniEnv->FindClass(kJniClassName);
if(!javaClass.isValid()) { if(!objectClass) {
__android_log_print(ANDROID_LOG_ERROR, kJTag, "Java class %s not valid", kJniClassName); __android_log_print(ANDROID_LOG_ERROR, kJTag, "Couldn't find class: %s", kJniClassName);
return false; return;
} }
jclass objectClass = jniEnv->GetObjectClass(javaClass.object<jobject>());
jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0])); 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"); __android_log_print(ANDROID_LOG_INFO, kJTag, "Native Functions Registered");
if (jniEnv->ExceptionCheck()) { if (jniEnv->ExceptionCheck()) {
...@@ -172,11 +146,34 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) ...@@ -172,11 +146,34 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
jniEnv->ExceptionClear(); jniEnv->ExceptionClear();
} }
if(val < 0) { if (val < 0) {
__android_log_print(ANDROID_LOG_ERROR, kJTag, "Error registering methods"); __android_log_print(ANDROID_LOG_ERROR, kJTag, "Error registering methods");
q_ptr->setError(QSerialPort::OpenError);
return false;
} }
}
bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
{
rwMode = mode;
__android_log_print(ANDROID_LOG_INFO, kJTag, "Opening %s", systemLocation.toLatin1().data());
__android_log_print(ANDROID_LOG_INFO, kJTag, "Calling Java Open");
QAndroidJniObject jnameL = QAndroidJniObject::fromString(systemLocation);
cleanJavaException();
deviceId = QAndroidJniObject::callStaticMethod<jint>(
kJniClassName,
"open",
"(Ljava/lang/String;I)I",
jnameL.object<jstring>(),
(jint)this);
cleanJavaException();
isReadStopped = false;
if (deviceId == BAD_PORT)
{
__android_log_print(ANDROID_LOG_ERROR, kJTag, "Error opening %s", systemLocation.toLatin1().data());
q_ptr->setError(QSerialPort::DeviceNotFoundError);
return false;
} }
__android_log_print(ANDROID_LOG_INFO, kJTag, "Calling Java getDeviceHandle"); __android_log_print(ANDROID_LOG_INFO, kJTag, "Calling Java getDeviceHandle");
......
...@@ -108,10 +108,11 @@ public: ...@@ -108,10 +108,11 @@ public:
qint64 pendingBytesWritten; qint64 pendingBytesWritten;
static void setNativeMethods(void);
private: private:
QIODevice::OpenMode rwMode; QIODevice::OpenMode rwMode;
int deviceId; int deviceId;
bool hasRegisteredFunctions;
int jniDataBits; int jniDataBits;
int jniStopBits; int jniStopBits;
int jniParity; int jniParity;
......
...@@ -86,6 +86,25 @@ int WindowsCrtReportHook(int reportType, char* message, int* returnValue) ...@@ -86,6 +86,25 @@ int WindowsCrtReportHook(int reportType, char* message, int* returnValue)
#endif #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 * @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