Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Diving Into the Device API

Diving Into the Device API

Matt Steele

August 07, 2013
Tweet

More Decks by Matt Steele

Other Decks in Programming

Transcript

  1. http://www.flickr.com/photos/hitormiss/4835135015/ Tuesday, August 6, 13 Hey folks, I’m Matt Steele.

    I work on mobile web stuff at Union Pacific, and I’m @mattdsteele on Twitter and GitHub.
  2. http://www.flickr.com/photos/hitormiss/4835135015/ @mattdsteele Tuesday, August 6, 13 Hey folks, I’m Matt

    Steele. I work on mobile web stuff at Union Pacific, and I’m @mattdsteele on Twitter and GitHub.
  3. Native vs HTML5 Tuesday, August 6, 13 So I’m here

    to talk about one facet of the great Native vs HTML5 war
  4. Tuesday, August 6, 13 It’s a debate that’s about as

    old as the Internet itself. Pretty sure the guy on the right builds native iOS apps.
  5. Tuesday, August 6, 13 And this guy builds native Windows

    Phone apps. I just wanted to use some of these pictures.
  6. Tuesday, August 6, 13 Anyway, Tim Wright had an article

    in A List Apart called “Environmental Design”. His point was that your apps should respond to the environment they’re running in: dimming the screen in a dark room, etc.
  7. “A year ago, the device-level APIs needed weren’t ready for

    primetime yet. Today, we can start to do something to improve our users’ experiences, even under these dynamic conditions, thanks to the recent buildup of the Device API.” http://alistapart.com/article/environmental-design-with-the-device-api Tuesday, August 6, 13 So he advocated building websites with this tech. Traditionally you’ve had to go to native code to get access.
  8. Device API Tuesday, August 6, 13 So Device API is

    a set of features that give browser access to more sensor inputs, where native always has had a leg up. Originally this started with Mozilla’s FirefoxOS, but lots of it is being standardized by the W3C right now. Let’s talk about three of these APIs.
  9. navigator.battery.addEventListener('chargingchange', function(e) { var isCharging = navigator.battery.charging; console.log('The battery '

    + isCharging ? 'is' : 'is not' + 'charging'); }); Tuesday, August 6, 13 Here’s an example of the code. You get a new Battery object on Navigator; as well as a set of events you can listen to as the battery level drops or when you plug into power.
  10. Support Android Desktop FirefoxOS Tuesday, August 6, 13 Just firefox

    right now. The code has actually been implemented in WebKit for a year, but it’s never been turned on. No idea why.
  11. Ambient Light Sensor Tuesday, August 6, 13 Most phones have

    an ambient light sensor to dim the screen in dark rooms; but you can capture light using a camera if needed.
  12. window.addEventListener('devicelight', function(event) { console.log('Light level: ' + event.value + '

    lux'); }); Tuesday, August 6, 13 You get one event to hook to. The ‘value’ is measured in lux, but from my testing it appears that a reading of 100 lux on one device might translate to only 25 lux on another. So your mileage may vary.
  13. Support Android Desktop (Mac) FirefoxOS Tuesday, August 6, 13 Just

    Firefox, and haven’t seen any movement in other browsers yet.
  14. window.addEventListener('deviceorientation', function(event) { console.log('The device is rotated' + event.alpha +

    'degrees'); }); Tuesday, August 6, 13 The main event to hook into is deviceorientation, which lets you inspect the device’s: alpha: Compass direction beta: Tilted front to back Gamma: tilted left to right Also has a DeviceMotion event, which lets you use accelerometer data
  15. Support IE11 15+ Mobile 4+ Tuesday, August 6, 13 Great

    support, but here there be dragons. Big discrepancies in implementation. For example, rotating device clockwise increases the alpha value on Mobile Safari, but *decreases* it on Android Chrome. A library I wish existed: something to normalize the discrepancies. Make a name for yourself!
  16. Tuesday, August 6, 13 Lots of other APIs: - Network

    information: crappy bandwidth, 3G, etc - Proximity - Vibration