gpt4 book ai didi

java.lang 在 JNI 中调用 BluetoothAdapter.getDefaultAdapter() 时抛出 UNsatisfiedLinkError

转载 作者:行者123 更新时间:2023-11-28 06:10:57 26 4
gpt4 key购买 nike

我正在 Qt Creator 5.4 中构建一个简单的 Android 应用程序,使用 JNI 在 C++ 中调用 Android 的蓝牙 API。我花了几个小时查看 JNI 教程,但我终其一生都无法让这个非常简单的类(class)发挥作用。每次我运行我的应用程序时,它都会立即崩溃并显示以下控制台输出:

JNI_OnLoad(JavaVM*, void*)): Can't find getDefaultAdapter
D/AndroidRuntime(15795): Shutting down VM
E/AndroidRuntime(15795): FATAL EXCEPTION: main
E/AndroidRuntime(15795): Process: org.qtproject.example.HostSim, PID: 15795
E/AndroidRuntime(15795): java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad in "/data/app/org.qtproject.example.HostSim-1/lib/arm/libHostSim.so"
E/AndroidRuntime(15795): at java.lang.Runtime.loadLibrary(Runtime.java:371)
E/AndroidRuntime(15795): at java.lang.System.loadLibrary(System.java:989)
E/AndroidRuntime(15795): at org.qtproject.qt5.android.bindings.QtActivity.loadApplication(QtActivity.java:252)
E/AndroidRuntime(15795): at org.qtproject.qt5.android.bindings.QtActivity.startApp(QtActivity.java:655)
E/AndroidRuntime(15795): at org.qtproject.qt5.android.bindings.QtActivity.onCreate(QtActivity.java:895)
E/AndroidRuntime(15795): at android.app.Activity.performCreate(Activity.java:5933)
E/AndroidRuntime(15795): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
E/AndroidRuntime(15795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
E/AndroidRuntime(15795): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
E/AndroidRuntime(15795): at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime(15795): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
E/AndroidRuntime(15795): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(15795): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(15795): at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime(15795): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(15795): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(15795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime(15795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
I/Process (15795): Sending signal. PID: 15795 SIG: 9

现在我有一个名为 androidBluetoothService.cpp 的类,我正尝试使用 getDefaultAdapter() 创建 BluetoothAdapter 对象。我已经在 list 文件中添加了 android.permission.BLUETOOTH 和 android.permission.BLUETOOTH_ADMIN。下面是我的代码:

#include <Qdebug>
#include "androidBluetoothService.h"

// JNI properties
static JavaVM* jvm = 0;
static jclass bluetoothAdapterClassID = 0;
static jmethodID getDefaultAdapterMethodID=0;

//File path for Java classes
const char *bluetoothAdapterClassPath = "android/bluetooth/BluetoothAdapter";

// AndroidBluetoothService Constructor
androidBluetoothService::androidBluetoothService()
{
JNIEnv* env;
// Qt is running in a different thread than android UI, so you always Java VM *MUST* be attached to current thread
if (jvm->AttachCurrentThread(&env, NULL)<0)
{
qCritical()<<"AttachCurrentThread failed";
return;
}

// Start bluetoothAdapter with BluetoothAdapter.getDefaultAdapter()
bluetoothAdapter = env->CallStaticObjectMethod(bluetoothAdapterClassID,getDefaultAdapterMethodID);
if (!bluetoothAdapter)
{ qCritical()<<"Can't getDefaultAdapter";
return;}
else {
qDebug() << "Bluetooth ready";
}

jvm->DetachCurrentThread();
}


// this method is called immediately after the module is load
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
{
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
qCritical()<<"Can't get the enviroument";
return JNI_ERR;}
else{
qDebug() << "JRE ready"; }
jvm = vm;

// search for our class
jclass clazz=env->FindClass(bluetoothAdapterClassPath);
if (!clazz)
{ qCritical()<<"Can't find BluetoothAdapter class";
return JNI_ERR;}
else{ qDebug() << "BluetoothAdapter class found";}


// keep a global reference to it
bluetoothAdapterClassID = (jclass)env->NewGlobalRef(clazz);

//FIXME: Still causes fatal exception
// search for getDefaultAdapter
getDefaultAdapterMethodID = env->GetStaticMethodID(bluetoothAdapterClassID, "getDefaultAdapter", "()Landroid/bluetooth/BluetoothAdapter"); //This is where exception occurs
if (!getDefaultAdapterMethodID) {
qCritical()<<"Can't find getDefaultAdapter";
return JNI_ERR; }

qDebug()<<"Method registering finished";
return JNI_VERSION_1_6;
}

我知道不满意的链接错误意味着 JNI 找不到该方法,但我多次检查了签名和名称,发现对其他人的代码有相同的调用,并且对他们有效。请指教。

我正在使用 Android SDK 21 和 Qt Creator 5.4

最佳答案

您为 getDefaultAdapter 指定的签名不正确。当您指定以 L 开头的类名时,您应该以分号结尾,如:

"()Landroid/bluetooth/BluetoothAdapter;"

关于java.lang 在 JNI 中调用 BluetoothAdapter.getDefaultAdapter() 时抛出 UNsatisfiedLinkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com