Search Unity

Resolved InputDevices.deviceConnected does not fire on Quest

Discussion in 'AR/VR (XR) Discussion' started by float, Feb 6, 2020.

Thread Status:
Not open for further replies.
  1. float

    float

    Joined:
    Jul 29, 2012
    Posts:
    42
    Unity 2019.3.0f6
    I've made a small script that should load the devices found. While it seems to work in the Editor (on a Rift) i get not a single event that something has been connected or found.

    If i use InputDevices.GetDeviceAtXRNode(XRNode.RightHand) the system finds the device.

    Is that a bug or does the Quest just not work with that?

    Thanks
     
    Last edited: Feb 6, 2020
  2. StayTalm_Unity

    StayTalm_Unity

    Unity Technologies

    Joined:
    May 3, 2017
    Posts:
    182
    Maybe?
    Before the 2019.3 final release candidates I found a bug where the call from C++ -> C# that reported a newly connected device was stripped out in certain configurations (IL2CPP mainly). However, that got fixed. Is it possible you aren't getting connected events because those devices are already connected? Some platforms are able to report devices so early they beat Monobehaviour.Start(). Check if GetDevice retrieves something *before* you hook up the event delegate in case it's already connected.
     
  3. float

    float

    Joined:
    Jul 29, 2012
    Posts:
    42
    Hello and thank you for the reply.
    Yes, it seems that the devices are allready connected even bevore the OnEnable() function.
    So i just let my InitializeDevices-Method run in the Start again. On the Rift it will be called mutliple times anyway so it does not matter :D

    Code (CSharp):
    1. private void Awake()
    2.         {
    3.             if (Instance == null)
    4.                 Instance = this;
    5.  
    6.             rightCtrlChara = (InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Right);
    7.             leftCtrlChara  = (InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Left);
    8.         }
    9.  
    10.         private void Start()
    11.         {
    12.             InitializeDevices();
    13.         }
    14.  
    15.         private void InitializeDevices()
    16.         {
    17.             List<InputDevice> devices = new List<InputDevice>();
    18.  
    19.             InputDevices.GetDevicesWithCharacteristics(rightCtrlChara, devices);
    20.             if (devices.Count > 0)
    21.                 rightCtrl = devices[0];
    22.  
    23.             devices.Clear();
    24.  
    25.             InputDevices.GetDevicesWithCharacteristics(leftCtrlChara, devices);
    26.             if (devices.Count > 0)
    27.                 leftCtrl = devices[0];
    28.         }
    29.  
    30.         private void OnDeviceConnected(InputDevice device)
    31.         {
    32.             InitializeDevices();
    33.         }
    34.  
    35.         private void OnDeviceDisconnected(InputDevice device)
    36.         {
    37.             InitializeDevices();
    38.         }
    39.  
    40.         void OnEnable()
    41.         {
    42.             InputDevices.deviceConnected += OnDeviceConnected;
    43.             InputDevices.deviceDisconnected += OnDeviceDisconnected;
    44.         }
    45.  
    46.         private void OnDisable()
    47.         {
    48.             InputDevices.deviceConnected -= OnDeviceConnected;
    49.             InputDevices.deviceDisconnected -= OnDeviceDisconnected;
    50.         }
     
  4. StayTalm_Unity

    StayTalm_Unity

    Unity Technologies

    Joined:
    May 3, 2017
    Posts:
    182
    Phew glad it worked out.
    I'm documenting a lot of these quirks to throw into the manual.
     
  5. Lumen-Digital

    Lumen-Digital

    Joined:
    Aug 3, 2012
    Posts:
    16
    This is still an issue in 2020.17f1

    A catch-all solution, while we wait for a fix, is a coroutine...

    Code (CSharp):
    1.  
    2. private void Start()
    3.     {
    4.         StartCoroutine(InitializeDevices());
    5.     }
    6.  
    7. IEnumerator InitializeDevices()
    8.     {
    9.         WaitForEndOfFrame wait = new WaitForEndOfFrame();
    10.         List<InputDevice> devices = new List<InputDevice>();
    11.         InputDevices.GetDevices(devices);
    12.         while (devices.Count == 0)
    13.         {
    14.             yield return wait;
    15.             InputDevices.GetDevices(devices);
    16.         }
    17.         foreach (InputDevice device in devices)
    18.         {
    19. // Do stuff...
    20.         }
    21.     }
     
  6. Shaunyowns

    Shaunyowns

    Joined:
    Nov 4, 2019
    Posts:
    328
    Hey @Lumen-Digital, can you file a bug to make sure this lands in our backlog so the team can help resolve this issue?
     
    mgear likes this.
Thread Status:
Not open for further replies.