src/hidapi/android/hid.cpp
changeset 12346 b4c98c84a0b2
parent 12308 4e3b4ddb8787
child 12765 80586f864508
     1.1 --- a/src/hidapi/android/hid.cpp	Sun Oct 21 22:40:17 2018 -0400
     1.2 +++ b/src/hidapi/android/hid.cpp	Mon Oct 22 14:55:42 2018 -0700
     1.3 @@ -14,6 +14,10 @@
     1.4  #include <string.h>	// For memcpy()
     1.5  
     1.6  #define TAG "hidapi"
     1.7 +
     1.8 +// Have error log always available
     1.9 +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
    1.10 +
    1.11  #ifdef DEBUG
    1.12  #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
    1.13  #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
    1.14 @@ -398,6 +402,33 @@
    1.15  		return m_pDevice;
    1.16  	}
    1.17  
    1.18 +	void ExceptionCheck( JNIEnv *env, const char *pszMethodName )
    1.19 +	{
    1.20 +		if ( env->ExceptionCheck() )
    1.21 +		{
    1.22 +			// Get our exception
    1.23 +			jthrowable jExcept = env->ExceptionOccurred();
    1.24 +
    1.25 +			// Clear the exception so we can call JNI again
    1.26 +			env->ExceptionClear();
    1.27 +
    1.28 +			// Get our exception message
    1.29 +			jclass jExceptClass = env->GetObjectClass( jExcept );
    1.30 +			jmethodID jMessageMethod = env->GetMethodID( jExceptClass, "getMessage", "()Ljava/lang/String;" );
    1.31 +			jstring jMessage = (jstring)( env->CallObjectMethod( jExcept, jMessageMethod ) );
    1.32 +			const char *pszMessage = env->GetStringUTFChars( jMessage, NULL );
    1.33 +
    1.34 +			// ...and log it.
    1.35 +			LOGE( "CHIDDevice::%s threw an exception: %s", pszMethodName, pszMessage );
    1.36 +
    1.37 +			// Cleanup
    1.38 +			env->ReleaseStringUTFChars( jMessage, pszMessage );
    1.39 +			env->DeleteLocalRef( jMessage );
    1.40 +			env->DeleteLocalRef( jExceptClass );
    1.41 +			env->DeleteLocalRef( jExcept );
    1.42 +		}
    1.43 +	}
    1.44 +
    1.45  	bool BOpen()
    1.46  	{
    1.47  		// Make sure thread is attached to JVM/env
    1.48 @@ -407,6 +438,7 @@
    1.49  
    1.50  		m_bIsWaitingForOpen = false;
    1.51  		m_bOpenResult = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerOpen, m_nId );
    1.52 +		ExceptionCheck( env, "BOpen" );
    1.53  
    1.54  		if ( m_bIsWaitingForOpen )
    1.55  		{
    1.56 @@ -515,6 +547,8 @@
    1.57  
    1.58  		jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
    1.59  		int nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendOutputReport, m_nId, pBuf );
    1.60 +		ExceptionCheck( env, "SendOutputReport" );
    1.61 +
    1.62  		env->DeleteLocalRef( pBuf );
    1.63  		return nRet;
    1.64  	}
    1.65 @@ -528,6 +562,7 @@
    1.66  
    1.67  		jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
    1.68  		int nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendFeatureReport, m_nId, pBuf );
    1.69 +		ExceptionCheck( env, "SendFeatureReport" );
    1.70  		env->DeleteLocalRef( pBuf );
    1.71  		return nRet;
    1.72  	}
    1.73 @@ -564,6 +599,7 @@
    1.74  
    1.75  		jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
    1.76  		int nRet = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerGetFeatureReport, m_nId, pBuf ) ? 0 : -1;
    1.77 +		ExceptionCheck( env, "GetFeatureReport" );
    1.78  		env->DeleteLocalRef( pBuf );
    1.79  		if ( nRet < 0 )
    1.80  		{
    1.81 @@ -622,7 +658,8 @@
    1.82  		pthread_setspecific( g_ThreadKey, (void*)env );
    1.83  
    1.84  		env->CallVoidMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerClose, m_nId );
    1.85 -
    1.86 +		ExceptionCheck( env, "Close" );
    1.87 +	
    1.88  		hid_mutex_guard dataLock( &m_dataLock );
    1.89  		m_vecData.clear();
    1.90