Ambient Light Web API - No Fluff Just Stuff

Ambient Light Web API

Posted by: Thomas Valletta on September 24, 2012

The ambient light sensor can now be accessed from a web page through the Ambient Light API.

I was able to access the ambient light sensor on a couple of my android devices from a web page through the new “devicelight” event listener. At the time of this post, only FireFox Mobile 15.0.1 supports this API.  I tested this on iOS 6 Safari, Opera 12.0.4, Chrome 18.0.1025308, and Browser 4.1.1-398337 as well.  None of them called the event.

My tests were successful on both my Google Galaxy Nexus (Samsung) and my Google Nexus 7 (Asus).  The values that the devices returned were significantly different for similar lighting conditions. This may make it difficult to build software that responds to certain light conditions. The polling interval is also inconsistent between devices.

My Galaxy Nexus gets values that range from 4 to over 50,000. The lowest value: 4 is achieved by covering up the ambient light sensor. On the Galaxy Nexus (and on most phones) it is located close to the ear speaker. Here is a list of values and the conditions in which I receive them:

Galaxy Nexus Sensor Value Nexus 7 Sensor Value Lighting Condition
4 0 Darkest Condition
30-40 20-60 Indoor angled away from lights
200-400 600-850 Indoor angled towards lights
1500-2500 untested Indoor angled towards sunny window
7780 11600 Outdoor angled towards an overcast sun
50,000 untested Outdoor angled towards an unobstructed sun

On the Galaxy Nexus, the event listener is called only when the value changes. If the ambient light is constantly changing, the listener will be called every 200 milliseconds. If the light doesn’t change the event does not get called.

On the Nexus 7, the event listener is called every 69-70 milliseconds. The light value changes constantly. There are times when it takes up to 450 milliseconds between event listener calls, but I attribute this to garbage collection or background processing. The reported light values ar constantly changing.

The good part is that the code couldn’t be easier. Here is mine:

window.addEventListener('devicelight', function(event) {
  console.log(event.value);
});


Thomas Valletta

About Thomas Valletta

Thomas A. Valletta, Mobile Architect, Open Web Evangelist, and hack has been developing for the web for fourteen years. His clients range across industries including defence, healthcare, technology, e-commerce, human resources and religion. He has professionally developed native applications for Android, iPhone, WebOS, Blackberry, and Windows. He has engineered solutions using Java, .Net, PHP, JavaScript, Objective C, VBScript and Commodore Basic (I am pretty sure that those last two don't count). He lives outside of Salt Lake City, Utah with his wife and four children.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »