diff --git a/Source/SenseGloveAndroid/Private/SGAndroid/SGConnectJNI.cpp b/Source/SenseGloveAndroid/Private/SGAndroid/SGConnectJNI.cpp index 2d51c95c..a192da9c 100644 --- a/Source/SenseGloveAndroid/Private/SGAndroid/SGConnectJNI.cpp +++ b/Source/SenseGloveAndroid/Private/SGAndroid/SGConnectJNI.cpp @@ -40,11 +40,11 @@ #include "Android/AndroidJava.h" #endif /* PLATFORM_ANDROID */ - static constexpr char SGCONNECT_JAVA_CLASS_NAME[] = "com.senseglove.sgconnect.SGConnect"; +static constexpr char SGCONNECT_JAVA_CLASS_NAME[] = "com.senseglove.sgconnect.SGConnect"; int32 FSGConnectJNI::Init() { - int32 Result = 0; + int32 Result = -999; #if PLATFORM_ANDROID JNIEnv* Env = FAndroidApplication::GetJavaEnv(); @@ -67,7 +67,7 @@ int32 FSGConnectJNI::Init() int32 FSGConnectJNI::Dispose() { - int32 Result = 0; + int32 Result = -999; #if PLATFORM_ANDROID JNIEnv* Env = FAndroidApplication::GetJavaEnv(); @@ -111,5 +111,104 @@ FString FSGConnectJNI::GetLibraryVersion() } #endif /* PLATFORM_ANDROID */ - return Result; + return Result; +} + +int32 FSGConnectJNI::ActiveDevices() +{ + int32 Result = -999; + +#if PLATFORM_ANDROID + JNIEnv* Env = FAndroidApplication::GetJavaEnv(); + if (Env) + { + jclass Class = FAndroidApplication::FindJavaClass(SGCONNECT_JAVA_CLASS_NAME); + if (!Env->IsSameObject(Class, nullptr)) + { + jmethodID Method = Env->GetStaticMethodID(Class, "ActiveDevices", "()I"); + if (Method) + { + Result = Env->CallStaticIntMethod(Class, Method); + } + } + } +#endif /* PLATFORM_ANDROID */ + + return Result; +} + +FString FSGConnectJNI::GetDeviceString(const int32 DeviceAddress) +{ + FString Result; + +#if PLATFORM_ANDROID + JNIEnv* Env = FAndroidApplication::GetJavaEnv(); + if (Env) + { + jclass Class = FAndroidApplication::FindJavaClass(SGCONNECT_JAVA_CLASS_NAME); + if (!Env->IsSameObject(Class, nullptr)) + { + jmethodID Method = Env->GetStaticMethodID(Class, "GetDeviceString", "(I)Ljava/lang/String;"); + if (Method) + { + jstring ResultJString = (jstring)Env->CallStaticObjectMethod(Class, Method, DeviceAddress); + const char* ResultChars = Env->GetStringUTFChars(ResultJString, nullptr); + Result = FString(UTF8_TO_TCHAR(ResultChars)); + Env->ReleaseStringUTFChars(ResultJString, ResultChars); + } + } + } +#endif /* PLATFORM_ANDROID */ + + return Result; +} + +FString FSGConnectJNI::GetSensorString(const int32 DeviceAddress) +{ + FString Result; + +#if PLATFORM_ANDROID + JNIEnv* Env = FAndroidApplication::GetJavaEnv(); + if (Env) + { + jclass Class = FAndroidApplication::FindJavaClass(SGCONNECT_JAVA_CLASS_NAME); + if (!Env->IsSameObject(Class, nullptr)) + { + jmethodID Method = Env->GetStaticMethodID(Class, "GetSensorString", "(I)Ljava/lang/String;"); + if (Method) + { + jstring ResultJString = (jstring)Env->CallStaticObjectMethod(Class, Method, DeviceAddress); + const char* ResultChars = Env->GetStringUTFChars(ResultJString, nullptr); + Result = FString(UTF8_TO_TCHAR(ResultChars)); + Env->ReleaseStringUTFChars(ResultJString, ResultChars); + } + } + } +#endif /* PLATFORM_ANDROID */ + + return Result; +} + +int32 FSGConnectJNI::WriteHaptics(const int32 DeviceAddress, const FString& Haptics) +{ + int32 Result = -999; + +#if PLATFORM_ANDROID + JNIEnv* Env = FAndroidApplication::GetJavaEnv(); + if (Env) + { + jclass Class = FAndroidApplication::FindJavaClass(SGCONNECT_JAVA_CLASS_NAME); + if (!Env->IsSameObject(Class, nullptr)) + { + jmethodID Method = Env->GetStaticMethodID(Class, "ActiveDevices", "(ILjava/lang/String;)I"); + if (Method) + { + jstring HapticsString = Env->NewStringUTF(TCHAR_TO_UTF8(*Haptics)); + Result = Env->CallStaticIntMethod(Class, Method, HapticsString); + } + } + } +#endif /* PLATFORM_ANDROID */ + + return Result; } \ No newline at end of file diff --git a/Source/SenseGloveAndroid/Public/SGAndroid/SGConnectJNI.h b/Source/SenseGloveAndroid/Public/SGAndroid/SGConnectJNI.h index f041e95c..70bd5966 100644 --- a/Source/SenseGloveAndroid/Public/SGAndroid/SGConnectJNI.h +++ b/Source/SenseGloveAndroid/Public/SGAndroid/SGConnectJNI.h @@ -50,4 +50,12 @@ public: static int32 Dispose(); static FString GetLibraryVersion(); + + static int32 ActiveDevices(); + + static FString GetDeviceString(int32 DeviceAddress); + + static FString GetSensorString(int32 DeviceAddress); + + static int32 WriteHaptics(int32 DeviceAddress, const FString& Haptics); }; \ No newline at end of file diff --git a/Source/SenseGloveCore/Private/SGCore/SGDeviceList.cpp b/Source/SenseGloveCore/Private/SGCore/SGDeviceList.cpp index 12f01404..51d5cfb4 100644 --- a/Source/SenseGloveCore/Private/SGCore/SGDeviceList.cpp +++ b/Source/SenseGloveCore/Private/SGCore/SGDeviceList.cpp @@ -51,9 +51,17 @@ #include "SGInterop/SGIC_TArray_TSharedRef_FSGDeviceImpl.h" #include "SGInterop/SGIC_TSharedPtr_FSGDeviceImpl.h" +#if PLATFORM_ANDROID +#include "SGAndroid/SGConnectJNI.h" +#endif /* PLATFORM_ANDROID */ + int32 FSGDeviceList::ActiveDevices() { +#if ! PLATFORM_ANDROID return SGCoreImpl_FSGDeviceListImpl_ActiveDevices(); +#else + return FSGConnectJNI::ActiveDevices(); +#endif /* ! PLATFORM_ANDROID */ } bool FSGDeviceList::SenseCommRunning() @@ -157,27 +165,50 @@ TArray FSGDeviceList::GetDevices(UObject* Outer, const ESGDeviceType bool FSGDeviceList::GetSensorDataString(const int32 IpcIndex, const FString& IpcAddress, FString& OutString) { +#if ! PLATFORM_ANDROID FSGIC_FString IpcAddressContainer{IpcAddress}; FSGIC_FString OutStringContainer; const bool bReturn = SGCoreImpl_FSGDeviceListImpl_GetSensorDataString( IpcIndex, &IpcAddressContainer, &OutStringContainer); OutString = OutStringContainer.String; return bReturn; +#else + FString Result = FSGConnectJNI::GetSensorString(IpcIndex); + if (Result.IsEmpty()) + { + return false; + } + OutString = MoveTemp(Result); + return true; +#endif /* ! PLATFORM_ANDROID */ } bool FSGDeviceList::SendHaptics(const int32 IpcIndex, const FString& IpcAddress, const FString& Commands) { +#if ! PLATFORM_ANDROID FSGIC_FString IpcAddressContainer{IpcAddress}; FSGIC_FString CommandsContainer{Commands}; return SGCoreImpl_FSGDeviceListImpl_SendHaptics(IpcIndex, &IpcAddressContainer, &CommandsContainer); +#else + int32 Result = FSGConnectJNI::WriteHaptics(IpcIndex, Commands); + if (Result <= 0) + { + return false; + } + return true; +#endif /* ! PLATFORM_ANDROID */ } FString FSGDeviceList::GetDeviceStringAt(const int32 IpcIndex, const FString& IpcAddress, const int32 Index) { +#if ! PLATFORM_ANDROID FSGIC_FString IpcAddressContainer{IpcAddress}; FSGIC_FString OutStringContainer; SGCoreImpl_FSGDeviceListImpl_GetDeviceStringAt(&OutStringContainer, IpcIndex, &IpcAddressContainer, Index); return OutStringContainer.String; +#else + return FSGConnectJNI::GetDeviceString(IpcIndex); +#endif /* ! PLATFORM_ANDROID */ } FString FSGDeviceList::GetAddress(const int32 IpcIndex, const FString& IpcAddress) diff --git a/Source/SenseGloveCore/SenseGloveCore.Build.cs b/Source/SenseGloveCore/SenseGloveCore.Build.cs index 7484b642..38493f60 100644 --- a/Source/SenseGloveCore/SenseGloveCore.Build.cs +++ b/Source/SenseGloveCore/SenseGloveCore.Build.cs @@ -55,6 +55,7 @@ public class SenseGloveCore : ModuleRules new string[] { "CoreUObject", + "SenseGloveAndroid", "SenseGloveCoreImpl", "SenseGloveInterop", "SenseGloveLog",