简体   繁体   中英

Reading the iPhone's Ambient Light sensor

I notice on my iPhone, after a few seconds of being in direct sun light, the screen will adjust to become brighter, dimmer etc. I was wondering if there was a way to interact with this sensor?

I have an application which is used outside. When you go into direct light, it becomes very difficult to see the screen for a few momments, before it adjusts. And even then, it's not always as bright as I'd like it to be. I would like to implement a high contrast skin for outdoor viewing, and a low contrast for indoor viewing.

Is this possible to read light sensor data, and if so, how do I extract these sensor values?

I would assume there is a light sensor however, as the camera knows when to use the flash.

On the other hand this is a different idea (maybe a silly one), using the brightness of the device's screen you can get some value of the external conditions.

From 0.12 (Dark) to 0.99 (Light)

The next line will get those values, give it a try, put some light on and off over the device to get different values.

NSLog(@"Screen Brightness: %f",[[UIScreen mainScreen] brightness]);

Obviously Automatic Brightness feature should be turned on in order to get this to work.

Regards.

To read the ambient light sensor data, you need to use IOHID in the IOKit framework.

http://iphonedevwiki.net/index.php/AppleISL29003

http://iphonedevwiki.net/index.php/IOKit.framework

However, this requires private headers, so if you use it, Apple probably won't let your app into the app store.

I continually ask the iOS forums whether there will be support for ambient light sensor readings in the future, but to no avail.

You can actually use the camera to do this, which is independent of the user's screen brightness settings (and works even if Automatic Brightness is OFF).

You read the Brightness Value from the video frames' metadata as I explain in this Stack Overflow answer .

Try using GSEventSetBacklightLevel(); , which requires <GraphicsServices/GraphicsServices.h> . This is how one can programmatically adjust the brightness levels. There is also a get option, so I think that may have the information you're after.

For Swift 5, here is how to use the brightness detection which indirectly gives you the luminosity of the outside:

/// A view controller (you can use any UIView or AnyObj)
class MyViewConroller: UIViewController { 

    /// Remove observers on deinit
    deinit {
        removeObservers()
    }

    // MARK: - Observers management helpers

    /// Add my observers to the vc
    func addObservers() {

        NotificationCenter.default.addObserver(self, selector: #selector(onScreenBrightnessChanged(_:)), name: UIScreen.brightnessDidChangeNotification, object:nil)
    }

    /// Clean up observers
    func removeObservers() {
        NotificationCenter.default.removeObserver(self)
    }

    /// Load the views
    func loadView() {
        // Add my observes to the vc
        addObservers()
    }

    /**
    Handles brightness changes
    */
    @objc func onScreenBrightnessChanged(_ sender: Notification) {

        // Tweak as needed: 0.5 is a good value for me
        let isDark = UIScreen.main.brightness < 0.5.   // in 0...1
        // Do whatever you want with the `isDark` flag: here I turn the headlights off
        vehicle.turnOnTheHeadlights( isDark )
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM