src/hidapi/android/hid.cpp
changeset 12308 4e3b4ddb8787
parent 12242 df7260f149f2
child 12346 b4c98c84a0b2
     1.1 --- a/src/hidapi/android/hid.cpp	Mon Oct 08 12:49:28 2018 -0700
     1.2 +++ b/src/hidapi/android/hid.cpp	Mon Oct 08 12:49:30 2018 -0700
     1.3 @@ -696,7 +696,7 @@
     1.4  
     1.5  
     1.6  extern "C"
     1.7 -JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz, jobject callbackHandler);
     1.8 +JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz);
     1.9  
    1.10  extern "C"
    1.11  JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReleaseCallback)(JNIEnv *env, jobject thiz);
    1.12 @@ -721,7 +721,7 @@
    1.13  
    1.14  
    1.15  extern "C"
    1.16 -JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz, jobject callbackHandler)
    1.17 +JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceRegisterCallback)(JNIEnv *env, jobject thiz )
    1.18  {
    1.19  	LOGV( "HIDDeviceRegisterCallback()");
    1.20  
    1.21 @@ -735,11 +735,19 @@
    1.22  		__android_log_print(ANDROID_LOG_ERROR, TAG, "Error initializing pthread key");
    1.23  	}
    1.24  
    1.25 -	g_HIDDeviceManagerCallbackHandler = env->NewGlobalRef( callbackHandler );
    1.26 -	jclass objClass = env->GetObjectClass( callbackHandler );
    1.27 +	if ( g_HIDDeviceManagerCallbackHandler != NULL )
    1.28 +	{
    1.29 +		env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
    1.30 +		g_HIDDeviceManagerCallbackClass = NULL;
    1.31 +		env->DeleteGlobalRef( g_HIDDeviceManagerCallbackHandler );
    1.32 +		g_HIDDeviceManagerCallbackHandler = NULL;
    1.33 +	}
    1.34 +
    1.35 +	g_HIDDeviceManagerCallbackHandler = env->NewGlobalRef( thiz );
    1.36 +	jclass objClass = env->GetObjectClass( thiz );
    1.37  	if ( objClass )
    1.38  	{
    1.39 -		g_HIDDeviceManagerCallbackClass = reinterpret_cast< jclass >( env->NewGlobalRef(objClass) );
    1.40 +		g_HIDDeviceManagerCallbackClass = reinterpret_cast< jclass >( env->NewGlobalRef( objClass ) );
    1.41  		g_midHIDDeviceManagerOpen = env->GetMethodID( g_HIDDeviceManagerCallbackClass, "openDevice", "(I)Z" );
    1.42  		if ( !g_midHIDDeviceManagerOpen )
    1.43  		{
    1.44 @@ -773,8 +781,13 @@
    1.45  JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE(HIDDeviceReleaseCallback)(JNIEnv *env, jobject thiz)
    1.46  {
    1.47  	LOGV("HIDDeviceReleaseCallback");
    1.48 -	env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
    1.49 -	env->DeleteGlobalRef( g_HIDDeviceManagerCallbackHandler );
    1.50 +	if ( env->IsSameObject( thiz, g_HIDDeviceManagerCallbackHandler ) )
    1.51 +	{
    1.52 +		env->DeleteGlobalRef( g_HIDDeviceManagerCallbackClass );
    1.53 +		g_HIDDeviceManagerCallbackClass = NULL;
    1.54 +		env->DeleteGlobalRef( g_HIDDeviceManagerCallbackHandler );
    1.55 +		g_HIDDeviceManagerCallbackHandler = NULL;
    1.56 +	}
    1.57  }
    1.58  
    1.59  extern "C"