static jobject g_foundDeviceListenerObject = NULL;
static jobject g_listenerObject = NULL;
static JavaVM *g_jvm = NULL;
+static jclass g_jni_cls_enum = NULL;
+static jmethodID g_jni_mid_enum = NULL;
JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
{
goto exit_error;
}
- jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
- if (!jni_cls_enum)
+ if (g_jni_cls_enum && g_jni_mid_enum)
{
- LOGE("could not get jni_cls_enum");
- goto exit_error;
+ jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
+ g_jni_mid_enum, info->adapter);
+ (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
+ jni_adaptertype, jni_address,
+ (jboolean)connected);
}
- jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
- "(I)Lorg/iotivity/base/OcConnectivityType;");
- if (!jni_mid_enum)
- {
- LOGE("could not get Method ID (getInstance)");
- goto exit_error;
- }
-
- jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
- jni_mid_enum, info->adapter);
- (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
- jni_adaptertype, jni_address,
- (jboolean)connected);
-
exit_error:
if (isAttached)
{
goto exit_error;
}
- jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
- if (!jni_cls_enum)
+ if (g_jni_cls_enum && g_jni_mid_enum)
{
- LOGE("could not get jni_cls_enum");
- goto exit_error;
- }
+ jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum,
+ g_jni_mid_enum, adapter);
- jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance",
- "(I)Lorg/iotivity/base/OcConnectivityType;");
- if (!jni_mid_enum)
- {
- LOGE("could not get Method ID (getInstance)");
- goto exit_error;
+ (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
+ jni_adaptertype, (jboolean)enabled);
}
- jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum,
- jni_mid_enum, adapter);
-
- (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener,
- jni_adaptertype, (jboolean)enabled);
-
exit_error:
if (isAttached)
{
CAUtilClientInitialize(env, g_jvm, context);
- g_listenerObject = (*env)->NewGlobalRef(env, listener);
+ if (listener)
+ {
+ g_listenerObject = (*env)->NewGlobalRef(env, listener);
+ }
+
+ if (g_listenerObject)
+ {
+ jclass cls = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType");
+ if (cls)
+ {
+ g_jni_cls_enum = (jclass)(*env)->NewGlobalRef(env, cls);
+ }
+ if (g_jni_cls_enum)
+ {
+ g_jni_mid_enum = (*env)->GetStaticMethodID(env, g_jni_cls_enum, "getInstance",
+ "(I)Lorg/iotivity/base/OcConnectivityType;");
+ }
+ }
CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB,
CAManagerConnectionStateChangedCB);
}
(*env)->DeleteGlobalRef(env, g_listenerObject);
g_listenerObject = NULL;
}
+
+ if (g_jni_cls_enum)
+ {
+ (*env)->DeleteGlobalRef(env, g_jni_cls_enum);
+ g_jni_cls_enum = NULL;
+ }
+
+ if (g_jni_mid_enum)
+ {
+ (*env)->DeleteGlobalRef(env, g_jni_mid_enum);
+ g_jni_mid_enum = NULL;
+ }
}
JNIEXPORT void JNICALL