iosarkitlight-sensor

Reading iOS light sensor with ARKit


Is there a way to access the ambient light sensor of an iOS device, using the ARKit, without using the AR at all?

https://developer.apple.com/documentation/arkit/arlightestimate/2878308-ambientintensity

In other words, can I access the value of "ambientIntensity" without creating an AR scene.


Solution

  • See the docs for ARLightEstimate.ambientIntensity:

    This value is based on the internal exposure compensation of the camera device

    In other words, if you want to use the device camera to estimate local lighting conditions and aren't otherwise using ARKit, you might be better off using the camera APIs. (For one thing, those APIs are available on all iOS 11 devices and several earlier iOS versions, rather than needing the steep OS/hardware requirements of ARKit.)

    A quick tour of what you'll need to do there:

    1. Set up an AVCaptureSession and choose the camera AVCaptureDevice that you want. You may or may not need to wire up a video/photo capture output (which in your case will be mostly unused).
    2. Start running the capture session.
    3. Use KVO to monitor the exposure, temperature, and/or white balance related properties on AVCaptureDevice.

    You can find (older, ObjC) code covering all this (and a lot more, so you'll need to extract the parts that are relevant to you) in Apple's AVCamManual sample code.