How to detect camera permission in Niantic Studio

Howdy,

I need to know when the user has accepted camera permissions in Niantic Studio so I can begin the game.

I’m looking for the equivalent to this:

window.addEventListener(‘realityready’, () => {
console.log(‘Camera is ready and permission was granted’);
});

Any ideas?

window.addEventListener('xrloaded', () => {console.log('ready')})

Is what you’re looking for. :slight_smile:

1 Like

Hey George!

So the good news is the call back works, but unfortunately it gets called before the user gives camera permissions.

Is there by any chance another method?

Cheers!

You could try something like the following, however it would need to check in tick since there’s no way to listen for the permission granted as far as I know.

async function onCameraPermissionGranted(onGranted) {
  if (!navigator.permissions) return;
  try {
    const status = await navigator.permissions.query({ name: 'camera' });
    if (status.state === 'granted') onGranted();
    status.onchange = () => {
      if (status.state === 'granted') onGranted();
    };
  } catch (err) {
    console.error('Could not query camera permission:', err);
  }
}

onCameraPermissionGranted(() => {
  console.log('Camera permission is granted – go ahead and use the camera!');
});
1 Like

Are you sure that this works? Doesn’t seem like it’s firing if I add it in my component?


add: (world, component) => {
    window.addEventListener('xrloaded', () => {
      console.log('XR8 Engine Loaded. Ready for XR8 configurations.')
      XR8.addCameraPipelineModule({
        name: 'microphone',
        requiredPermissions: () => ([XR8.XrPermissions.permissions().MICROPHONE]),
      })
      XR8.addCameraPipelineModule(XR8.MediaRecorder.pipelineModule())
      console.log('should see mic permissions?')
    })
  },


it seems like adding it in add: is not good practice because window is not available yet so I move it to .onEnter and it seems ok.

1 Like

Hey Sebastian! I’ve been running in the same issue and have been trying to figure out a way to do this properly for like the past 4 days… and I think i found the trick! Originally, I was trying what @GeorgeButler sugested to basically check the navigator permission status onTick() but it didn’t seem to work for permission denials so I read through the legacy docs to see if there wasn’t any obscure events deep in there I could use and I found this onCameraStatusChange() here that is pretty useful to know when the camera is ready etc… So I implemented this component and seems to work ok for me. The only downfall is that this is only for the actual camera feed so it does really detect other permissions like gyro or gps but you can use it to know generally if the AR is ready.


import * as ecs from '@8thwall/ecs'  

ecs.registerComponent({
  name: 'permissionsManager',

  stateMachine: ({world, eid, schemaAttribute, dataAttribute}) => {

    ecs.defineState('initial')
      .initial()
      .onEnter(() => {
        console.log('[permissionsManager] entered initial state')
      })
      .onEvent('userClickedPrePrompt', 'pending', {target: world.events.globalId})

    ecs.defineState('pending')
      .onEnter(() => {
        console.log('[permissionsManager] entered pending state')
        const {XR8} = window as any
        if (XR8) {
          console.log('XR8 available')
          XR8.addCameraPipelineModule({
            name: 'permissions-check',
            onCameraStatusChange: ({status}) => {
              if (status === 'requesting') {
                console.log('[permissionsManager] requesting status')
                world.events.dispatch(world.events.globalId, 'requestingPermissions')
              } else if (status === 'hasStream') {
                console.log('[permissionsManager] hasStream status')
                world.events.dispatch(world.events.globalId, 'userGrantedAllPermissions')
              } else if (status === 'hasVideo') {
                console.log('hasvideo status')
                console.log('[permissionsManager] hasVideo status')
                world.events.dispatch(world.events.globalId, 'arExperienceReady')
              } else if (status === 'failed') {
                console.log('[permissionsManager] failed status')
                world.events.dispatch(world.events.globalId, 'userDeniedSomePermissions')
              }
            },
          })
        } else {
          console.log('XR8 not available')
        }
      })
  },
})


1 Like