From 2b205fda91bef4ab2592b8818bc0d76438af5a7e Mon Sep 17 00:00:00 2001 From: Doug McLain Date: Fri, 5 Apr 2024 13:12:34 -0400 Subject: [PATCH] Updates for Qt6.7 and Android API 34 --- android/src/main/java/USBSerialWrapper.java | 7 +++++-- androidserialport.cpp | 17 +++++++++++++---- androidserialport.h | 2 ++ serialambe.cpp | 1 + 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/USBSerialWrapper.java b/android/src/main/java/USBSerialWrapper.java index 20a79c7..06811e6 100644 --- a/android/src/main/java/USBSerialWrapper.java +++ b/android/src/main/java/USBSerialWrapper.java @@ -62,6 +62,7 @@ public class USBSerialWrapper implements SerialInputOutputManagerTest.Listener { } else { System.out.println("USB Permission Denied"); + device_denied(); } } if ( (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) || (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) ) { @@ -171,10 +172,11 @@ public class USBSerialWrapper implements SerialInputOutputManagerTest.Listener { } } } - + m_driver = availableDrivers.get(devicenum); - PendingIntent mPendingIntent = PendingIntent.getBroadcast(c, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE); + PendingIntent mPendingIntent = PendingIntent.getBroadcast(c, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT); m_manager.requestPermission(m_driver.getDevice(), mPendingIntent); + System.out.println("setup_serial() finished"); return "Yipee!"; } @@ -253,5 +255,6 @@ public class USBSerialWrapper implements SerialInputOutputManagerTest.Listener { private static native void data_received(byte[] data); private static native void device_open(); + private static native void device_denied(); private static native void devices_changed(); } diff --git a/androidserialport.cpp b/androidserialport.cpp index a5f7908..4067c44 100644 --- a/androidserialport.cpp +++ b/androidserialport.cpp @@ -25,7 +25,7 @@ AndroidSerialPort::AndroidSerialPort(QObject *) qDebug() << "com.hoho.android.usbserial.driver/UsbSerialDriver available"; serialJavaObject = QAndroidJniObject("DroidStar/USBSerialWrapper"); QAndroidJniEnvironment env; - JNINativeMethod methods[] = { {"data_received", "([B)V", reinterpret_cast(java_data_received)}, {"device_open", "()V", reinterpret_cast(java_device_open)}, {"devices_changed", "()V", reinterpret_cast(java_devices_changed)}}; + JNINativeMethod methods[] = { {"data_received", "([B)V", reinterpret_cast(java_data_received)}, {"device_open", "()V", reinterpret_cast(java_device_open)}, {"device_denied", "()V", reinterpret_cast(java_device_denied)}, {"devices_changed", "()V", reinterpret_cast(java_devices_changed)}}; jclass objectClass = env->GetObjectClass(serialJavaObject.object()); env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0])); env->DeleteLocalRef(objectClass); @@ -41,8 +41,8 @@ QStringList AndroidSerialPort::discover_devices() QStringList l; l.clear(); qDebug() << "AndroidSerialPort::discover_devices()"; - - QAndroidJniObject d = serialJavaObject.callObjectMethod("discover_devices", "(Landroid/content/Context;)[Ljava/lang/String;", QNativeInterface::QAndroidApplication::context()); + auto activity = QJniObject(QNativeInterface::QAndroidApplication::context()); + QAndroidJniObject d = serialJavaObject.callObjectMethod("discover_devices", "(Landroid/content/Context;)[Ljava/lang/String;", activity.object()); jobjectArray devices = d.object(); int size = env->GetArrayLength(devices); @@ -55,7 +55,9 @@ QStringList AndroidSerialPort::discover_devices() int AndroidSerialPort::open(int p) { - serialJavaObject.callObjectMethod("setup_serial", "(Landroid/content/Context;)Ljava/lang/String;", QNativeInterface::QAndroidApplication::context()); + auto activity = QJniObject(QNativeInterface::QAndroidApplication::context()); + serialJavaObject.callObjectMethod("setup_serial", "(Landroid/content/Context;)Ljava/lang/String;", activity.object()); + qDebug() << "AndroidSerialPort::open() finished"; return p; } @@ -144,6 +146,13 @@ void AndroidSerialPort::java_device_open(JNIEnv *, jobject) emit AndroidSerialPort::GetInstance().device_ready(); } + +void AndroidSerialPort::java_device_denied(JNIEnv *, jobject) +{ + emit AndroidSerialPort::GetInstance().device_denied(); + +} + void AndroidSerialPort::java_devices_changed(JNIEnv *, jobject) { emit AndroidSerialPort::GetInstance().devices_changed(); diff --git a/androidserialport.h b/androidserialport.h index a89d77e..460e95c 100644 --- a/androidserialport.h +++ b/androidserialport.h @@ -51,11 +51,13 @@ signals: void readyRead(); void data_received(QByteArray); void device_ready(); + void device_denied(); void devices_changed(); private: explicit AndroidSerialPort(QObject * parent = nullptr); static void java_data_received(JNIEnv *env, jobject t, jbyteArray data); static void java_device_open(JNIEnv *env, jobject t); + static void java_device_denied(JNIEnv *env, jobject t); static void java_devices_changed(JNIEnv *env, jobject t); #if QT_VERSION < QT_VERSION_CHECK(6, 3, 0) QAndroidJniObject serialJavaObject; diff --git a/serialambe.cpp b/serialambe.cpp index bbaee82..692399b 100755 --- a/serialambe.cpp +++ b/serialambe.cpp @@ -120,6 +120,7 @@ void SerialAMBE::connect_to_serial(QString p) #else m_serial = &AndroidSerialPort::GetInstance(); connect(m_serial, SIGNAL(device_ready()), this, SLOT(config_ambe())); + //connect(m_serial, SIGNAL(device_denied()), this, SLOT(config_ambe())); #endif m_serial->setPortName(p); m_serial->setBaudRate(br);