Jump to content

[UNITY3d][ANDROID] VIVE Wave XR Plugin 5.1.1 - how to detect headset is mounted / unmounted


Greg_

Recommended Posts

Hi @UnendlicheMoeglichkeiten,

Here comes the solution for quick support by following the code snippet to modify WaveVR SDK. You can use GetHMDWornState after doing so. We will support the function in the next release.

image.png.96bd4f8ad5a5f37246be08c95a524c29.png

 

You need to modify the following four files and put the attached file ProximitySensorEventListener.java under Plugins\WaveVR\Source\WaveVR\java\com\htc\vr\unreal

modified:   plugin/Plugins/WaveVR/Source/WaveVR/Private/Platforms/Android/JNI.cpp
modified:   plugin/Plugins/WaveVR/Source/WaveVR/Private/WaveVRHMD.cpp
modified:   plugin/Plugins/WaveVR/Source/WaveVR/Private/WaveVRHMD.h
modified:   plugin/Plugins/WaveVR/Source/WaveVR/WaveVR_UPL.xml

prerequisite:

The project should be a C++ project

Here are the steps:

1.  Put the attached file ProximitySensorEventListener.java under Plugins\WaveVR\Source\WaveVR\java\com\htc\vr\unreal

2. modified:   plugin/Plugins/WaveVR/Source/WaveVR/Private/Platforms/Android/JNI.cpp

 // conditions signed by you and all SDK and API requirements,
 // specifications, and documentation provided by HTC to You."

+#include "HeadMountedDisplayTypes.h"
+
 #if PLATFORM_ANDROID
 #include "RequestResultObject.h"
 #include "RequestUsbResultObject.h"
@@ -231,4 +233,8 @@ extern "C" void Java_com_htc_vr_unreal_VRTransitionBroadcastReceiver_StateOnRece
         FWaveVRHMD::SetVRTransitionState(state);
 }

+extern "C" void Java_com_htc_vr_unreal_ProximitySensorEventListener_OnHMDWornStateChanged(JNIEnv* LocalJNIEnv, jobject LocalThiz, jboolean isWearing) {
+       (isWearing) ? FWaveVRHMD::SetHMDWornState(EHMDWornState::Worn) : FWaveVRHMD::SetHMDWornState(EHMDWornState::NotWorn);
+}
+
 #endif  // PLATFORM_ANDROID

3.  plugin/Plugins/WaveVR/Source/WaveVR/Private/WaveVRHMD.h

@@ -131,6 +131,7 @@ public:
        virtual void RecordAnalytics() override { LOG_FUNC(); FHeadMountedDisplayBase::RecordAnalytics(); }
        virtual bool IsRenderingPaused() const override { LOG_FUNC(); return IHeadMountedDisplay::IsRenderingPaused();/*false*/ }
        virtual void SetClippingPlanes(float NCP, float FCP) override;
+       virtual EHMDWornState::Type GetHMDWornState() override { return HMDWornState; }

        /** IHeadMountedDisplay interface not used*/
        virtual void EnableHMD(bool allow = true) override;
@@ -156,7 +157,6 @@ public:
        virtual const float* GetBlueDistortionParameters() const override { LOG_FUNC(); return nullptr; }
        virtual bool DoesAppUseVRFocus() const override { LOG_FUNC(); return IHeadMountedDisplay::DoesAppUseVRFocus(); }
        virtual bool DoesAppHaveVRFocus() const override { LOG_FUNC(); return IHeadMountedDisplay::DoesAppHaveVRFocus(); }
-       virtual EHMDWornState::Type GetHMDWornState() override { return EHMDWornState::Unknown; }

         //** FHeadMountedDisplayBase interface */
         virtual bool PopulateAnalyticsAttributes(TArray<struct FAnalyticsEventAttribute>& EventAttributes) override { LOG_FUNC(); return FHeadMountedDisplayBase::PopulateAnalyticsAttributes(EventAttributes);}
@@ -562,6 +562,7 @@ public:
        static void SetVRTransitionState(bool state);
        bool IsVRTransitionShowing();
        void SetFrameSharpnessEnhancementLevel(float level);
+       static void SetHMDWornState(EHMDWornState::Type state) { LOG_FUNC(); HMDWornState = state; }

 private:
        bool bHmdEnabled;
@@ -606,6 +607,8 @@ private:
        static bool bVRTransitionIsShowning;
        static bool bIsVRTransitionInUse; //Focus3 and later.

+       static EHMDWornState::Type HMDWornState;
+
        TArray<FTransform> PoseModeOffsets;
        uint8_t CachedPoseModes[2] = { 0 };

4.  plugin/Plugins/WaveVR/Source/WaveVR/Private/WaveVRHMD.cpp

@@ -2367,3 +2367,5 @@ void FWaveVRHMD::SetFrameSharpnessEnhancementLevel(float level)
        LOG_FUNC();
        mRender.SetFrameSharpnessEnhancementLevel(level);
 }
+
+EHMDWornState::Type FWaveVRHMD::HMDWornState = EHMDWornState::Unknown;

5. modified:   plugin/Plugins/WaveVR/Source/WaveVR/WaveVR_UPL.xml

@@ -163,6 +163,7 @@
                        import com.htc.vr.unreal.OEMConfig;
                        import com.htc.vr.unreal.SoftwareIpd;
                        import com.htc.vr.unreal.VRTransitionBroadcastReceiver;
+                       import com.htc.vr.unreal.ProximitySensorEventListener;
                </insert>
        </gameActivityImportAdditions>

@@ -180,6 +181,8 @@
                        private PermissionCallback mPermissionCallback = null;
                        private OEMConfig mOEMConfig = null;
                        private VRTransitionBroadcastReceiver mVRTransitionBR = null;
+                       private Sensor mProximity = null;
+                       private ProximitySensorEventListener mProximitySensorEventListener = null;

                        ///@Override
                        ///public void setRequestedOrientation(int requestedOrientation) {
@@ -289,6 +292,9 @@
                        mOEMConfig.setDelegate(mVRActivityDelegate);

                        mVRTransitionBR = VRTransitionBroadcastReceiver.getInstance();
+
+                       mProximity = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
+                       mProximitySensorEventListener = new ProximitySensorEventListener();
                </insert>
        </gameActivityOnCreateAdditions>

@@ -348,6 +354,12 @@
                        mVRActivityDelegate.onPause();
                        <!--for Permission Manager-->
                        mPM.onPauseAndReturnFail();
+
+                       if (mProximitySensorEventListener != null)
+                       {
+                               sensorManager.unregisterListener(mProximitySensorEventListener);
+                       }
+
                        if (isFinishing()) {
                                Log.debug("Activity isFinishing()!");
                        }
@@ -360,6 +372,12 @@
                        Log.debug("[WaveVR_UPL Additions] onResume()!");
                        mOEMConfig.onResume();
                        mVRActivityDelegate.onResume();
+
+                       if (mProximity != null &amp;&amp; mProximitySensorEventListener != null)
+                       {
+                               sensorManager.registerListener(mProximitySensorEventListener, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
+                       }
+
                        <!--for Permission Manager-->
                        mPM.onResume();
                        <!--Workaround to prevent restarting application.-->

6. Rebuild the Development Editor, the GetHMDWornState will take effect.

ProximitySensorEventListener.java

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...