Welcome to the Oculus Developer Forums!

Your participation on the forum is subject to the Oculus Code of Conduct.

In general, please be respectful and kind. If you violate the Oculus Code of Conduct, your access to the developer forums may be revoked at the discretion of Oculus staff.

OnHead Detection Sensor

fredvicentinfredvicentin Posts: 29
edited October 2015 in Oculus Go Development
Hello Guys, I'm developping for Gear VR on Unity3D

The Gear VR detect when you but the Oculus on the Head, using the distance detection sensor.

I want to put some functions when the user put on or take of the head of the display.
Where can I find the functions that control this features ?

I'm searching on OVRScripts on C# but I dont found the class that turn off or turn on the display.

Comments

  • mrelusivemrelusive Posts: 13 Oculus Staff
    We provide an interface to native applications to detect when the user mounts/unmounts the headset (ovr_HeadsetIsMounted). We will try to make this available to Unity applications as well.
  • wibarra88wibarra88 Posts: 4
    I'm desperately needing the same ability to read proximity of GearVR in Unity. Since it's been a month since the last post, is there any updated timing info for the implementation of this? Seems like a very important and basic event many Unity apps/games could use.

    update - Going to try to build a native plugin to test for removal. If anybody already has one built or started please share. /me launches self into the void...
  • wibarra88wibarra88 Posts: 4
    Here is my 'progress' so far...

    I installed Android Studio and latest OVR Mobile SDK.

    I found ovr_HeadsetIsMounted(); inside of VrApi_Android.h in the OVR Mobile SDK 0.6.0.1 so know it exists and is supposed to return a bool value (see bottom of code chunk):

    excerpt from VrApi_Android.h
    //-----------------------------------------------------------------
    // Device Feature Support
    // These can be called any time from any thread.
    //-----------------------------------------------------------------
    
    OVR_VRAPI_EXPORT int				ovr_GetSystemProperty( const ovrJava * java, const ovrSystemProperty prop );
    
    //-----------------------------------------------------------------
    // Device Status
    //
    // Note that there is currently no way to tell what the initial dock/mount state
    // is when the vrapi_Initialize() is first called. Only when the state changes after
    // vrapi_Initialize() has been called we can tell what the real state is. To work
    // around this problem, we assume the device is docked and the headset is mounted
    // when we enter VR mode, and no dock/mount state changes have been recorded up
    // to that point. Under normal circumstances this is usually correct. This is
    // incorrect when in developer mode, but in that case you want the device to
    // appear as docked and the headset as mounted anyway.
    //
    // These can be called any time from any thread.
    //-----------------------------------------------------------------
    
    // Can be used to detect when the device is docked/undocked.
    OVR_VRAPI_EXPORT bool				ovr_DeviceIsDocked();
    
    // Can be used to detect when the device is mounted/unmounted.
    OVR_VRAPI_EXPORT bool				ovr_HeadsetIsMounted();
    


    I created a new Android Studio project for the plugin and imported the VrApi.jar as a library.

    I looked through the VrApi.jar files and can't find ovr_HeadsetIsMounted(); anywhere in them- maybe that's OK.

    Can someone help take me through the finish line by telling me how to poll ovr_HeadsetIsMounted(); from Java? Hoping this is super simple, I have only used Unity/C# for GearVr before today so it's all new.
  • cyberealitycybereality Posts: 26,156 Oculus Staff
    The ovr_HeadsetIsMounted() is used from native code and doesn't work in Java. However, I am unsure if that function will give you what you need and may not be reliable.
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i
    Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
  • wibarra88wibarra88 Posts: 4
    Thank you for the response!

    I'm installing the Android NDK: https://developer.android.com/ndk/index.html and will try it out today. If I can get a native code plugin working or partially working I'll post code up here for anyone following this thread.
  • wibarra88wibarra88 Posts: 4
    After a week of hair pulling I finally managed to setup my environment for native development, created a functional native .so plugin, successfully communicated with it from C# inside Unity, and attempted to poll the ovr_HeadsetIsMounted method via native code in the plugin.

    For whatever reason ovr_HeadsetIsMounted always returns a FALSE value, no matter physical headset position. So unless I missed something in the code, this method seems unreliable as cybereality suggested.

    If anybody has run into this issue and knows a fix I'll buy you a beer. In the meantime I'll continue poking around. It may have something to do with how the Oculus libs are being built since compiling also builds a sidecar libvrapi.so file that I put in the Android plugins alongside my .so. Maybe they are not talking together at that point and I need to completely wrap libvrapi.so inside my .so somehow- prob in the Android.mk file somewhere if that's it...

    Note - I got no build errors via terminal ndk-build or unity build, and a fake random bool generator worked fine in the same plugin so I know Unity<>my .so communication works fine.
  • isefkaisefka Posts: 8
    I don't know if it can help in Unity but in Java you can detect the headset position by intercepting an intent.
        @Override
        protected void onResume() {
            super.onResume();
            IntentFilter intentFilter = new IntentFilter(
                    "android.intent.action.proximity_sensor");
            mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
    
                    if(intent.getType().equals("1"))
                    {
                        Log.v(LOGTAG,"headset mounted");                    
                    }
                    else
                    {
                        Log.v(LOGTAG,"headset unmounted");                    
                    }
    
                }
            };
            this.registerReceiver(mReceiver, intentFilter);
        }
    	
    	 @Override
        protected void onPause() {
    	super.onPause();
    	unregisterReceiver(mReceiver);
        }
    

    You have to add an intent filter in the manifest :
    &lt;intent-filter&gt;
                    &lt;action android:name="android.intent.action.proximity_sensor" /&gt;
    &lt;/intent-filter&gt;
    
  • Any updates on this capability being exposed in Unity?
  • xamarxamar Posts: 1
    edited June 2016
    @wibarra88
     Any updates on this for Android with Studio? ref
  • hassankhassank Posts: 45
    Brain Burst
    Bump - would also appreciate any update on Unity support!
  • r0b0sarur0b0saru Posts: 47 Oculus Start Member
    I am also in need of a Unity solution for this. I tried OnApplicationHasFocus and OVRManager.hasVrFocus and neither seem to work with GearVR.
Sign In or Register to comment.