Reading the iPhone's Ambient Light sensor
Asked Answered
R

6

24

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.

Reasonable answered 10/6, 2011 at 16:54 Comment(2)
It's extremely frustrating that this question (and the answer about using screen brightness) don't show up for most S.O. searches I do on this topic. Even knowing that the question and answer are here, I was unable to find it. Fortunately I'd put a link in a comment in my code.Ombudsman
I've edited the title to something I hope will show up in results – sorry if this isn't appropriate!Ombudsman
S
6

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.

Sevenup answered 10/6, 2011 at 16:57 Comment(2)
@George, ok, so read the current setting and set up a notification if it changes. Then you can adjust accordingly.Sevenup
Sorry, I doesn't see this get option. Could you post a link?Kilt
R
23

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.

Ratsbane answered 10/4, 2013 at 22:17 Comment(2)
VERY clever, and works like a charm! :D Although according to Apple docs, it doesn't go from 0.12 to 0.99: "The value of this property should be a number between 0.0 and 1.0, inclusive."Distill
UIScreen also posts UIScreenBrightnessDidChangeNotification when the level changes.Ombudsman
S
14

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.

Standard answered 1/9, 2011 at 21:3 Comment(0)
D
10

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.

Drawtube answered 20/4, 2014 at 9:43 Comment(3)
would this turn on the camera? and therefore also ask the user for permission to do so?Minyan
Yes, it would. Any use of the camera does this.Drawtube
It would also use a lot more battery than the ambient light sensor, which, for my use case, defeats the purpose.Walkling
S
6

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.

Sevenup answered 10/6, 2011 at 16:57 Comment(2)
@George, ok, so read the current setting and set up a notification if it changes. Then you can adjust accordingly.Sevenup
Sorry, I doesn't see this get option. Could you post a link?Kilt
T
4

For iOS 14 and above, Apple has provided SensorKit (https://developer.apple.com/documentation/sensorkit/srsensor/3377673-ambientlightsensor ) for explicit access to all kinds of sensors and system logs (call logs, message logs, etc.). In addition to the raw lux value, you can also get the chromaticity of the ambient light and the orientation relative to the device sensor.

(From https://developer.apple.com/documentation/sensorkit/srambientlightsample )

Measuring Light Level

var chromaticity: SRAmbientLightSample.Chromaticity A coordinate pair that describes the sample’s light brightness and tint.

struct SRAmbientLightSample.Chromaticity A coordinate pair that describes light brightness and tint.

var lux: Measurement An object that describes the sample’s luminous flux.

var placement: SRAmbientLightSample.SensorPlacement The light’s location relative to the sensor.

enum SRAmbientLightSample.SensorPlacement Directional values that describe light-source location with respect to the sensor.

However, you need to request for approval for such an App to be accepted and published on App Store.

Tallia answered 20/9, 2021 at 2:34 Comment(0)
O
2

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 )
    }
}
Oberammergau answered 29/4, 2020 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.