下边是我在做串口通信时的代码:
static const char *classPathName = "android/serialport/SerialPort"; //注意Ljava/io/FileDescriptor;最后的分号,刚开始做时漏了这个分号,查了两天时间,汗 static JNINativeMethod methods[] = { {"open", "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)android_serialport_SerialPort_open }, {"close", "()V", (void*)android_serialport_SerialPort_close }, }; /* * Register several native methods for one class. */ static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int numMethods) { jclass clazz; clazz = (*env)->FindClass(env, className); if (clazz == NULL) return JNI_FALSE; if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) { LOGE("register nativers error"); return JNI_FALSE; } return JNI_TRUE; } /* * Register native methods for all classes we know about. * * returns JNI_TRUE on success. */ static int registerNatives(JNIEnv* env) { if (!registerNativeMethods(env, classPathName, methods, sizeof(methods) / sizeof(methods[0]))) { return JNI_FALSE; } return JNI_TRUE; } /* * This is called by the VM when the shared library is first loaded. */ jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; jint result = -1; LOGI("Entering JNI_OnLoad\n"); if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) goto bail; assert(env != NULL); if (!registerNatives(env)) goto bail; /* success -- return valid version number */ result = JNI_VERSION_1_4; bail: LOGI("Leaving JNI_OnLoad (result=0x%x)\n", result); return result; }