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

时间:2015-07-08 01:02:55

标签: java android c++ qt java-native-interface

我正在使用JNI在Qt Creator 5.4中构建一个简单的Android应用程序,用C ++调用Android的蓝牙API。我花了好几个小时来查看JNI教程,但是我不能为我的生活找到这个非常简单的课程。每次运行我的应用程序时,它都会立即崩溃,并显示以下控制台输出:

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对象。我已经在我的清单文件中添加了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

1 个答案:

答案 0 :(得分:1)

您为Traceback (most recent call last): File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/Library/Python/2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request response = self.make_response(rv) File "/Library/Python/2.7/site-packages/flask/app.py", line 1566, in make_response raise ValueError('View function did not return a response') ValueError: View function did not return a response 指定的签名不正确。当您指定以getDefaultAdapter开头的类名时,应使用分号结束,如:

L